buffer.h

Go to the documentation of this file.
00001 /* Buffers base 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_BUFFER_H
00025 #define SOCKETS_BUFFER_H
00026 
00027 /*************************************************************************/
00028 
00029 #include <iterator>
00030 
00031 /*************************************************************************/
00032 
00033 namespace sockets
00034 {
00035 
00036 /*************************************************************************/
00037 
00039 
00044 class Buffer
00045 {
00046     public:
00047         /* Typedefs */
00049         typedef char value_type;
00051         typedef char & reference;
00053         typedef char const & const_reference;
00055         typedef char * pointer;
00057         typedef char const * const_pointer;
00059         typedef size_t size_type;
00061         typedef ptrdiff_t difference_type;
00062         
00063         /* Classes */
00064         
00066 
00071         class Iterator;
00072         friend class Iterator;
00073         class Iterator : public std::iterator<std::bidirectional_iterator_tag,
00074                 Buffer::value_type, Buffer::difference_type,
00075                 Buffer::const_pointer, Buffer::const_reference>
00076         {   
00077             public:
00078                 /* Constants */
00079                 
00081                 enum
00082                 {
00084                     isBegin = 0,
00086                     isEnd = 1
00087                 };
00088                 
00089                 /* Functions */
00090             
00092                 Iterator() : m_buffer(0), m_idx(0), m_ch(0) { }
00094                 Iterator(Buffer const *buffer, int state);
00096                 Iterator(Buffer const *buffer, Buffer::size_type idx);
00098                 Iterator(Buffer const *buffer, Buffer::size_type idx, char ch) :
00099                         m_buffer(buffer), m_idx(idx), m_ch(ch) { }
00101                 Iterator(Iterator const &it) : m_buffer(it.m_buffer),
00102                         m_idx(it.m_idx), m_ch(it.m_ch) { }
00104                 Iterator &operator=(Iterator const &right);
00105                 
00107                 reference operator*() const { return m_ch; }
00109                 pointer operator->() const { return &m_ch; }
00110                 
00112                 bool operator==(Iterator const &right) const;
00114                 bool operator!=(Iterator const &right) const
00115                         { return !(*this == right); }
00116                 
00118                 Iterator &operator++();
00120                 Iterator operator++(int);
00121                 
00123                 Iterator &operator--();
00125                 Iterator operator--(int);
00126                 
00128                 difference_type operator-(Iterator const &right) const
00129                         { return (m_idx - right.m_idx); }
00130             private:
00131                 Buffer const *m_buffer;
00132                 Buffer::size_type m_idx;
00133                 value_type m_ch;
00134         };
00135         
00136         /* Typedefs */
00137         
00139         typedef Iterator iterator;
00141         typedef iterator const_iterator;
00143         typedef std::reverse_iterator<iterator> reverse_iterator;
00145         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00146         
00147         /* Functions */
00148         
00150         virtual ~Buffer() { }
00151         
00153 
00172         virtual bool read(size_t max,
00173                 char const **data, size_t *len) const = 0;
00174         
00176 
00186         virtual bool write(char const *data, size_t len) = 0;
00187         
00189 
00200         virtual bool erase(size_t len) = 0;
00201         
00203         virtual bool erase(iterator end) { return erase(end - begin()); }
00204         
00206 
00209         virtual bool clear() = 0;
00210         
00212         iterator begin() { return iterator(this, iterator::isBegin); }
00214         iterator end() { return iterator(this, iterator::isEnd); }
00216         const_iterator begin() const 
00217                 { return const_iterator(this, const_iterator::isBegin); }
00219         const_iterator end() const 
00220                 { return const_iterator(this, const_iterator::isEnd); }
00221         
00223         reverse_iterator rbegin() { return reverse_iterator(end()); }
00225         reverse_iterator rend() { return reverse_iterator(begin()); }
00227         const_reverse_iterator rbegin() const
00228                 { return const_reverse_iterator(end()); }
00230         const_reverse_iterator rend() const
00231                 { return const_reverse_iterator(begin()); }
00232         
00234         virtual bool empty() const = 0;
00236         virtual size_type size() const = 0;
00238         virtual size_type max_size() const = 0;
00240         virtual size_type remaining() const { return max_size() - size(); }
00241     protected:
00243 
00250         virtual bool read_byte(size_type pos, char *ch) const = 0;
00251     private:
00252 };
00253 
00254 /*************************************************************************/
00255 
00256 } /* namespace sockets */
00257 
00258 /*************************************************************************/
00259 
00260 #endif /* SOCKETS_BUFFER_H */

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