dbuffer.h

Go to the documentation of this file.
00001 /* Dynamic buffer class.
00002  *
00003  * PegSoft sockets library (c) 2007 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef SOCKETS_DBUFFER_H
00025 #define SOCKETS_DBUFFER_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <list>
00030 
00031 #include <sockets/buffer.h>
00032 
00033 /*************************************************************************/
00034 
00035 namespace sockets
00036 {
00037 
00038 /*************************************************************************/
00039 
00041 
00053 class DBuffer : public Buffer
00054 {
00055     private:
00056         /* Classes */
00057         
00058         class Chunk;
00059     public:
00060         /* Constants */
00061         
00063         static size_t const chunk_unit = 1024;
00064         
00065         /* Typedefs */
00066         
00067         typedef std::list<Chunk *> chunks_list;
00068         
00069         /* Functions */
00070         
00072 
00081         explicit DBuffer(uint8_t chunkmul, size_t maxsize);
00083         virtual ~DBuffer();
00084         
00086         virtual bool read(size_t max,
00087                 char const **data, size_t *len) const;
00089         virtual bool write(char const *data, size_t len);
00091         virtual bool erase(size_t len);
00093         virtual bool clear();
00094         
00096         virtual bool empty() const { return !m_size; }
00098         virtual size_type size() const { return m_size; }
00100         virtual size_type max_size() const { return m_maxsize; }
00101     protected:
00103         virtual bool read_byte(size_type pos, char *ch) const;
00104     private:
00105         /* Classes */
00106         
00107         class Chunk
00108         {
00109             public:
00111                 Chunk(size_t maxsize) : m_data(new char [maxsize]), m_first(0),
00112                         m_next(m_data), m_size(0), m_maxsize(maxsize),
00113                         m_remaining(maxsize) { }
00115                 ~Chunk() { delete [] m_data; }
00116                 
00118                 bool write(char const *data, size_t len);
00120                 bool erase(size_t len);
00122                 void clear();
00123                 
00125                 char const *data() const { return m_data; }
00127                 char const *first() const { return m_first; }
00129                 char *next() { return m_next; }
00130                 
00132                 size_t empty() const { return !m_size; }
00134                 size_t size() const { return m_size; }
00136                 size_t max_size() const { return m_maxsize; }
00138                 size_t remaining() const { return m_remaining; }
00139             private:
00140                 char *m_data;
00141                 
00142                 char *m_first;
00143                 char *m_next;
00144                 
00145                 size_t m_size;
00146                 size_t const m_maxsize;
00147                 size_t m_remaining;
00148                 
00149                 Chunk(Chunk const &);
00150                 Chunk &operator=(Chunk const &);
00151         };
00152         
00153         /* Static data */
00154         
00156         static chunks_list chunks_pool[64];
00157         
00158         /* Data */
00159         
00161         chunks_list m_chunks;
00162         
00164         size_t m_chunksize;
00166         uint8_t m_poolidx;
00167         
00169         size_t m_size;
00171         size_t m_maxsize;
00172         
00173         /* Functions */
00174         
00176         DBuffer(DBuffer const &);
00178         DBuffer &operator=(DBuffer const &);
00179         
00181         Chunk *alloc();
00183         void recycle();
00184 };
00185 
00186 /*************************************************************************/
00187 
00188 } /* namespace sockets */
00189 
00190 /*************************************************************************/
00191 
00192 #endif /* SOCKETS_DBUFFER_H */

Generated on Wed Aug 15 00:37:22 2007 for Epona API by  doxygen 1.5.2