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 if (data == NULL)
00096 throw YAPETException(_("Memory exhausted"));
00097
00098 _size = sizeof(T);
00099 }
00100
00106 void free_mem() {
00107 memset(data, 0, _size);
00108 free(data);
00109 }
00110
00111 public:
00121 Record<T>(const T& d) throw(YAPETException) {
00122 alloc_mem();
00123 memcpy(data, &d, sizeof(T));
00124 }
00125
00132 Record<T>() throw (YAPETException){
00133 alloc_mem();
00134 }
00135
00136 Record<T>(const Record<T>& r) throw (YAPETException) {
00137 alloc_mem();
00138 memcpy(data, r.data, _size);
00139 }
00140
00141 virtual ~Record<T>() {
00142 free_mem();
00143 }
00144
00150 uint32_t size() const { return _size; }
00151
00159 T* getData() { return data; }
00167 const T* getData() const { return data; }
00168
00176 operator T*() { return data; }
00184 operator const T*() const { return data; }
00193 operator uint8_t*() { return (uint8_t*)data; }
00202 operator const uint8_t*() const { return (const uint8_t*)data; }
00203
00213 const Record<T>& operator=(const Record<T>& r)
00214 throw(YAPETException) {
00215 if (this == &r) return *this;
00216
00217 free_mem();
00218
00219
00220 alloc_mem();
00221 memcpy(data, r.data, r._size);
00222
00223 return *this;
00224 }
00225
00235 const Record<T>& operator=(const T& r) throw(YAPETException) {
00236 free_mem();
00237
00238 alloc_mem();
00239 memcpy(data, &r, _size);
00240
00241 return *this;
00242 }
00243
00253 const Record<T>& operator=(const T* r) throw(YAPETException){
00254 free_mem();
00255
00256 alloc_mem();
00257 memcpy(data, r, _size);
00258
00259 return *this;
00260 }
00261
00275 const Record<T>& operator=(const BDBuffer& bdb)
00276 throw(YAPETException) {
00277 if (bdb.size() < _size)
00278 throw YAPETException(_("BDBuffer too small"));
00279
00280 free_mem();
00281
00282 alloc_mem();
00283
00284 memcpy(data, bdb(), _size);
00285
00286 return *this;
00287 }
00288 };
00289 }
00290
00291 #endif // _RECORD_H