[219] | 1 | #include "buffer.hpp" |
---|
| 2 | |
---|
| 3 | #include "mpi_manager.hpp" |
---|
| 4 | #include "buffer_impl.hpp" |
---|
| 5 | |
---|
| 6 | namespace xmlioserver |
---|
| 7 | { |
---|
| 8 | namespace comm |
---|
| 9 | { |
---|
| 10 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 11 | |
---|
| 12 | CBuffer::CBuffer(StdSize size) |
---|
| 13 | : size(size), delIdata(true) |
---|
| 14 | { |
---|
| 15 | this->idata = new char[size](); |
---|
| 16 | //CMPIManager::AllocMem(&idata, size); |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | CBuffer::CBuffer(char * data, StdSize size) |
---|
| 20 | : size(size), delIdata(false) |
---|
| 21 | { |
---|
| 22 | this->idata = data; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | CBuffer::CBuffer(const CBuffer & buffer) |
---|
| 26 | : size(buffer.size), delIdata(true) |
---|
| 27 | { |
---|
| 28 | this->idata = new char[size](); |
---|
| 29 | std::copy (buffer.idata, buffer.idata+size, this->idata); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | CBuffer::CBuffer(const CBuffer * const buffer) |
---|
| 33 | : size(buffer->size), delIdata(true) |
---|
| 34 | { |
---|
| 35 | this->idata = new char[size](); |
---|
| 36 | std::copy (buffer->idata, buffer->idata+size, this->idata); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | CBuffer::~CBuffer(void) |
---|
| 40 | { |
---|
| 41 | if (delIdata) delete [] this->idata; |
---|
| 42 | else this->fillData('\0', this->getSize(), 0); |
---|
| 43 | //CMPIManager::FreeMem(idata); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | ///-------------------------------------------------------------- |
---|
| 47 | |
---|
| 48 | CBuffer::operator char * (void) |
---|
| 49 | { return (this->getData()); } |
---|
| 50 | |
---|
| 51 | char * CBuffer::operator[](StdSize position) |
---|
| 52 | { return (this->getData(position)); } |
---|
| 53 | |
---|
| 54 | //--------------------------------------------------------------- |
---|
| 55 | |
---|
| 56 | StdSize CBuffer::getSize(StdSize position) const |
---|
| 57 | { |
---|
| 58 | if (position > this->size) |
---|
| 59 | ERROR("CBuffer::getSize(position)", |
---|
| 60 | << " Buffer size < size + position !"); |
---|
| 61 | return (this->size - position); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | char * CBuffer::getData(StdSize position) const |
---|
| 65 | { |
---|
| 66 | if (position > this->size) |
---|
| 67 | ERROR("CBuffer::getData(position)", |
---|
| 68 | << " Buffer size < position !"); |
---|
| 69 | return &(this->idata[position]); |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | //--------------------------------------------------------------- |
---|
| 73 | |
---|
| 74 | void CBuffer::getData(char * data, StdSize size, StdSize position) const |
---|
| 75 | { |
---|
| 76 | if (this->size < (size + position)) |
---|
| 77 | ERROR("CBuffer::getData(data, size, position)", |
---|
| 78 | << " Buffer size < size + position !"); |
---|
| 79 | std::copy(this->getData(position), this->getData(position + size), data); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | //--------------------------------------------------------------- |
---|
| 83 | |
---|
| 84 | void CBuffer::setData(const char * data, StdSize size, StdSize position) |
---|
| 85 | { |
---|
| 86 | if (this->size < (size + position)) |
---|
| 87 | ERROR("CBuffer::getData(data, size, position)", |
---|
| 88 | << " Buffer size < size + position !"); |
---|
| 89 | std::copy(&(data[0]), &(data[size]), this->getData(position)); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | //--------------------------------------------------------------- |
---|
| 93 | |
---|
| 94 | #define BufferDataTypeGetter(type, type_enum)\ |
---|
| 95 | template <> CBuffer::CBufferDataType \ |
---|
| 96 | CBuffer::getBufferDataType<type>(void) const { return (type_enum); }; \ |
---|
| 97 | template <> CBuffer::CBufferDataType \ |
---|
| 98 | CBuffer::getBufferDataType<ARRAY(type, 1)>(void) const { return (type_enum); }; |
---|
| 99 | |
---|
| 100 | BufferDataTypeGetter(bool, TBOOL8); |
---|
| 101 | BufferDataTypeGetter(char, TCHAR8); |
---|
| 102 | BufferDataTypeGetter(float, TFLOAT32); |
---|
| 103 | BufferDataTypeGetter(double, TDOUBLE64); |
---|
| 104 | BufferDataTypeGetter(long int, TINT32); |
---|
| 105 | |
---|
| 106 | #undef BufferDataTypeGetter |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | //--------------------------------------------------------------- |
---|
| 110 | |
---|
| 111 | void CBuffer::getBufferData(CBufferData & bufdata, StdSize position) const |
---|
| 112 | { |
---|
| 113 | StdSize currposition = position; |
---|
| 114 | this->getData( reinterpret_cast<char*>(&(bufdata.type)) |
---|
| 115 | , sizeof(CBufferDataType), currposition); |
---|
| 116 | this->getData( reinterpret_cast<char*>(&(bufdata.isArray)) |
---|
| 117 | , sizeof(bool), currposition += sizeof(CBufferDataType)); |
---|
| 118 | this->getData( reinterpret_cast<char*>(&(bufdata.size)) |
---|
| 119 | , sizeof(StdSize), currposition += sizeof(bool)); |
---|
| 120 | this->getData( reinterpret_cast<char*>(&(bufdata.position)) |
---|
| 121 | , sizeof(StdSize), currposition += sizeof(StdSize)); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void CBuffer::setBufferData(const CBufferData & bufdata, StdSize position) |
---|
| 125 | { |
---|
| 126 | StdSize currposition = position; |
---|
| 127 | this->setData( reinterpret_cast<const char*>(&(bufdata.type)) |
---|
| 128 | , sizeof(CBufferDataType), currposition); |
---|
| 129 | this->setData( reinterpret_cast<const char*>(&(bufdata.isArray)) |
---|
| 130 | , sizeof(bool), currposition += sizeof(CBufferDataType)); |
---|
| 131 | this->setData( reinterpret_cast<const char*>(&(bufdata.size)) |
---|
| 132 | , sizeof(StdSize), currposition += sizeof(bool)); |
---|
| 133 | this->setData( reinterpret_cast<const char*>(&(bufdata.position)) |
---|
| 134 | , sizeof(StdSize), currposition += sizeof(StdSize)); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | //--------------------------------------------------------------- |
---|
| 138 | |
---|
| 139 | #define CBufferGetter(type, Type) \ |
---|
| 140 | type CBuffer::get##Type(StdSize position) const \ |
---|
| 141 | { type retvalue; this->getData<type>(retvalue, position); return (retvalue); } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | CBufferGetter(char , Char) |
---|
| 145 | CBufferGetter(bool , Bool) |
---|
| 146 | CBufferGetter(float , Float) |
---|
| 147 | CBufferGetter(double , Double) |
---|
| 148 | CBufferGetter(long int , Int) |
---|
| 149 | |
---|
| 150 | #undef CBufferGetter |
---|
| 151 | |
---|
| 152 | //--------------------------------------------------------------- |
---|
| 153 | |
---|
| 154 | #define CBufferArrayGetter(type, Type) \ |
---|
| 155 | ARRAY(type, 1) CBuffer::get##Type##Array(StdSize position) const \ |
---|
| 156 | { ARRAY_CREATE(retvalue, type, 1, [1]); \ |
---|
| 157 | this->getDataArray<type>(retvalue, position); \ |
---|
| 158 | return (retvalue); } |
---|
| 159 | |
---|
| 160 | CBufferArrayGetter(char , Char) |
---|
| 161 | CBufferArrayGetter(bool , Bool) |
---|
| 162 | CBufferArrayGetter(float , Float) |
---|
| 163 | CBufferArrayGetter(double , Double) |
---|
| 164 | CBufferArrayGetter(long int , Int) |
---|
| 165 | |
---|
| 166 | #undef CBufferArrayGetter |
---|
| 167 | |
---|
| 168 | StdString CBuffer::getString(StdSize position) const |
---|
| 169 | { |
---|
| 170 | ARRAY(char, 1) array = this->getCharArray(position); |
---|
| 171 | return (StdString(array->data(), array->num_elements())); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | //--------------------------------------------------------------- |
---|
| 175 | |
---|
| 176 | #define CBufferSetter(type, Type) \ |
---|
| 177 | void CBuffer::set##Type(type value, StdSize position) \ |
---|
| 178 | { this->setData(value, position); } |
---|
| 179 | |
---|
| 180 | CBufferSetter(char , Char) |
---|
| 181 | CBufferSetter(bool , Bool) |
---|
| 182 | CBufferSetter(float , Float) |
---|
| 183 | CBufferSetter(double , Double) |
---|
| 184 | CBufferSetter(long int , Int) |
---|
| 185 | |
---|
| 186 | #undef CBufferSetter |
---|
| 187 | |
---|
| 188 | //--------------------------------------------------------------- |
---|
| 189 | |
---|
| 190 | #define CBufferArraySetter(type, Type) \ |
---|
| 191 | void CBuffer::set##Type##Array(ARRAY(type, 1) value, StdSize position) \ |
---|
| 192 | { this->setDataArray(value, position); } |
---|
| 193 | |
---|
| 194 | CBufferArraySetter(char , Char) |
---|
| 195 | CBufferArraySetter(bool , Bool) |
---|
| 196 | CBufferArraySetter(float , Float) |
---|
| 197 | CBufferArraySetter(double , Double) |
---|
| 198 | CBufferArraySetter(long int , Int) |
---|
| 199 | |
---|
| 200 | #undef CBufferArraySetter |
---|
| 201 | |
---|
| 202 | void CBuffer::setString(const StdString & value, StdSize position) |
---|
| 203 | { |
---|
| 204 | ARRAY_CREATE(arr, char, 1, [value.size()]); |
---|
| 205 | std::copy(value.data(), &(value.data()[value.size()]), arr->data()); |
---|
| 206 | this->setCharArray(arr, position); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | //--------------------------------------------------------------- |
---|
| 210 | |
---|
| 211 | void CBuffer::printToBinaryFile (const StdString & filename) |
---|
| 212 | { |
---|
| 213 | StdOFStream ofs(filename.c_str()); |
---|
| 214 | this->printToBinaryStream(ofs); |
---|
| 215 | ofs.close(); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | //--------------------------------------------------------------- |
---|
| 219 | |
---|
| 220 | void CBuffer::printToBinaryStream (StdOStream & ostr) |
---|
| 221 | { |
---|
| 222 | ostr.write (this->getData(), this->getSize()); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | //--------------------------------------------------------------- |
---|
| 226 | |
---|
| 227 | StdSize CBuffer::getNextDataPosition(StdSize position) |
---|
| 228 | { |
---|
| 229 | CBufferData bufdata; |
---|
| 230 | this->updateBufferData(position); |
---|
| 231 | this->getBufferData(bufdata, position); |
---|
| 232 | return (bufdata.size + bufdata.position); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | //--------------------------------------------------------------- |
---|
| 236 | |
---|
| 237 | template <> |
---|
| 238 | StdSize CBuffer::getRequestedSize(StdString data) const |
---|
| 239 | { |
---|
| 240 | return (DATA_HEADER_SIZE + data.size() * sizeof (char)); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | //--------------------------------------------------------------- |
---|
| 244 | |
---|
| 245 | void CBuffer::updateBufferData(StdSize position) |
---|
| 246 | { |
---|
| 247 | CBufferData bufdata; |
---|
| 248 | this->getBufferData(bufdata, position); |
---|
| 249 | bufdata.position = position + DATA_HEADER_SIZE; |
---|
| 250 | this->setBufferData(bufdata, position); |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | //--------------------------------------------------------------- |
---|
| 254 | |
---|
| 255 | void CBuffer::fillData(char data, StdSize size, StdSize position) |
---|
| 256 | { |
---|
| 257 | if (this->size < (size + position)) |
---|
| 258 | ERROR("CBuffer::getData(data, size, position)", |
---|
| 259 | << " Buffer size < size + position !"); |
---|
| 260 | std::fill_n(this->getData(position), size, data); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | ///-------------------------------------------------------------- |
---|
| 264 | |
---|
| 265 | } // namespace tree |
---|
| 266 | } // namespace xmlioserver |
---|