source: XMLIO_V2/dev/dev_rv/src/xmlio/linear_buffer.cpp @ 172

Last change on this file since 172 was 152, checked in by hozdoba, 13 years ago
File size: 9.5 KB
Line 
1#include "linear_buffer.hpp"
2
3#include "linear_buffer_impl.hpp"
4
5namespace xmlioserver
6{
7   namespace comm
8   {
9      /// ////////////////////// Définitions ////////////////////// ///
10      CLinearBuffer::CLinearBuffer(char * data, StdSize size)
11         : SuperClass(data, size), p_write(size)
12      { this->computeBufferData(); }
13
14      CLinearBuffer::CLinearBuffer(StdSize size)
15         : SuperClass(size), p_write(0)
16      { /* Ne rien faire de plus */ }
17
18      CLinearBuffer::~CLinearBuffer(void)
19      { /* Ne rien faire de plus */ }
20
21      ///--------------------------------------------------------------
22
23      long int * CLinearBuffer::NULL_ARG = NULL;
24
25      //---------------------------------------------------------------
26
27      void CLinearBuffer::clear(void)
28      { this->bdata.clear(); this->p_write = 0; }
29
30      StdSize CLinearBuffer::getUsedSize(void) const
31      { return (this->p_write); }
32
33      StdSize CLinearBuffer::getUnusedSize(void) const
34      { return (SuperClass::getSize() - this->getUsedSize()); }
35
36      //---------------------------------------------------------------
37
38#define CLinearBufferGetter(type, Type) \
39   type CLinearBuffer::get##Type(StdSize position) const     \
40   {                                                         \
41      if (position >= bdata.size())                          \
42         ERROR("CLinearBuffer::get"#Type"(position)",        \
43               << " invalid position !");                    \
44      return (SuperClass::get##Type(this->bdata[position])); \
45   }
46
47
48      CLinearBufferGetter(char     , Char)
49      CLinearBufferGetter(bool     , Bool)
50      CLinearBufferGetter(float    , Float)
51      CLinearBufferGetter(double   , Double)
52      CLinearBufferGetter(long int , Int)
53
54#undef CLinearBufferGetter
55
56      //---------------------------------------------------------------
57
58#define CLinearBufferArrayGetter(type, Type) \
59   ARRAY(type, 1) CLinearBuffer::get##Type##Array(StdSize position) const \
60   {                                                                      \
61      if (position >= bdata.size())                                       \
62         ERROR("CLinearBuffer::get"#Type"Array(position)",                \
63               << " invalid position !");                                 \
64      return (SuperClass::get##Type##Array(this->bdata[position]));       \
65   }
66
67      CLinearBufferArrayGetter(char     , Char)
68      CLinearBufferArrayGetter(bool     , Bool)
69      CLinearBufferArrayGetter(float    , Float)
70      CLinearBufferArrayGetter(double   , Double)
71      CLinearBufferArrayGetter(long int , Int)
72
73#undef CLinearBufferArrayGetter
74
75      StdString CLinearBuffer::getString(StdSize position) const
76      { return (SuperClass::getString(this->bdata[position])); }
77
78      //---------------------------------------------------------------
79
80#define CLinearBufferSetter(type, Type)                        \
81      void  CLinearBuffer::append##Type(type value)            \
82      {                                                        \
83         this->bdata.push_back(this->p_write);                 \
84         SuperClass::set##Type(value, this->p_write);          \
85         this->p_write += SuperClass::getRequestedSize(value); \
86      }
87
88      CLinearBufferSetter(char     , Char)
89      CLinearBufferSetter(bool     , Bool)
90      CLinearBufferSetter(float    , Float)
91      CLinearBufferSetter(double   , Double)
92      CLinearBufferSetter(long int , Int)
93
94#undef CLinearBufferSetter
95
96      //---------------------------------------------------------------
97
98#define CLinearBufferArraySetter(type, Type) \
99      void  CLinearBuffer::append##Type##Array(ARRAY(type, 1) value) \
100      {                                                              \
101         this->bdata.push_back(this->p_write);                       \
102         SuperClass::set##Type##Array(value, this->p_write);         \
103         this->p_write += SuperClass::getRequestedSize(value);       \
104      }
105
106      CLinearBufferArraySetter(char     , Char)
107      CLinearBufferArraySetter(bool     , Bool)
108      CLinearBufferArraySetter(float    , Float)
109      CLinearBufferArraySetter(double   , Double)
110      CLinearBufferArraySetter(long int , Int)
111
112#undef CLinearBufferArrayAppend
113
114      void CLinearBuffer::appendString(const StdString & value)
115      {
116         ARRAY_CREATE(arr, char, 1, [value.size()]);
117         std::copy(value.data(), &(value.data()[value.size()]), arr->data());
118         this->appendCharArray(arr);
119      }
120
121      //---------------------------------------------------------------
122
123      StdSize CLinearBuffer::getNumberOfStoredData(void) const
124      { return (this->bdata.size()); }
125
126      std::vector<StdSize> CLinearBuffer::getPositionsOfStoredData(void) const
127      {
128         std::vector<StdSize> retvalue;
129         std::vector<StdSize>::const_iterator it = this->bdata.begin(), end = this->bdata.end();
130         for (;it != end; it++)
131         {
132            StdSize pos = *it;
133            CBufferData bufdata;
134            SuperClass::getBufferData(bufdata, pos);
135            retvalue.push_back(bufdata.position);
136         }
137
138         return (retvalue);
139      }
140
141      std::vector<StdSize> CLinearBuffer::getSizesOfStoredData(void) const
142      {
143         std::vector<StdSize> retvalue;
144         std::vector<StdSize>::const_iterator it = this->bdata.begin(), end = this->bdata.end();
145         for (;it != end; it++)
146         {
147            StdSize pos = *it;
148            CBufferData bufdata;
149            SuperClass::getBufferData(bufdata, pos);
150            retvalue.push_back(bufdata.size);
151         }
152         return (retvalue);
153      }
154
155      std::vector<std::pair<CBuffer::CBufferDataType, bool> >
156         CLinearBuffer::getTypesOfStoredData(void) const
157      {
158         std::vector<std::pair<CBufferDataType, bool> > retvalue;
159         std::vector<StdSize>::const_iterator it = this->bdata.begin(), end = this->bdata.end();
160         for (;it != end; it++)
161         {
162            StdSize pos = *it;
163            CBufferData bufdata;
164            SuperClass::getBufferData(bufdata, pos);
165            retvalue.push_back(std::make_pair(bufdata.type, bufdata.isArray));
166         }
167
168         return (retvalue);
169      }
170
171      //---------------------------------------------------------------
172
173      void CLinearBuffer::computeBufferData(void)
174      {
175
176         CBufferData bufdata; this->clear();
177         StdSize total_size = SuperClass::getSize();
178         int i = 0;
179         while ((1000 >= i++) && (p_write != total_size))
180         {
181            bdata.push_back(this->p_write);
182            SuperClass::getBufferData(bufdata, this->p_write);
183            this->p_write = (bufdata.size + bufdata.position);
184         }
185      }
186
187      //---------------------------------------------------------------
188
189      void CLinearBuffer::appendRequestInfos(const long int & managerId,
190                                             const long int & methodId,
191                                             const long int & nbargs)
192      {
193         this->appendInt(managerId);
194         this->appendInt(methodId);
195         this->appendInt(nbargs);
196      }
197
198      void CLinearBuffer::getRequestInfos(StdSize position,
199                           long int & managerId, long int & methodId, long int & nbargs)
200      {
201         managerId = this->getInt(position);
202         methodId  = this->getInt(position+1);
203         nbargs    = this->getInt(position+2);
204      }
205
206
207      //---------------------------------------------------------------
208
209      void CLinearBuffer::printToTextFile (const StdString & filename)
210      {
211         StdOFStream ofs(filename.c_str());
212         this->printToTextStream(ofs);
213         ofs.close();
214      }
215
216      void CLinearBuffer::printToTextStream (StdOStream & ostr)
217      { // A améliorer ....
218         StdSize position = 0;
219         typedef std::pair<CBufferDataType, bool> pairBufBool;
220         std::vector<pairBufBool> vect = this->getTypesOfStoredData();
221         ostr << "|";
222         
223         std::vector<pairBufBool>::const_iterator it = vect.begin(), end = vect.end();
224         for (;it != end; it++)
225         {
226            pairBufBool elm = *it;
227            ostr << "|";
228            if (!elm.second)
229            {
230               switch (elm.first)
231               {
232                  case (TBOOL8):    ostr << this->getBool(position);
233                     break;
234                  case (TINT32):    ostr << this->getInt(position);
235                     break;
236                  case (TCHAR8):    ostr << this->getChar(position);
237                     break;
238                  case (TFLOAT32):  ostr << this->getFloat(position);
239                     break;
240                  case (TDOUBLE64): ostr << this->getDouble(position);
241                     break;
242                  default :
243                     return;
244               }
245            }
246            else
247            {
248               switch (elm.first)
249               {
250                  case (TBOOL8):    ostr << this->getBoolArray(position);
251                     break;
252                  case (TINT32):    ostr << this->getIntArray(position);
253                     break;
254                  case (TCHAR8):    ostr << this->getCharArray(position);
255                     break;
256                  case (TFLOAT32):  ostr << this->getFloatArray(position);
257                     break;
258                  case (TDOUBLE64): ostr << this->getDoubleArray(position);
259                     break;
260                  default :
261                     return;
262               }
263            }
264            position++;
265         }
266      }
267
268      ///--------------------------------------------------------------
269
270   } // namespace tree
271} // namespace xmlioserver
272
Note: See TracBrowser for help on using the repository browser.