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