close Warning: Can't use blame annotator:
No changeset 2259 in the repository

source: source/ariba/utility/serialization/Data.hpp@ 12760

Last change on this file since 12760 was 10653, checked in by Michael Tänzer, 12 years ago

Merge the ASIO branch back into trunk

File size: 11.4 KB
RevLine 
1// [License]
2// The Ariba-Underlay Copyright
3//
4// Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
5//
6// Institute of Telematics
7// UniversitÀt Karlsruhe (TH)
8// Zirkel 2, 76128 Karlsruhe
9// Germany
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
22// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ARIBA PROJECT OR
25// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33// The views and conclusions contained in the software and documentation
34// are those of the authors and should not be interpreted as representing
35// official policies, either expressed or implied, of the Institute of
36// Telematics.
37// [License]
38
39#ifndef DATA_HPP_
40#define DATA_HPP_
41
42//== library includes ==
43#include <string.h>
44#include <stdlib.h>
45#include <iostream>
46#include <boost/cstdint.hpp>
47#include <boost/type_traits/make_unsigned.hpp>
48
49// forward declaration
50template<typename T> class DefaultDataModel;
51template<typename T = uint8_t, typename DataModel = DefaultDataModel<uint8_t> > class DataTpl;
52typedef DataTpl<> Data;
53template<typename T, typename DataModel> std::ostream& operator<<(std::ostream& stream, const DataTpl<T, DataModel>& data);
54
55//== internal includes ==
56#include "../internal/Utilities.hpp"
57
58//== local includes ==
59#include "DataUtilities.hpp"
60#include "Serialization.hpp"
61
62/**
63 * This class implements a wrapper for binary data. And this time, when the
64 * word binary is used -- it really means binary! Length and manipulation
65 * are specified and operate in bit precision. Most of the operations are
66 * highly effective using inline functions and compiler optimizations.
67 *
68 * Two versions of binaries are supported: static, non-resizeable binaries
69 * and dynamic, resizeable ones. Please note, this class does not manage
70 * memory, reference counting etc. you have to take care about memory
71 * management yourself! However you can use the <code>release()</code>
72 * to delete the underlaying array.
73 *
74 * @author Sebastian Mies
75 */
76template<typename T, typename DataModel>
77class DataTpl: public Serializeable { SERIALIZEABLE
78protected:
79 typedef DataTpl<T,DataModel> _Data;
80 DataModel model;
81
82public:
83 static const size_t word_width = sizeof(T) * 8;
84
85 class DataManipulator {
86 private:
87 DataModel bits;
88 size_t index;
89
90 public:
91 finline DataManipulator( DataModel& _bits, size_t _index) :
92 bits(_bits), index(_index) {
93 }
94
95 template<typename X>
96 finline operator X() const {
97 return bitget<X>(bits.buffer(), index);
98 }
99
100 template<typename X>
101 finline DataManipulator& operator=(X value) {
102 bitset(value, bits.buffer(), index );
103 return *this;
104 }
105
106 template<typename X>
107 finline void set(X value, size_t length = sizeof(X) * 8, if_uint(X)) {
108 bitcpy(value, 0, bits.buffer(), index, length);
109 }
110
111 template<typename X>
112 finline void set(X value, size_t length = sizeof(X) * 8, if_int(X)) {
113 set(_unsigned(value),length);
114 }
115
116 template<typename X>
117 finline void get(X& value, size_t length = sizeof(X) * 8, if_uint(X)) const {
118 value = bitget<X> (bits.buffer(), index, length);
119 }
120
121 template<typename X>
122 finline void get(X& value, size_t length = sizeof(X) * 8, if_int(X)) const {
123 typedef typename boost::make_unsigned<X>::type unsigned_type;
124 _unsigned(value) = bitget<unsigned_type> (bits.buffer(), index, length);
125 }
126
127 finline void get(bool& value) const {
128 value = bitget( bits.buffer(), index );
129 }
130 };
131
132public:
133 static const Data UNSPECIFIED;
134
135 /**
136 * Constructs a dynamic bit-data object of variable size.
137 */
138 finline DataTpl() : model() {
139 }
140
141 /**
142 * Contructs a copy of an existing data buffer
143 */
144 finline DataTpl(const DataTpl<T, DataModel>& copy) {
145 this->model = copy.model;
146 }
147
148 finline DataTpl( const DataModel& _model ) : model( _model ) {
149 }
150
151 /**
152 * Constructs a static bit-data object with the given buffer.
153 */
154 finline DataTpl(const T* buffer, size_t length) {
155 model.buffer() = buffer;
156 model.length() = length;
157 }
158
159 /**
160 * Constructs a dynamic bit-data object with the given buffer.
161 */
162 finline DataTpl( T* buffer, size_t length ) {
163 model.buffer() = buffer;
164 model.length() = length;
165 }
166
167 /**
168 * Constructs a dynamic bit-data object of variable size with the given
169 * initial size.
170 */
171 finline DataTpl(size_t length) : model() {
172 model.resize(length);
173 }
174
175
176 /**
177 * Returns the internal buffer of the data.
178 */
179 finline T* getBuffer() const {
180 return model.buffer();
181 }
182
183 /**
184 * Returns the length of the data in bits
185 */
186 finline size_t getLength() const {
187 return model.length();
188 }
189 /**
190 * Sets the length of the data in bits
191 */
192 void setLength(size_t new_length) {
193 model.resize(new_length);
194 }
195
196 /**
197 * Ensures that the buffer pointer has the given
198 * number of bits of memory reserved.
199 *
200 * @param neededLength The minimum data length required.
201 */
202 finline void ensureLength( size_t neededLength ) {
203 if ((int) neededLength > model.length() ) model.resize(neededLength);
204 }
205
206 /**
207 * Returns a manipulator object for a certain bit position.
208 *
209 * @param index The index in bits quantitites.
210 * @return A data manipulation object
211 */
212 finline DataManipulator operator[](size_t index) {
213 return DataManipulator(model, index);
214 }
215
216 /**
217 * Returns a constant manipulator object for a certain bit position.
218 *
219 * @param index The index in bits quantitites.
220 * @return A constant data manipulation object
221 */
222 finline const DataManipulator operator[](size_t index) const {
223 return DataManipulator(model, index);
224 }
225
226 _Data sub( size_t index, size_t length = ~0 ) {
227 if (length == ~0) length = model.length()-index;
228 return _Data(model.sub(index,length));
229 }
230
231 /**
232 * Returns a copy of the specified bit range.
233 *
234 * @param index The first bit to copy
235 * @param length The length of the bit range
236 * @return The cloned data object
237 */
238 _Data clone( size_t index, size_t length ) const {
239 DataModel new_model = model.clone(index,length);
240 return _Data(new_model);
241 }
242
243 /**
244 * Returns a copy of the data.
245 *
246 * @return A cloned data object
247 */
248 _Data clone() const {
249 DataModel new_model = model.clone( 0, model.length() );
250 return _Data(new_model);
251 }
252
253 /**
254 * Returns true, if the data is empty (zero length)
255 *
256 * @return True, if the data is empty (zero length)
257 */
258 finline bool isEmpty() const {
259 return (model.length() == 0);
260 }
261
262 /**
263 * Returns true, if the data buffer is unspecified.
264 * In this case no memory is associated with this data object.
265 *
266 * @return True, if the data buffer is unspecified.
267 */
268 finline bool isUnspecified() const {
269 return model.isUnspecified();
270 }
271
272 /**
273 * This method frees the memory associated with
274 * this data object and sets this object into an unspecified
275 * state.
276 */
277 finline void release() {
278 model.release();
279 }
280
281 // operators
282
283 /**
284 * Assigns another buffer
285 */
286 template<typename X>
287 _Data& operator= (const DataTpl<X>& source) {
288 this->model = source.model;
289 return *this;
290 }
291
292 /**
293 * Returns true, if the datas buffer's pointer and length
294 * are the same.
295 */
296 template<typename X>
297 finline bool operator==( DataTpl<X>& data) {
298 return (data.model.buffer() == model.buffer() &&
299 data.model.length() == model.length() );
300 }
301
302 finline _Data& operator&=(_Data& data) {
303 return *this;
304 }
305
306 finline _Data& operator|=(_Data& data) {
307 return *this;
308 }
309
310 finline _Data& operator^=(_Data& data) {
311 return *this;
312 }
313
314 /* implied operators */
315
316 finline _Data operator&(_Data& data) {
317 return (this->clone() &= data);
318 }
319
320 finline _Data operator|(_Data& data) {
321 return (this->clone() |= data);
322 }
323
324 finline _Data operator^(_Data& data) {
325 return (this->clone() ^= data);
326 }
327};
328
329/* unspecified type */
330template<typename T, typename DataModel>
331const Data DataTpl<T, DataModel>::UNSPECIFIED;
332
333/**
334 * This class implements the default data model
335 *
336 * @author Sebastian Mies
337 */
338template<typename _T>
339class DefaultDataModel {
340public:
341 typedef _T T;
342 typedef DefaultDataModel<T> _Model;
343
344private:
345
346 int32_t bufferLen;
347 T* bufferPtr;
348
349 static finline int calcLength(int length) {
350 if (length<0) return 0;
351 return ((length/8)/sizeof(T)+1);
352 }
353public:
354 finline DefaultDataModel() {
355 bufferPtr = NULL;
356 bufferLen = -1;
357 }
358
359 finline DefaultDataModel( void* buffer, size_t length ) {
360 bufferPtr = (T*)buffer;
361 bufferLen = length;
362 }
363
364 finline DefaultDataModel( const _Model& source ) {
365 this->bufferPtr = source.bufferPtr;
366 this->bufferLen = source.bufferLen;
367 }
368
369 finline _Model& operator=( const _Model& source ) {
370 this->bufferPtr = source.bufferPtr;
371 this->bufferLen = source.bufferLen;
372 return *this;
373 }
374
375 finline T*& buffer() {
376 return bufferPtr;
377 }
378
379 finline T* buffer() const {
380 return bufferPtr;
381 }
382
383 finline int32_t& length() {
384 return bufferLen;
385 }
386
387 finline int32_t length() const {
388 return (bufferLen == -1) ? 0 : bufferLen;
389 }
390
391 finline bool isUnspecified() const {
392 return bufferLen < 0;
393 }
394
395 finline void resize( size_t new_length ) {
396 size_t old_length = calcLength(bufferLen);
397 size_t res_length = calcLength(new_length);
398 if (old_length != res_length) {
399
400 if(res_length <= 0){
401 if (bufferPtr != NULL) delete [] bufferPtr;
402 bufferPtr = NULL;
403 bufferLen = 0;
404 }else{
405 T* new_buffer = new T[res_length];
406 if (new_buffer != NULL) memset(new_buffer, 0, res_length*sizeof(T));
407 if (bufferPtr != NULL) {
408 size_t clength = res_length < old_length ? res_length : old_length;
409 memcpy( new_buffer, bufferPtr, clength*sizeof(T) );
410 delete [] bufferPtr;
411 }
412 bufferPtr = new_buffer;
413 bufferLen = new_length;
414 }
415 }
416 }
417
418 finline void release() {
419 if (bufferPtr!=NULL && bufferLen>=0) delete [] bufferPtr;
420 bufferPtr = NULL;
421 bufferLen = -1;
422 }
423
424 finline _Model sub( size_t index, size_t length ) {
425 return _Model( bufferPtr + index/sizeof(T), length );
426 }
427
428 finline _Model clone( size_t index, size_t length ) const {
429 _Model new_model;
430 new_model.resize( length );
431 bitcpy( this->buffer(), index, new_model.buffer(), 0, length );
432 return new_model;
433 }
434};
435
436/* serialization */
437sznBeginDefault( Data, X ){
438 for (size_t i = 0; i< getLength() / word_width; i++) X && getBuffer()[i];
439}sznEnd();
440
441/* default human readable text output */
442template<typename T, typename DataModel>
443std::ostream& operator<<(std::ostream& stream, const DataTpl<T, DataModel>& data) {
444 stream << "[" << bitstr(data.getBuffer(), data.getLength(), 4)
445 << "|'";
446 const char* buffer = (const char*) data.getBuffer();
447 for (size_t i = 0; i < data.getLength() / 8; i++) {
448 char c = buffer[i] < 32 ? '.' : buffer[i];
449 stream << c;
450 }
451 stream << "']";
452 return stream;
453}
454#endif /* DATA_HPP_ */
Note: See TracBrowser for help on using the repository browser.