00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef BITDATA_H
00020 #define BITDATA_H
00021
00022 #include <string>
00023 #include <cstdlib>
00024
00025 #include <sigc++/sigc++.h>
00026
00027 #include <bit/enums.h>
00028 #include <bit/pointer.h>
00029
00035 namespace bit {
00036
00037 typedef enum DataMode {
00038 COPY,
00039 MANAGED,
00040 UNMANAGED,
00041 } DataMode;
00042
00049 class Data
00050 {
00051 public:
00052
00054 Data( size_t s = 0 ) throw (std::bad_alloc);
00055
00057 Data( const void* d, size_t s, DataMode mode=COPY ) throw (std::bad_alloc);
00058
00060 Data( const Data& other );
00061
00063 ~Data();
00064
00066 uint8_t* data();
00067
00069 const uint8_t* data() const;
00070
00076 bool set_data( const void* newdata, size_t newsize, DataMode mode=COPY ) throw (std::bad_alloc);
00077
00079 size_t size() const;
00080
00082 size_t max_size() const;
00083
00085 void set_max_size( size_t ms );
00086
00096 bool resize( size_t s ) throw (std::bad_alloc);
00097
00103 Data clone() const;
00104
00105 operator bool();
00106
00107 operator bool() const;
00108
00110 operator uint8_t*();
00111
00113 operator const uint8_t*() const;
00114
00116 std::string encoded( Encoding en ) const;
00117
00119 std::string base64() const;
00120
00122 std::string hex( std::string separator = std::string() ) const;
00123
00125 std::string oct( std::string separator = std::string() ) const;
00126
00128 std::string dec( std::string separator = std::string(" ") ) const;
00129
00130 bool set_data( const std::string& encoded, Encoding en );
00131
00132 bool set_base64_data( const std::string& encoded );
00133
00134 bool set_hex_data( const std::string& encoded );
00135
00136
00137
00138
00139
00141 void clear();
00142
00143 bool operator<( const Data& other ) const;
00144 bool operator<=( const Data& other ) const;
00145 bool operator==( const Data& other ) const;
00146 bool operator!=( const Data& other ) const;
00147 bool operator>=( const Data& other ) const;
00148 bool operator>( const Data& other ) const;
00149
00163 int compare( const Data& other ) const;
00164
00165 protected:
00166
00167 class DataStorage {
00168 public:
00169
00170 DataStorage(): data(NULL), size(0), manage_data(false), max_size(0) { }
00171
00172 ~DataStorage() {
00173 if ( data && manage_data ) {
00174 ::free( data );
00175 data = NULL;
00176 }
00177 }
00178
00179 typedef BitPointer<DataStorage> pointer;
00180
00182 uint8_t* data;
00183
00185 size_t size;
00186
00188 bool manage_data;
00189
00190 size_t max_size;
00191
00192 };
00193
00194 DataStorage::pointer m_data_storage;
00195
00196 };
00197
00198
00199 }
00200
00201 #endif