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