1/* 2 * Copyright (c) 2011-2015, Intel Corporation 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation and/or 13 * other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its contributors 16 * may be used to endorse or promote products derived from this software without 17 * specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30#include "ParameterBlackboard.h" 31#include "Iterator.hpp" 32#include "AlwaysAssert.hpp" 33#include <algorithm> 34 35// Size 36void CParameterBlackboard::setSize(size_t size) 37{ 38 mBlackboard.resize(size); 39} 40 41size_t CParameterBlackboard::getSize() const 42{ 43 return mBlackboard.size(); 44} 45 46// Single parameter access 47void CParameterBlackboard::writeInteger(const void *pvSrcData, size_t size, size_t offset) 48{ 49 assertValidAccess(offset, size); 50 51 auto first = MAKE_ARRAY_ITERATOR(static_cast<const uint8_t *>(pvSrcData), size); 52 auto last = first + size; 53 auto dest_first = atOffset(offset); 54 55 std::copy(first, last, dest_first); 56} 57 58void CParameterBlackboard::readInteger(void *pvDstData, size_t size, size_t offset) const 59{ 60 assertValidAccess(offset, size); 61 62 auto first = atOffset(offset); 63 auto last = first + size; 64 auto dest_first = MAKE_ARRAY_ITERATOR(static_cast<uint8_t *>(pvDstData), size); 65 66 std::copy(first, last, dest_first); 67} 68 69void CParameterBlackboard::writeString(const std::string &input, size_t offset) 70{ 71 assertValidAccess(offset, input.size() + 1); 72 73 auto dest_last = std::copy(begin(input), end(input), atOffset(offset)); 74 *dest_last = '\0'; 75} 76 77void CParameterBlackboard::readString(std::string &output, size_t offset) const 78{ 79 // As the string is null terminated in the blackboard, 80 // the size that will be read is not known. (>= 1) 81 assertValidAccess(offset, sizeof('\0')); 82 83 // Get the pointer to the null terminated string 84 const uint8_t *first = &mBlackboard[offset]; 85 output = reinterpret_cast<const char *>(first); 86} 87 88void CParameterBlackboard::writeBuffer(const void *pvSrcData, size_t size, size_t offset) 89{ 90 writeInteger(pvSrcData, size, offset); 91} 92void CParameterBlackboard::readBuffer(void *pvDstData, size_t size, size_t offset) const 93{ 94 readInteger(pvDstData, size, offset); 95} 96 97// Element access 98void CParameterBlackboard::writeBytes(const std::vector<uint8_t> &bytes, size_t offset) 99{ 100 assertValidAccess(offset, bytes.size()); 101 102 std::copy(begin(bytes), end(bytes), atOffset(offset)); 103} 104 105void CParameterBlackboard::readBytes(std::vector<uint8_t> &bytes, size_t offset) const 106{ 107 assertValidAccess(offset, bytes.size()); 108 109 std::copy_n(atOffset(offset), bytes.size(), begin(bytes)); 110} 111 112// Access from/to subsystems 113uint8_t *CParameterBlackboard::getLocation(size_t offset) 114{ 115 assertValidAccess(offset, 1); 116 return &mBlackboard[offset]; 117} 118 119// Configuration handling 120void CParameterBlackboard::restoreFrom(const CParameterBlackboard *pFromBlackboard, size_t offset) 121{ 122 const auto &fromBB = pFromBlackboard->mBlackboard; 123 assertValidAccess(offset, fromBB.size()); 124 std::copy(begin(fromBB), end(fromBB), atOffset(offset)); 125} 126 127void CParameterBlackboard::saveTo(CParameterBlackboard *pToBlackboard, size_t offset) const 128{ 129 auto &toBB = pToBlackboard->mBlackboard; 130 assertValidAccess(offset, toBB.size()); 131 std::copy_n(atOffset(offset), toBB.size(), begin(toBB)); 132} 133 134void CParameterBlackboard::assertValidAccess(size_t offset, size_t size) const 135{ 136 ALWAYS_ASSERT(offset + size <= getSize(), 137 "Invalid data size access: offset=" << offset << " size=" << size 138 << "reference size=" << getSize()); 139} 140