NewNet/nnbuffer.h

00001 /*  NewNet - A networking framework in C++
00002     Copyright (C) 2006 Ingmar K. Steen (iksteen@gmail.com)
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
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 General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018  */
00019 
00020 #ifndef NEWNET_BUFFER_H
00021 #define NEWNET_BUFFER_H
00022 
00023 #include "nnobject.h"
00024 #include <sys/types.h>
00025 #include <assert.h>
00026 
00027 namespace NewNet
00028 {
00030 
00032   class Buffer : public NewNet::Object
00033   {
00034   public:
00036 
00037     Buffer() : m_Ptr(0), m_Pos(0), m_Count(0), m_Left(0)
00038     {
00039     }
00040 
00042 
00043     Buffer(const Buffer & that) : m_Ptr(0), m_Pos(0), m_Count(0), m_Left(0)
00044     {
00045       append(that.data(), that.count());
00046     }
00047 
00049 
00050     Buffer & operator=(const Buffer & that)
00051     {
00052       m_Left += m_Pos + m_Count;
00053       m_Pos = m_Count = 0;
00054       append(that.data(), that.count());
00055       return *this;
00056     }
00057 
00059 
00060     ~Buffer();
00061 
00063 
00064     unsigned char * data()
00065     {
00066       return m_Ptr + m_Pos;
00067     }
00068 
00070 
00071     const unsigned char * data() const
00072     {
00073       return m_Ptr + m_Pos;
00074     }
00075 
00077 
00079     size_t count() const
00080     {
00081       return m_Count;
00082     }
00083 
00085 
00086     bool empty() const
00087     {
00088       return m_Count == 0;
00089     }
00090 
00092 
00096     void seek(size_t n)
00097     {
00098       assert(n <= m_Count);
00099       m_Pos += n;
00100       m_Count -= n;
00101     }
00102 
00104 
00107     void append(const unsigned char * data, size_t n);
00108 
00109   private:
00110     unsigned char * m_Ptr;
00111     size_t m_Pos, m_Count, m_Left;
00112   };
00113 }
00114 
00115 #endif // NEWNET_BUFFER_H

Generated on Sun Jan 7 14:00:00 2007 for NewNet by  doxygen 1.5.1