buffer.h

00001 
00002 /* Copyright (c) 2007-2009, Stefan Eilemann <eile@equalizergraphics.com> 
00003  *
00004  * This library is free software; you can redistribute it and/or modify it under
00005  * the terms of the GNU Lesser General Public License version 2.1 as published
00006  * by the Free Software Foundation.
00007  *  
00008  * This library is distributed in the hope that it will be useful, but WITHOUT
00009  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00010  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00011  * details.
00012  * 
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this library; if not, write to the Free Software Foundation, Inc.,
00015  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00016  */
00017 
00018 #ifndef EQBASE_BUFFER_H
00019 #define EQBASE_BUFFER_H
00020 
00021 #include <eq/base/nonCopyable.h> // base class
00022 #include <eq/base/debug.h>       // EQASSERT macro
00023 
00024 namespace eq
00025 {
00026 namespace base
00027 {
00037     template< typename T >
00038     class Buffer
00039     {
00040     public:
00042         Buffer() : _data(0), _size(0), _maxSize(0) {}
00043 
00045         ~Buffer() { clear(); }
00046 
00048         void clear() 
00049             { if( _data ) free( _data ); _data=0; _size=0; _maxSize=0; }
00050 
00052         Buffer( Buffer& from )
00053             {
00054                 _data = from._data; _size = from._size; _maxSize =from._maxSize;
00055                 from._data = 0; from._size = 0; from._maxSize = 0;
00056             }
00057 
00059         const Buffer& operator = ( Buffer& from )
00060             {
00061                 replace( from._data, from._size );
00062                 return *this;
00063             }
00064 
00066         T&       operator[]( const size_t position )
00067             { EQASSERT( _size > position ); return _data[ position ]; }
00069         const T& operator[]( const size_t position) const
00070             { EQASSERT( _size > position ); return _data[ position ]; }
00071 
00076         void resize( const uint64_t newSize )
00077             { 
00078                 _size = newSize;
00079                 if( newSize <= _maxSize )
00080                     return;
00081                 
00082                 const size_t nBytes = newSize * sizeof( T );
00083                 if( _data )
00084                     _data = static_cast< T* >( realloc( _data, nBytes ));
00085                 else
00086                     _data = static_cast< T* >( malloc( nBytes ));
00087                 
00088                 _maxSize = newSize;
00089             }
00090 
00095         void reserve( const uint64_t newSize )
00096             { 
00097                 if( newSize <= _maxSize )
00098                     return;
00099 
00100                 if( _data )
00101                     free( _data );
00102                 
00103                 _data = static_cast< T* >( malloc( newSize * sizeof( T )));
00104                 _maxSize = _size;
00105             }
00106 
00108         void append( const T* addData, const uint64_t addSize )
00109             {
00110                 EQASSERT( addData );
00111                 EQASSERT( addSize );
00112 
00113                 const uint64_t oldSize = _size;
00114                 resize( oldSize + addSize );
00115                 memcpy( _data + oldSize, addData, addSize * sizeof( T ));
00116             }
00117 
00119         void append( const T& element )
00120             {
00121                 resize( _size + 1 );
00122                 _data[ _size - 1 ] = element;
00123             }
00124 
00126         void replace( const void* newData, const uint64_t newSize )
00127             {
00128                 EQASSERT( newData );
00129                 EQASSERT( newSize );
00130 
00131                 reserve( newSize );
00132                 memcpy( _data, newData, newSize * sizeof( T ));
00133                 _size = newSize;
00134             }
00135 
00137         void swap( Buffer& buffer )
00138             {
00139                 T*             tmpData    = buffer._data;
00140                 const uint64_t tmpSize    = buffer._size;
00141                 const uint64_t tmpMaxSize = buffer._maxSize;
00142 
00143                 buffer._data = _data;
00144                 buffer._size = _size;
00145                 buffer._maxSize = _maxSize;
00146 
00147                 _data     = tmpData;
00148                 _size     = tmpSize;
00149                 _maxSize = tmpMaxSize;
00150             }
00151 
00153         T* getData() { return _data; }
00154 
00156         const T* getData() const { return _data; }
00157 
00165         bool setSize( const uint64_t size )
00166             {
00167                 EQASSERT( size <= _maxSize );
00168                 if( size > _maxSize )
00169                     return false;
00170 
00171                 _size = size;
00172                 return true;
00173             }
00174                     
00176         uint64_t getSize() const { return _size; }
00177         
00179         bool isEmpty() const { return (_size==0); }
00180         
00182         uint64_t getMaxSize() const { return _maxSize; }
00183 
00184     private:
00186         T*    _data;
00187 
00189         uint64_t _size;
00190 
00192         uint64_t _maxSize;
00193     };
00194 
00195     typedef Buffer< uint8_t > Bufferb;
00196 }
00197 
00198 }
00199 #endif //EQBASE_BUFFER_H
Generated on Mon Aug 10 18:58:31 2009 for Equalizer 0.9 by  doxygen 1.5.8