source: XMLIO_V2/dev/dev_rv/src/xmlio/buffer.cpp @ 196

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