sprint 1-alpha
sprint/container/storage_buffer.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::storage_buffer
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library 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 GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00025 #ifndef _SPRINT_STORAGE_BUFFER_H
00026 #define _SPRINT_STORAGE_BUFFER_H
00027 
00028 namespace sprint {
00029 
00039 class storage_buffer {
00040         char * m_str;
00041         unsigned int m_lenght;
00042         unsigned int m_capacity;
00043         public:
00044 
00046         template<class R>
00047         storage_buffer(R *ptr, unsigned int capacity) : m_str( reinterpret_cast<char *>(ptr) ), m_lenght(0), m_capacity(capacity * sizeof(R) ) {}
00048 
00050         template<class R>
00051         storage_buffer(const R &buf) : m_str(buf.data()), m_lenght(0), m_capacity(buf.lenght()) {}
00052 
00054         template<class T>
00055         void insert(const T *a, int len)
00056         {
00057       // TODO: check size
00058                 memcpy(&m_str[m_lenght], a, len * sizeof(T));
00059                 m_lenght+=len;
00060         }
00061 
00064         void push_back(char c)
00065         {
00066           m_str[m_lenght] = c;
00067           m_lenght++;
00068     }
00069 
00075         inline storage_buffer & operator << (const char *str)
00076                 {
00077                 insert(str, strlen(str) );
00078                 return *this;
00079                 }
00080 
00086         inline storage_buffer & operator << (char c)
00087                 {
00088         push_back(c);
00089                 return *this;
00090                 }
00091 
00097         template<class T>
00098         inline storage_buffer & operator << (const T & c)
00099          {
00100                insert(reinterpret_cast<char *>(&c), sizeof(c) );
00101                return *this;
00102          }
00103                 
00104         
00106         inline const char *str() { m_str[m_lenght]=0; return &m_str[0]; }
00107         
00109         inline const char *data() const { return &m_str[0]; }
00110 
00112         inline unsigned int size() const { return m_lenght; }
00113 
00115         inline unsigned int lenght() const { return m_lenght; }
00116 
00118         inline unsigned int capacity() const { return m_capacity; }
00119 };
00120 
00121 }
00122 
00123 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines