[219] | 1 | #ifndef __XMLIO_CBuffer_impl__ |
---|
| 2 | #define __XMLIO_CBuffer_impl__ |
---|
| 3 | |
---|
| 4 | namespace xmlioserver |
---|
| 5 | { |
---|
| 6 | namespace comm |
---|
| 7 | { |
---|
| 8 | /// ////////////////// Définitions (inline) /////////////////// /// |
---|
| 9 | |
---|
| 10 | #define DATA_HEADER_SIZE sizeof(CBufferDataType) + 2 * sizeof(StdSize) + sizeof(bool) |
---|
| 11 | |
---|
| 12 | template <class T> |
---|
| 13 | StdSize CBuffer::getRequestedSize(T data) const |
---|
| 14 | { return (DATA_HEADER_SIZE + sizeof (T)); } |
---|
| 15 | |
---|
| 16 | template <class T> |
---|
| 17 | StdSize CBuffer::getRequestedSize(ARRAY(T, 1) data) const |
---|
| 18 | { return (DATA_HEADER_SIZE + data->num_elements() * sizeof (T)); } |
---|
| 19 | |
---|
| 20 | template <> |
---|
| 21 | StdSize CBuffer::getRequestedSize(StdString data) const; |
---|
| 22 | |
---|
| 23 | //--------------------------------------------------------------- |
---|
| 24 | |
---|
| 25 | template <class T> |
---|
| 26 | void CBuffer::getData(T & data, StdSize position) const |
---|
| 27 | { |
---|
| 28 | if (this->size < (position + this->getRequestedSize(data))) |
---|
| 29 | ERROR("CBuffer::getData(data, position)", |
---|
| 30 | << " Buffer size < size + position !"); |
---|
| 31 | CBufferData bufdata; |
---|
| 32 | this->getBufferData(bufdata, position); |
---|
| 33 | if (bufdata.type != this->getBufferDataType<T>()) |
---|
| 34 | ERROR("CBuffer::getData(data, position)", |
---|
| 35 | << "[ BufferDataType Read : " << bufdata.type << ", " |
---|
| 36 | << ", BufferDataType T : " << this->getBufferDataType<T>() << "] " |
---|
| 37 | << " invalid type !"); |
---|
| 38 | if (bufdata.isArray != false) |
---|
| 39 | ERROR("CBuffer::getData(data, position)", |
---|
| 40 | << " type should be an array !"); |
---|
| 41 | |
---|
| 42 | this->getData(reinterpret_cast<char*>(&data), bufdata.size , bufdata.position); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | template <class T> |
---|
| 46 | void CBuffer::getDataArray(ARRAY(T, 1) data, StdSize position) const |
---|
| 47 | { |
---|
| 48 | CBufferData bufdata; |
---|
| 49 | this->getBufferData(bufdata, position); |
---|
| 50 | if (bufdata.type != this->getBufferDataType<T>()) |
---|
| 51 | ERROR("CBuffer::getDataArray(data, position)", << " invalid type !"); |
---|
| 52 | if (bufdata.isArray != true) |
---|
| 53 | ERROR("CBuffer::getDataArray(data, position)", |
---|
| 54 | << " type should not be an array !"); |
---|
| 55 | if (this->size < (position + (DATA_HEADER_SIZE + bufdata.size))) |
---|
| 56 | ERROR("CBuffer::getData<T>(data, size, position)", |
---|
| 57 | << " Buffer size < size + position !"); |
---|
| 58 | data->resize(boost::extents[bufdata.size/sizeof(T)]); |
---|
| 59 | this->getData(reinterpret_cast<char*>(data->data()), bufdata.size , bufdata.position); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | //--------------------------------------------------------------- |
---|
| 63 | |
---|
| 64 | template <class T> |
---|
| 65 | void CBuffer::setData(const T & data, StdSize position) |
---|
| 66 | { |
---|
| 67 | if (this->size < (position + this->getRequestedSize(data))) |
---|
| 68 | ERROR("CBuffer::setData<T>(data, size, position)", |
---|
| 69 | << " Buffer size < size + position !"); |
---|
| 70 | CBufferData bufdata; |
---|
| 71 | bufdata.type = this->getBufferDataType<T>(); |
---|
| 72 | bufdata.isArray = false; |
---|
| 73 | bufdata.size = sizeof(T); |
---|
| 74 | bufdata.position = position + DATA_HEADER_SIZE; |
---|
| 75 | this->setBufferData(bufdata, position); |
---|
| 76 | this->setData(reinterpret_cast<const char*>(&data), bufdata.size , bufdata.position); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | template <class T> |
---|
| 80 | void CBuffer::setDataArray(const ARRAY(T, 1) data, StdSize position) |
---|
| 81 | { |
---|
| 82 | if (this->size < (position + this->getRequestedSize(data))) |
---|
| 83 | ERROR("CBuffer::setDataArray<T>(data, size, position)", |
---|
| 84 | << " Buffer size < size + position !"); |
---|
| 85 | CBufferData bufdata; |
---|
| 86 | bufdata.type = this->getBufferDataType<T>(); |
---|
| 87 | bufdata.isArray = true; |
---|
| 88 | bufdata.size = data->num_elements() * sizeof (T); |
---|
| 89 | bufdata.position = position + DATA_HEADER_SIZE; |
---|
| 90 | |
---|
| 91 | this->setBufferData(bufdata, position); |
---|
| 92 | this->setData(reinterpret_cast<const char*>(data->data()), |
---|
| 93 | bufdata.size , bufdata.position); |
---|
| 94 | } |
---|
| 95 | ///---------------------------------------------------------------- |
---|
| 96 | |
---|
| 97 | } // namespace comm |
---|
| 98 | } // namespace xmlioserver |
---|
| 99 | |
---|
| 100 | #endif // __XMLIO_CBuffer_impl__ |
---|