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 "../intl.h"
00042
00043 #include "bdbuffer.h"
00044 #include "yapetexception.h"
00045
00046 namespace YAPET {
00047
00059 template<class T>
00060 class Record {
00061 private:
00068 uint32_t _size;
00074 T* data;
00075
00082 void alloc_mem() throw(YAPETException) {
00083 data = (T*) malloc(sizeof(T));
00084 if (data == NULL)
00085 throw YAPETException(_("Memory exhausted"));
00086
00087 _size = sizeof(T);
00088 }
00089
00095 void free_mem() {
00096 memset(data, 0, _size);
00097 free(data);
00098 }
00099
00100 public:
00110 Record<T>(const T& d) throw(YAPETException) {
00111 alloc_mem();
00112 memcpy(data, &d, sizeof(T));
00113 }
00114
00121 Record<T>() throw (YAPETException){
00122 alloc_mem();
00123 }
00124
00125 Record<T>(const Record<T>& r) throw (YAPETException) {
00126 alloc_mem();
00127 memcpy(data, r.data, _size);
00128 }
00129
00130 virtual ~Record<T>() {
00131 free_mem();
00132 }
00133
00139 uint32_t size() const { return _size; }
00140
00148 T* getData() { return data; }
00156 const T* getData() const { return data; }
00157
00165 operator T*() { return data; }
00173 operator const T*() const { return data; }
00182 operator uint8_t*() { return (uint8_t*)data; }
00191 operator const uint8_t*() const { return (const uint8_t*)data; }
00192
00202 const Record<T>& operator=(const Record<T>& r)
00203 throw(YAPETException) {
00204 if (this == &r) return *this;
00205
00206 free_mem();
00207
00208
00209 alloc_mem();
00210 memcpy(data, r.data, r._size);
00211
00212 return *this;
00213 }
00214
00224 const Record<T>& operator=(const T& r) throw(YAPETException) {
00225 free_mem();
00226
00227 alloc_mem();
00228 memcpy(data, &r, _size);
00229
00230 return *this;
00231 }
00232
00242 const Record<T>& operator=(const T* r) throw(YAPETException){
00243 free_mem();
00244
00245 alloc_mem();
00246 memcpy(data, r, _size);
00247
00248 return *this;
00249 }
00250
00264 const Record<T>& operator=(const BDBuffer& bdb)
00265 throw(YAPETException) {
00266 if (bdb.size() < _size)
00267 throw YAPETException(_("BDBuffer too small"));
00268
00269 free_mem();
00270
00271 alloc_mem();
00272
00273 memcpy(data, bdb(), _size);
00274
00275 return *this;
00276 }
00277 };
00278 }
00279
00280 #endif // _RECORD_H