source: XMLIO_V2/dev/dev_rv/src/buffer.cpp @ 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: 7.5 KB
Line 
1#include "buffer.hpp"
2
3#include "manager/mpi_manager.hpp"
4#include "buffer_impl.hpp"
5
6namespace xmlioserver
7{
8   namespace comm
9   {
10      /// ////////////////////// Définitions ////////////////////// ///
11
12      CBuffer::CBuffer(StdSize size)
13         : size(size), delIdata(true)
14      {
15         this->idata = new char[size]();
16         //CMPIManager::AllocMem(&idata, size);
17      }
18
19      CBuffer::CBuffer(char * data, StdSize size)
20         : size(size), delIdata(false)
21      {
22         this->idata = data;
23      }
24
25      CBuffer::~CBuffer(void)
26      {
27         if (delIdata) delete [] this->idata;
28         //CMPIManager::FreeMem(idata);
29      }
30
31      ///--------------------------------------------------------------
32
33      CBuffer::operator char * (void)
34      { return (this->getData()); }
35
36      char * CBuffer::operator[](StdSize position)
37      { return (this->getData(position)); }
38
39      //---------------------------------------------------------------
40
41      StdSize CBuffer::getSize(StdSize position) const
42      {
43         if (position > this->size)
44            ERROR("CBuffer::getSize(position)",
45                  << " Buffer size <  size + position !");
46         return (this->size - position);
47      }
48
49      char * CBuffer::getData(StdSize position) const
50      {
51         if (position > this->size)
52            ERROR("CBuffer::getData(position)",
53                   << " Buffer size < position !");
54         return &(this->idata[position]);
55      }
56
57      //---------------------------------------------------------------
58
59      void CBuffer::getData(char * data, StdSize size, StdSize position) const
60      {
61         if (this->size < (size + position))
62            ERROR("CBuffer::getData(data, size, position)",
63                   << " Buffer size <  size + position !");
64         std::copy(this->getData(position), this->getData(position + size), data);
65      }
66
67      void CBuffer::setData(const char * data, StdSize size, StdSize position)
68      {
69         if (this->size < (size + position))
70            ERROR("CBuffer::getData(data, size, position)",
71                   << " Buffer size <  size + position !");
72         std::copy(&(data[0]), &(data[size]), this->getData(position));
73      }
74
75      //---------------------------------------------------------------
76
77#define BufferDataTypeGetter(type, type_enum)\
78   template <> CBuffer::CBufferDataType \
79      CBuffer::getBufferDataType<type>(void) const { return (type_enum); }; \
80   template <> CBuffer::CBufferDataType \
81      CBuffer::getBufferDataType<ARRAY(type, 1)>(void) const { return (type_enum); };
82
83      BufferDataTypeGetter(bool, TBOOL8);
84      BufferDataTypeGetter(char, TCHAR8);
85      BufferDataTypeGetter(float, TFLOAT32);
86      BufferDataTypeGetter(double, TDOUBLE64);
87      BufferDataTypeGetter(long int, TINT32);
88
89#undef BufferDataTypeGetter
90
91
92      //---------------------------------------------------------------
93
94      void CBuffer::getBufferData(CBufferData & bufdata, StdSize position) const
95      {
96         StdSize currposition = position;
97         this->getData( reinterpret_cast<char*>(&(bufdata.type))
98                      , sizeof(CBufferDataType), currposition);
99         this->getData( reinterpret_cast<char*>(&(bufdata.isArray))
100                      , sizeof(bool), currposition += sizeof(CBufferDataType));
101         this->getData( reinterpret_cast<char*>(&(bufdata.size))
102                      , sizeof(StdSize), currposition += sizeof(bool));
103         this->getData( reinterpret_cast<char*>(&(bufdata.position))
104                      , sizeof(StdSize), currposition += sizeof(StdSize));
105      }
106
107      void CBuffer::setBufferData(const CBufferData & bufdata, StdSize position)
108      {
109         StdSize currposition = position;
110         this->setData( reinterpret_cast<const char*>(&(bufdata.type))
111                      , sizeof(CBufferDataType), currposition);
112         this->setData( reinterpret_cast<const char*>(&(bufdata.isArray))
113                      , sizeof(bool), currposition += sizeof(CBufferDataType));
114         this->setData( reinterpret_cast<const char*>(&(bufdata.size))
115                      , sizeof(StdSize), currposition += sizeof(bool));
116         this->setData( reinterpret_cast<const char*>(&(bufdata.position))
117                      , sizeof(StdSize), currposition += sizeof(StdSize));
118      }
119
120      //---------------------------------------------------------------
121
122#define CBufferGetter(type, Type)       \
123   type CBuffer::get##Type(StdSize position) const \
124   { type retvalue; this->getData<type>(retvalue, position); return (retvalue); }
125
126
127      CBufferGetter(char     , Char)
128      CBufferGetter(bool     , Bool)
129      CBufferGetter(float    , Float)
130      CBufferGetter(double   , Double)
131      CBufferGetter(long int , Int)
132
133#undef CBufferGetter
134
135      //---------------------------------------------------------------
136
137#define CBufferArrayGetter(type, Type) \
138   ARRAY(type, 1) CBuffer::get##Type##Array(StdSize position) const \
139   {  ARRAY_CREATE(retvalue, type, 1, [1]);                         \
140      this->getDataArray<type>(retvalue, position);                 \
141      return (retvalue); }
142
143      CBufferArrayGetter(char     , Char)
144      CBufferArrayGetter(bool     , Bool)
145      CBufferArrayGetter(float    , Float)
146      CBufferArrayGetter(double   , Double)
147      CBufferArrayGetter(long int , Int)
148
149#undef CBufferArrayGetter
150
151      StdString CBuffer::getString(StdSize position) const
152      {
153         ARRAY(char, 1) array = this->getCharArray(position);
154         return (StdString(array->data(), array->num_elements()));
155      }
156
157      //---------------------------------------------------------------
158
159#define CBufferSetter(type, Type) \
160      void  CBuffer::set##Type(type value, StdSize position) \
161      { this->setData(value, position); }
162
163      CBufferSetter(char     , Char)
164      CBufferSetter(bool     , Bool)
165      CBufferSetter(float    , Float)
166      CBufferSetter(double   , Double)
167      CBufferSetter(long int , Int)
168
169#undef CBufferSetter
170
171      //---------------------------------------------------------------
172
173#define CBufferArraySetter(type, Type) \
174      void  CBuffer::set##Type##Array(ARRAY(type, 1) value, StdSize position) \
175      { this->setDataArray(value, position); }
176
177      CBufferArraySetter(char     , Char)
178      CBufferArraySetter(bool     , Bool)
179      CBufferArraySetter(float    , Float)
180      CBufferArraySetter(double   , Double)
181      CBufferArraySetter(long int , Int)
182
183#undef CBufferArraySetter
184
185      void CBuffer::setString(const StdString & value, StdSize position)
186      {
187         ARRAY_CREATE(arr, char, 1, [value.size()]);
188         std::copy(value.data(), &(value.data()[value.size()]), arr->data());
189         this->setCharArray(arr, position);
190      }
191
192      //---------------------------------------------------------------
193
194      void CBuffer::printToBinaryFile (const StdString & filename)
195      {
196         StdOFStream ofs(filename.c_str());
197         this->printToBinaryStream(ofs);
198         ofs.close();
199      }
200
201      void CBuffer::printToBinaryStream (StdOStream & ostr)
202      {  ostr.write (this->getData(), this->getSize()); }
203
204
205      StdSize CBuffer::getNextDataPosition(StdSize position) const
206      {
207         CBufferData  bufdata;
208         this->getBufferData(bufdata, position);
209         return (bufdata.size + bufdata.position);
210      }
211
212      //---------------------------------------------------------------
213
214      template <>
215         StdSize CBuffer::getRequestedSize(StdString data) const
216      { return (DATA_HEADER_SIZE + data.size() * sizeof (char)); }
217
218      ///--------------------------------------------------------------
219
220   } // namespace tree
221} // namespace xmlioserver
Note: See TracBrowser for help on using the repository browser.