source: XMLIO_V2/dev/dev_rv/src/linear_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: 9.0 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         BOOST_FOREACH(StdSize pos, bdata)
130         {
131            CBufferData bufdata;
132            SuperClass::getBufferData(bufdata, pos);
133            retvalue.push_back(bufdata.position);
134         }
135
136         return (retvalue);
137      }
138
139      std::vector<StdSize> CLinearBuffer::getSizesOfStoredData(void) const
140      {
141         std::vector<StdSize> retvalue;
142         BOOST_FOREACH(StdSize pos, bdata)
143         {
144            CBufferData bufdata;
145            SuperClass::getBufferData(bufdata, pos);
146            retvalue.push_back(bufdata.size);
147         }
148         return (retvalue);
149      }
150
151      std::vector<std::pair<CBuffer::CBufferDataType, bool> >
152         CLinearBuffer::getTypesOfStoredData(void) const
153      {
154         std::vector<std::pair<CBufferDataType, bool> > retvalue;
155         BOOST_FOREACH(StdSize pos, bdata)
156         {
157            CBufferData bufdata;
158            SuperClass::getBufferData(bufdata, pos);
159            retvalue.push_back(std::make_pair(bufdata.type, bufdata.isArray));
160         }
161
162         return (retvalue);
163      }
164
165      //---------------------------------------------------------------
166
167      void CLinearBuffer::computeBufferData(void)
168      {
169
170         CBufferData bufdata; this->clear();
171         StdSize total_size = SuperClass::getSize();
172         int i = 0;
173         while ((1000 >= i++) && (p_write != total_size))
174         {
175            bdata.push_back(this->p_write);
176            SuperClass::getBufferData(bufdata, this->p_write);
177            this->p_write = (bufdata.size + bufdata.position);
178         }
179      }
180
181      //---------------------------------------------------------------
182
183      void CLinearBuffer::appendRequestInfos(const long int & managerId,
184                                             const long int & methodId,
185                                             const long int & nbargs)
186      {
187         this->appendInt(managerId);
188         this->appendInt(methodId);
189         this->appendInt(nbargs);
190      }
191
192      void CLinearBuffer::getRequestInfos(StdSize position,
193                           long int & managerId, long int & methodId, long int & nbargs)
194      {
195         managerId = this->getInt(position);
196         methodId  = this->getInt(position+1);
197         nbargs    = this->getInt(position+2);
198      }
199
200
201      //---------------------------------------------------------------
202
203      void CLinearBuffer::printToTextFile (const StdString & filename)
204      {
205         StdOFStream ofs(filename.c_str());
206         this->printToTextStream(ofs);
207         ofs.close();
208      }
209
210      void CLinearBuffer::printToTextStream (StdOStream & ostr)
211      { // A améliorer ....
212         StdSize position = 0;
213         typedef std::pair<CBufferDataType, bool> pairBufBool;
214         std::vector< pairBufBool > vect = this->getTypesOfStoredData();
215         ostr << "|";
216         BOOST_FOREACH(pairBufBool elm, vect)
217         {
218            ostr << "|";
219            if (!elm.second)
220            {
221               switch (elm.first)
222               {
223                  case (TBOOL8):    ostr << this->getBool(position);
224                     break;
225                  case (TINT32):    ostr << this->getInt(position);
226                     break;
227                  case (TCHAR8):    ostr << this->getChar(position);
228                     break;
229                  case (TFLOAT32):  ostr << this->getFloat(position);
230                     break;
231                  case (TDOUBLE64): ostr << this->getDouble(position);
232                     break;
233                  default :
234                     return;
235               }
236            }
237            else
238            {
239               switch (elm.first)
240               {
241                  case (TBOOL8):    ostr << this->getBoolArray(position);
242                     break;
243                  case (TINT32):    ostr << this->getIntArray(position);
244                     break;
245                  case (TCHAR8):    ostr << this->getCharArray(position);
246                     break;
247                  case (TFLOAT32):  ostr << this->getFloatArray(position);
248                     break;
249                  case (TDOUBLE64): ostr << this->getDoubleArray(position);
250                     break;
251                  default :
252                     return;
253               }
254            }
255            position++;
256         }
257      }
258
259      ///--------------------------------------------------------------
260
261   } // namespace tree
262} // namespace xmlioserver
263
Note: See TracBrowser for help on using the repository browser.