lux/storage/qdbm_data_unit.h

00001 #ifndef LUX_STORAGE_QDBMDATAUNIT_H
00002 #define LUX_STORAGE_QDBMDATAUNIT_H
00003 
00004 #include <cstdlib>
00005 #include <cassert>
00006 #include <iostream>
00007 
00008 namespace Lux {
00009 
00010     class QDBMDataUnit {
00011 
00012     public:
00013         QDBMDataUnit(bool is_cleanup_needed = false)
00014         : is_cleanup_needed_(is_cleanup_needed)
00015         {}
00016 
00017         QDBMDataUnit(void *data, unsigned int size, bool is_cleanup_needed = false)
00018         : data_(data), size_(size), is_cleanup_needed_(is_cleanup_needed)
00019         {
00020             assert(data_ && size_ > 0);         
00021         } 
00022 
00023         void set_data(void *data)
00024         {
00025             assert(data);         
00026             data_ = data; 
00027         }
00028 
00029         void set_size(unsigned int size)
00030         {
00031             assert(size > 0);         
00032             size_ = size;
00033         }
00034 
00035         void set(void *data, unsigned int size)
00036         {
00037             assert(data && size > 0);         
00038             set_data(data);
00039             set_size(size);
00040         }
00041 
00042         void *get_data(void)
00043         {
00044             return data_;
00045         }
00046 
00047         unsigned int get_size(void)
00048         {
00049             return size_;
00050         }
00051 
00052         void set_cleanup_needed(bool is_cleanup_needed)
00053         {
00054             is_cleanup_needed_ = is_cleanup_needed;
00055         }
00056 
00057     protected:
00058         ~QDBMDataUnit() {
00059             if (is_cleanup_needed_) {     
00060                 cleanup();
00061             }
00062         }
00063 
00064     private:
00065         void *data_;
00066         unsigned int size_;
00067         bool is_cleanup_needed_;
00068 
00069         void cleanup(void)
00070         {
00071             if (data_ != NULL) {
00072                 // data from QDBM is allocated by malloc, so do free
00073                 free(data_);
00074                 data_ = NULL;
00075             }
00076         }
00077     };
00078 }
00079 
00080 #endif

Generated on Fri Feb 5 15:50:30 2010 for Lux by  doxygen 1.4.7