Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _RECORD_H
00034 #define _RECORD_H
00035
00036 #ifdef HAVE_CONFIG_H
00037 # include <config.h>
00038 #endif
00039
00040 #ifdef HAVE_INTTYPES_H
00041 # include <inttypes.h>
00042 #endif
00043
00044 #ifdef HAVE_STDLIB_H
00045 # include <stdlib.h>
00046 #endif
00047
00048 #ifdef HAVE_STRING_H
00049 # include <string.h>
00050 #endif
00051
00052 #include "../intl.h"
00053
00054 #include "bdbuffer.h"
00055 #include "yapetexception.h"
00056
00057 namespace YAPET {
00058
00070 template<class T>
00071 class Record {
00072 private:
00079 uint32_t _size;
00085 T* data;
00086
00093 void alloc_mem() throw (YAPETException) {
00094 data = (T*) malloc (sizeof (T) );
00095
00096 if (data == NULL)
00097 throw YAPETException (_ ("Memory exhausted") );
00098
00099 _size = sizeof (T);
00100 }
00101
00107 void free_mem() {
00108 memset (data, 0, _size);
00109 free (data);
00110 }
00111
00112 public:
00122 Record<T> (const T& d) throw (YAPETException) {
00123 alloc_mem();
00124 memcpy (data, &d, sizeof (T) );
00125 }
00126
00133 Record<T>() throw (YAPETException) {
00134 alloc_mem();
00135 }
00136
00137 Record<T> (const Record<T>& r) throw (YAPETException) {
00138 alloc_mem();
00139 memcpy (data, r.data, _size);
00140 }
00141
00142 virtual ~Record<T>() {
00143 free_mem();
00144 }
00145
00151 uint32_t size() const {
00152 return _size;
00153 }
00154
00162 T* getData() {
00163 return data;
00164 }
00172 const T* getData() const {
00173 return data;
00174 }
00175
00183 operator T*() {
00184 return data;
00185 }
00193 operator const T*() const {
00194 return data;
00195 }
00204 operator uint8_t*() {
00205 return (uint8_t*) data;
00206 }
00215 operator const uint8_t*() const {
00216 return (const uint8_t*) data;
00217 }
00218
00228 const Record<T>& operator= (const Record<T>& r)
00229 throw (YAPETException) {
00230 if (this == &r) return *this;
00231
00232 free_mem();
00233
00234 alloc_mem();
00235 memcpy (data, r.data, r._size);
00236 return *this;
00237 }
00238
00248 const Record<T>& operator= (const T& r) throw (YAPETException) {
00249 free_mem();
00250
00251 alloc_mem();
00252 memcpy (data, &r, _size);
00253 return *this;
00254 }
00255
00265 const Record<T>& operator= (const T* r) throw (YAPETException) {
00266 free_mem();
00267
00268 alloc_mem();
00269 memcpy (data, r, _size);
00270 return *this;
00271 }
00272
00286 const Record<T>& operator= (const BDBuffer& bdb)
00287 throw (YAPETException) {
00288
00289 if (bdb.size() < _size)
00290 throw YAPETException (_ ("BDBuffer too small. Cannot assign to Record<T>"), BDBUFFER_TOO_SMALL );
00291 if (bdb.size() > _size)
00292 throw YAPETException (_ ("BDBuffer too big. Cannot assign to Record<T>"), BDBUFFER_TOO_BIG );
00293
00294 free_mem();
00295
00296 alloc_mem();
00297 memcpy (data, bdb(), _size);
00298 return *this;
00299 }
00300 };
00301 }
00302
00303 #endif // _RECORD_H