buffer.h

00001 
00002 /* Copyright (c) 2007-2010, 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/debug.h>       // EQASSERT macro
00022 
00023 namespace eq
00024 {
00025 namespace base
00026 {
00037     template< typename T > class Buffer
00038     {
00039     public:
00041         Buffer() : _data(0), _size(0), _maxSize(0) {}
00042 
00044         Buffer( const uint64_t size ) : _data(0), _size(0), _maxSize(0) 
00045             { resize( size ); }
00046 
00048         ~Buffer() { clear(); }
00049 
00051         void clear() 
00052             { if( _data ) free( _data ); _data=0; _size=0; _maxSize=0; }
00053 
00055         Buffer( Buffer& from )
00056             {
00057                 _data = from._data; _size = from._size; _maxSize =from._maxSize;
00058                 from._data = 0; from._size = 0; from._maxSize = 0;
00059             }
00060 
00062         const Buffer& operator = ( Buffer& from )
00063             {
00064                 replace( from._data, from._size );
00065                 return *this;
00066             }
00067 
00069         T&       operator[]( const uint64_t position )
00070             { EQASSERT( _size > position ); return _data[ position ]; }
00071 
00073         const T& operator[]( const uint64_t position ) const
00074             { EQASSERT( _size > position ); return _data[ position ]; }
00075 
00082         void resize( const uint64_t newSize )
00083             { 
00084                 _size = newSize;
00085                 if( newSize <= _maxSize )
00086                     return;
00087                 
00088                 const uint64_t nBytes = newSize * sizeof( T );
00089                 if( _data )
00090                     _data = static_cast< T* >( realloc( _data, nBytes ));
00091                 else
00092                     _data = static_cast< T* >( malloc( nBytes ));
00093                 
00094                 _maxSize = newSize;
00095             }
00096 
00103         void grow( const uint64_t newSize )
00104             { 
00105                 if( newSize > _size )
00106                     resize( newSize );
00107             }
00108 
00115         void reserve( const uint64_t newSize )
00116             { 
00117                 if( newSize <= _maxSize )
00118                     return;
00119 
00120                 if( _data )
00121                     free( _data );
00122                 
00123                 _data = static_cast< T* >( malloc( newSize * sizeof( T )));
00124                 _maxSize = _size;
00125             }
00126 
00128         void append( const T* data, const uint64_t size )
00129             {
00130                 EQASSERT( data );
00131                 EQASSERT( size );
00132 
00133                 const uint64_t oldSize = _size;
00134                 resize( oldSize + size );
00135                 memcpy( _data + oldSize, data, size * sizeof( T ));
00136             }
00137 
00139         void append( const T& element )
00140             {
00141                 resize( _size + 1 );
00142                 _data[ _size - 1 ] = element;
00143             }
00144 
00146         void replace( const void* data, const uint64_t size )
00147             {
00148                 EQASSERT( data );
00149                 EQASSERT( size );
00150 
00151                 reserve( size );
00152                 memcpy( _data, data, size * sizeof( T ));
00153                 _size = size;
00154             }
00155 
00157         void swap( Buffer& buffer )
00158             {
00159                 T*             tmpData    = buffer._data;
00160                 const uint64_t tmpSize    = buffer._size;
00161                 const uint64_t tmpMaxSize = buffer._maxSize;
00162 
00163                 buffer._data = _data;
00164                 buffer._size = _size;
00165                 buffer._maxSize = _maxSize;
00166 
00167                 _data     = tmpData;
00168                 _size     = tmpSize;
00169                 _maxSize = tmpMaxSize;
00170             }
00171 
00173         T* getData() { return _data; }
00174 
00176         const T* getData() const { return _data; }
00177 
00186         bool setSize( const uint64_t size )
00187             {
00188                 EQASSERT( size <= _maxSize );
00189                 if( size > _maxSize )
00190                     return false;
00191 
00192                 _size = size;
00193                 return true;
00194             }
00195                     
00197         uint64_t getSize() const { return _size; }
00198         
00200         bool isEmpty() const { return (_size==0); }
00201         
00203         uint64_t getMaxSize() const { return _maxSize; }
00204 
00205     private:
00207         T* _data;
00208 
00210         uint64_t _size;
00211 
00213         uint64_t _maxSize;
00214     };
00215 
00216     typedef Buffer< uint8_t > Bufferb;
00217 }
00218 
00219 }
00220 #endif //EQBASE_BUFFER_H
Generated on Sat Feb 6 12:59:42 2010 for Equalizer 0.9.1 by  doxygen 1.6.1