bit/data.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef BITDATA_H
00020 #define BITDATA_H
00021 
00022 #include <string>
00023 
00024 #include <sigc++/sigc++.h>
00025 
00026 #include <bit/enums.h>
00027 #include <bit/pointer.h>
00028 
00029 namespace bit {
00030 
00031   typedef enum DataMode {
00032     COPY,
00033     MANAGED,
00034     UNMANAGED,
00035   } DataMode;
00036 
00043   class Data
00044   {
00045     public:
00046 
00048       Data( size_t s = 0 ) throw (std::bad_alloc);
00049 
00051       Data( const void* d, size_t s, DataMode mode=COPY ) throw (std::bad_alloc);
00052 
00054       Data( const Data& other );
00055 
00057       ~Data();
00058 
00060       uint8_t* data();
00061 
00063       const uint8_t* data() const;
00064 
00070       bool set_data( const void* newdata, size_t newsize, DataMode mode=COPY ) throw (std::bad_alloc);
00071 
00073       size_t size() const;
00074 
00084       bool resize( size_t s ) throw (std::bad_alloc);
00085 
00091       Data clone() const;
00092 
00093       operator bool();
00094 
00095       operator bool() const;
00096 
00098       operator uint8_t*();
00099 
00101       operator const uint8_t*() const;
00102 
00104       std::string hex_string( std::string separator = std::string() ) const;
00105 
00107       void clear();
00108 
00109       bool operator<( const Data& other ) const;
00110       bool operator<=( const Data& other ) const;
00111       bool operator==( const Data& other ) const;
00112       bool operator!=( const Data& other ) const;
00113       bool operator>=( const Data& other ) const;
00114       bool operator>( const Data& other ) const;
00115 
00129       int compare( const Data& other ) const;
00130 
00131     protected:
00132 
00133       class Storage {
00134         public:
00135 
00136           Storage(): data(NULL), size(0), manage_data(false) { }
00137 
00138           ~Storage() {
00139             if ( data && manage_data ) {
00140                 ::free( data );
00141                 data = NULL;
00142             }
00143           }
00144 
00145           typedef BitPointer<Storage> pointer;
00146 
00148           uint8_t* data;
00149 
00151           size_t size;
00152 
00154           bool manage_data;
00155 
00156       };
00157 
00158       Storage::pointer m_storage;
00159 
00160   };
00161 
00162 
00163 }
00164 
00165 #endif

Generated on Tue Mar 13 20:00:01 2007 by  doxygen 1.5.1