00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _RECORD_H
00023 #define _RECORD_H
00024
00025 #ifdef HAVE_CONFIG_H
00026 # include <config.h>
00027 #endif
00028
00029 #ifdef HAVE_INTTYPES_H
00030 # include <inttypes.h>
00031 #endif
00032
00033 #ifdef HAVE_STDLIB_H
00034 # include <stdlib.h>
00035 #endif
00036
00037 #ifdef HAVE_STRING_H
00038 # include <string.h>
00039 #endif
00040
00041 #include "bdbuffer.h"
00042 #include "yapetexception.h"
00043
00044 namespace YAPET {
00045
00057 template<class T>
00058 class Record {
00059 private:
00066 uint32_t _size;
00072 T* data;
00073
00080 void alloc_mem() throw(YAPETException) {
00081 data = (T*) malloc(sizeof(T));
00082 if (data == NULL)
00083 throw YAPETException("Memory exhausted");
00084
00085 _size = sizeof(T);
00086 }
00087
00093 void free_mem() {
00094 memset(data, 0, _size);
00095 free(data);
00096 }
00097
00098 public:
00108 Record<T>(const T& d) throw(YAPETException) {
00109 alloc_mem();
00110 memcpy(data, &d, sizeof(T));
00111 }
00112
00119 Record<T>() throw (YAPETException){
00120 alloc_mem();
00121 }
00122
00123 Record<T>(const Record<T>& r) throw (YAPETException) {
00124 alloc_mem();
00125 memcpy(data, r.data, _size);
00126 }
00127
00128 virtual ~Record<T>() {
00129 free_mem();
00130 }
00131
00137 uint32_t size() const { return _size; }
00138
00146 T* getData() { return data; }
00154 const T* getData() const { return data; }
00155
00163 operator T*() { return data; }
00171 operator const T*() const { return data; }
00180 operator uint8_t*() { return (uint8_t*)data; }
00189 operator const uint8_t*() const { return (const uint8_t*)data; }
00190
00200 const Record<T>& operator=(const Record<T>& r)
00201 throw(YAPETException) {
00202 if (this == &r) return *this;
00203
00204 free_mem();
00205
00206
00207 alloc_mem();
00208 memcpy(data, r.data, r._size);
00209
00210 return *this;
00211 }
00212
00222 const Record<T>& operator=(const T& r) throw(YAPETException) {
00223 free_mem();
00224
00225 alloc_mem();
00226 memcpy(data, &r, _size);
00227
00228 return *this;
00229 }
00230
00240 const Record<T>& operator=(const T* r) throw(YAPETException){
00241 free_mem();
00242
00243 alloc_mem();
00244 memcpy(data, r, _size);
00245
00246 return *this;
00247 }
00248
00262 const Record<T>& operator=(const BDBuffer& bdb)
00263 throw(YAPETException) {
00264 if (bdb.size() < _size)
00265 throw YAPETException("BDBuffer too small");
00266
00267 free_mem();
00268
00269 alloc_mem();
00270
00271 memcpy(data, bdb(), _size);
00272
00273 return *this;
00274 }
00275 };
00276 }
00277
00278 #endif // _RECORD_H