source: XMLIO_V2/dev/dev_rv/src/buffer_impl.hpp @ 141

Last change on this file since 141 was 141, checked in by hozdoba, 13 years ago

Mise à jour depuis un autre dépôt

File size: 3.9 KB
Line 
1#ifndef __XMLIO_CBuffer_impl__
2#define __XMLIO_CBuffer_impl__
3
4namespace 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<T>(data, size, 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)", << " invalid type !");
35         if (bufdata.isArray != false)
36            ERROR("CBuffer::getData(data, position)",
37                   << " type should be an array !");
38
39         this->getData(reinterpret_cast<char*>(&data), bufdata.size , bufdata.position);
40      }
41
42      template <class T>
43         void CBuffer::getDataArray(ARRAY(T, 1) data, StdSize position) const
44      {
45         CBufferData bufdata;
46         this->getBufferData(bufdata, position);
47         if (bufdata.type != this->getBufferDataType<T>())
48            ERROR("CBuffer::getDataArray(data, position)", << " invalid type !");
49         if (bufdata.isArray != true)
50            ERROR("CBuffer::getDataArray(data, position)",
51                  << " type should not be an array !");
52         if (this->size < (position + (DATA_HEADER_SIZE + bufdata.size)))
53            ERROR("CBuffer::getData<T>(data, size, position)",
54                   << " Buffer size <  size + position !");
55         data->resize(boost::extents[bufdata.size/sizeof(T)]);
56         this->getData(reinterpret_cast<char*>(data->data()), bufdata.size , bufdata.position);
57      }
58
59      //---------------------------------------------------------------
60
61      template <class T>
62         void CBuffer::setData(const T & data, StdSize position)
63      {
64         if (this->size < (position + this->getRequestedSize(data)))
65            ERROR("CBuffer::setData<T>(data, size, position)",
66                   << " Buffer size <  size + position !");
67         CBufferData bufdata;
68         bufdata.type     = this->getBufferDataType<T>();
69         bufdata.isArray  = false;
70         bufdata.size     = sizeof(T);
71         bufdata.position = position + DATA_HEADER_SIZE;
72         this->setBufferData(bufdata, position);
73         this->setData(reinterpret_cast<const char*>(&data), bufdata.size , bufdata.position);
74      }
75
76      template <class T>
77         void CBuffer::setDataArray(const ARRAY(T, 1) data, StdSize position)
78      {
79         if (this->size < (position + this->getRequestedSize(data)))
80            ERROR("CBuffer::setDataArray<T>(data, size, position)",
81                   << " Buffer size <  size + position !");
82         CBufferData bufdata;
83         bufdata.type     = this->getBufferDataType<T>();
84         bufdata.isArray  = true;
85         bufdata.size     = data->num_elements() * sizeof (T);
86         bufdata.position = position + DATA_HEADER_SIZE;
87
88         this->setBufferData(bufdata, position);
89         this->setData(reinterpret_cast<const char*>(data->data()),
90                       bufdata.size , bufdata.position);
91      }
92      ///----------------------------------------------------------------
93
94   } // namespace comm
95} // namespace xmlioserver
96
97#endif // __XMLIO_CBuffer_impl__
Note: See TracBrowser for help on using the repository browser.