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