Home | History | Annotate | Download | only in parameter
      1 /*
      2  * Copyright (c) 2011-2014, 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 <string.h>
     32 #include <assert.h>
     33 
     34 CParameterBlackboard::CParameterBlackboard() : _pucData(NULL), _uiSize(0)
     35 {
     36 }
     37 
     38 CParameterBlackboard::~CParameterBlackboard()
     39 {
     40     delete [] _pucData;
     41 }
     42 
     43 // Size
     44 void CParameterBlackboard::setSize(uint32_t uiSize)
     45 {
     46     if (_pucData) {
     47 
     48         delete [] _pucData;
     49     }
     50 
     51     _pucData = new uint8_t[uiSize];
     52 
     53     memset(_pucData, 0, uiSize);
     54 
     55     _uiSize = uiSize;
     56 }
     57 
     58 uint32_t CParameterBlackboard::getSize() const
     59 {
     60     return _uiSize;
     61 }
     62 
     63 // Single parameter access
     64 void CParameterBlackboard::writeInteger(const void* pvSrcData, uint32_t uiSize, uint32_t uiOffset, bool bBigEndian)
     65 {
     66     assert(uiSize + uiOffset <= _uiSize);
     67 
     68     if (!bBigEndian) {
     69 
     70         memcpy(_pucData + uiOffset, pvSrcData, uiSize);
     71     } else {
     72 
     73         uint32_t uiIndex;
     74         const uint8_t* puiSrcData = (const uint8_t*)pvSrcData;
     75 
     76         for (uiIndex = 0; uiIndex < uiSize; uiIndex++) {
     77 
     78             _pucData[uiIndex + uiOffset] = puiSrcData[uiSize - uiIndex - 1];
     79         }
     80     }
     81 }
     82 
     83 void CParameterBlackboard::readInteger(void* pvDstData, uint32_t uiSize, uint32_t uiOffset, bool bBigEndian) const
     84 {
     85     assert(uiSize + uiOffset <= _uiSize);
     86 
     87     if (!bBigEndian) {
     88 
     89         memcpy(pvDstData, _pucData + uiOffset, uiSize);
     90     } else {
     91 
     92         uint32_t uiIndex;
     93         uint8_t* puiDstData = (uint8_t*)pvDstData;
     94 
     95         for (uiIndex = 0; uiIndex < uiSize; uiIndex++) {
     96 
     97             puiDstData[uiSize - uiIndex - 1] = _pucData[uiIndex + uiOffset];
     98         }
     99     }
    100 }
    101 
    102 void CParameterBlackboard::writeString(const std::string &input, uint32_t uiOffset)
    103 {
    104     strcpy((char*)_pucData + uiOffset, input.c_str());
    105 }
    106 
    107 void CParameterBlackboard::readString(std::string &output, uint32_t uiOffset) const
    108 {
    109     output = std::string((const char*)_pucData + uiOffset);
    110 }
    111 
    112 // Access from/to subsystems
    113 uint8_t* CParameterBlackboard::getLocation(uint32_t uiOffset)
    114 {
    115     return _pucData + uiOffset;
    116 }
    117 
    118 // Configuration handling
    119 void CParameterBlackboard::restoreFrom(const CParameterBlackboard* pFromBlackboard, uint32_t uiOffset)
    120 {
    121     memcpy(_pucData + uiOffset, pFromBlackboard->_pucData, pFromBlackboard->_uiSize);
    122 }
    123 
    124 void CParameterBlackboard::saveTo(CParameterBlackboard* pToBlackboard, uint32_t uiOffset) const
    125 {
    126     memcpy(pToBlackboard->_pucData, _pucData + uiOffset, pToBlackboard->_uiSize);
    127 }
    128 
    129 // Serialization
    130 void CParameterBlackboard::serialize(CBinaryStream& binaryStream)
    131 {
    132     if (binaryStream.isOut()) {
    133 
    134         binaryStream.write(_pucData, _uiSize);
    135     } else {
    136 
    137         binaryStream.read(_pucData, _uiSize);
    138     }
    139 }
    140