buffer.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 BITBUFFER_H
00020 #define BITBUFFER_H
00021 
00022 #include <sigc++/sigc++.h>
00023 
00024 #include <bit/data.h>
00025 
00031 namespace bit
00032   {
00033 
00034   typedef enum Location { START, END } Location;
00035   typedef enum Growth { FIXED, DYNAMIC } Growth;
00036   typedef enum DataMode { EXTERNAL_OWNER, TAKE_OWNERSHIP, COPY } DataMode;
00037 
00051   class Buffer: public sigc::trackable
00052     {
00053     public:
00058       Buffer(size_t initial_size=0, Growth growth=DYNAMIC, size_t sizemax=0);
00059 
00063       Buffer(void* external_data, size_t data_octets, DataMode datamode=COPY, Growth growth=DYNAMIC, size_t sizemax=0);
00064 
00065       virtual ~Buffer();
00066 
00073       const uint8_t * const data() const;
00074 
00078       size_t size() const;
00079 
00106       bool unpack(void* mem, size_t mem_octets, Location mem_loc, size_t buf_offset_bits, size_t extract_bits);
00107 
00108       template <typename T>
00109       bool unpack(T& val, Location mem_loc, size_t buf_offset_bits, size_t extract_bits)
00110       {
00111         return unpack(&val, sizeof(T), mem_loc, buf_offset_bits, extract_bits);
00112       }
00113 
00147       bool pack(const void* mem, size_t mem_octets, Location mem_loc, size_t buf_offset, size_t buf_tgtsize, size_t n);
00148 
00149       template <typename T>
00150       bool pack(const T& val, Location mem_loc, size_t offset, size_t destsize, size_t n)
00151       {
00152         bool b;
00153         b = pack(&val, sizeof(T), mem_loc, offset, destsize, n);
00154         return b;
00155       }
00156 
00157       bool clear_bits(size_t offset, size_t bits)
00158       {
00159         return clear_bits(offset, bits, false);
00160       }
00161 
00162       void clear();
00163 
00170       bool is_dynamic() const;
00171 
00172       bool is_fixed() const;
00173 
00174       Growth growth() const;
00175 
00180       void set_growth(Growth growth);
00181 
00185       virtual void set_data(void* data, size_t size, DataMode datamode=COPY);
00186 
00193       virtual size_t set_size(size_t size_request);
00194 
00198       size_t sizemax();
00199 
00205       void set_sizemax(size_t sizemax);
00206 
00217       Buffer& operator=(const Buffer& other);
00218 
00223       sigc::signal<void> signal_data_location_changed()
00224       {
00225         return m_signal_data_location_changed;
00226       }
00227 
00231       sigc::signal<void> signal_data_changed()
00232       {
00233         return m_signal_data_changed;
00234       }
00235 
00239       sigc::signal<void> signal_size_changed()
00240       {
00241         return m_signal_size_changed;
00242       }
00243 
00244     protected:
00245       uint8_t* m_pdata;
00246       size_t m_size;
00247       bool m_is_owner;
00248       Growth m_growth;
00249       size_t m_sizemax;
00250 
00257       void free_buffer();
00258 
00262       bool clear_bits(size_t offset, size_t bits, bool suppress);
00263 
00264       virtual void on_data_location_changed();
00265 
00266       virtual void on_size_changed();
00267 
00268       virtual void on_data_changed();
00269 
00270     private:
00271       sigc::signal<void> m_signal_data_changed;
00272       sigc::signal<void> m_signal_data_location_changed;
00273       sigc::signal<void> m_signal_size_changed;
00274 
00275       bool m_signal_data_changed_need_reemit;
00276       bool m_signal_data_changed_emitting;
00277 
00278 
00279     };
00280 
00281 }
00282 
00283 #endif

Generated on Thu Jul 6 14:38:08 2006 by  doxygen 1.4.6