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