conexus/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 CONEXUSDATA_H
00020 #define CONEXUSDATA_H
00021 
00022 #include <stdexcept>
00023 #include <string>
00024 
00025 #include <sigc++/sigc++.h>
00026 
00027 #include <conexus/pointer.h>
00028 
00029 #include <glibmm/timeval.h>
00030 #include <glibmm/thread.h>
00031 
00032 namespace Conexus
00033 {
00034 
00035   typedef enum DataMode {
00036     COPY,
00037     MANAGED,
00038     UNMANAGED,
00039   } DataMode;
00040 
00047   class Data
00048   {
00049     public:
00050 
00052       Data( size_t s = 0, unsigned priority = 0 ) throw (std::bad_alloc);
00053 
00055       Data( const void* d, size_t s, unsigned priority = 0, DataMode mode=COPY ) throw (std::bad_alloc);
00056 
00058       Data( const Data& other );
00059 
00061       ~Data();
00062 
00064       uint8_t* data();
00065 
00067       const uint8_t* data() const;
00068 
00074       bool set_data( const void* newdata, size_t newsize, DataMode mode=COPY ) throw (std::bad_alloc);
00075 
00077       size_t size() const;
00078 
00088       bool resize( size_t s ) throw (std::bad_alloc);
00089 
00095       Data clone() const;
00096 
00097       operator bool();
00098 
00099       operator bool() const;
00100 
00102       operator uint8_t*();
00103 
00105       operator const uint8_t*() const;
00106 
00108       std::string hex_string( std::string separator = std::string() ) const;
00109 
00111       void clear();
00112 
00114       const Glib::TimeVal& time() const;
00115 
00117       void set_time( const Glib::TimeVal& );
00118 
00120       void set_current_time();
00121 
00123       unsigned priority() const;
00124 
00126       void set_priority( unsigned p );
00127 
00128       bool operator<( const Data& other ) const;
00129       bool operator<=( const Data& other ) const;
00130       bool operator==( const Data& other ) const;
00131       bool operator!=( const Data& other ) const;
00132       bool operator>=( const Data& other ) const;
00133       bool operator>( const Data& other ) const;
00134 
00148       int compare( const Data& other ) const;
00149 
00150     protected:
00151 
00152       class Storage {
00153         public:
00154 
00155           Storage(): data(NULL), size(0), manage_data(false), priority(0) { }
00156 
00157           ~Storage() {
00158             if ( data && manage_data ) {
00159                 ::free( data );
00160                 data = NULL;
00161             }
00162           }
00163 
00164           typedef ConexusPointer<Storage> pointer;
00165 
00167           uint8_t* data;
00168 
00170           size_t size;
00171 
00173           bool manage_data;
00174 
00176           unsigned int priority;
00177 
00179           Glib::TimeVal time;
00180 
00181       };
00182 
00183       Storage::pointer m_storage;
00184 
00185   };
00186 
00187 
00188 }
00189 
00190 #endif

Generated on Tue Mar 13 19:54:42 2007 by  doxygen 1.5.1