Equalizer
1.2.1
|
00001 00002 /* Copyright (c) 2007-2011, 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 COBASE_BUFFER_H 00019 #define COBASE_BUFFER_H 00020 00021 #include <co/base/debug.h> // EQASSERT macro 00022 #include <co/base/types.h> 00023 00024 #include <cstdlib> // for malloc 00025 00026 namespace co 00027 { 00028 namespace base 00029 { 00040 template< typename T > class Buffer 00041 { 00042 public: 00044 Buffer() : _data(0), _size(0), _maxSize(0) {} 00045 00047 Buffer( const uint64_t size ) : _data(0), _size(0), _maxSize(0) 00048 { reset( size ); } 00049 00051 ~Buffer() { clear(); } 00052 00054 void clear() 00055 { if( _data ) free( _data ); _data=0; _size=0; _maxSize=0; } 00056 00062 T* pack() 00063 { 00064 if( _maxSize != _size ) 00065 { 00066 _data = static_cast< T* >( realloc( _data, 00067 _size * sizeof( T ))); 00068 _maxSize = _size; 00069 } 00070 return _data; 00071 } 00072 00074 Buffer( Buffer& from ) 00075 { 00076 _data = from._data; _size = from._size; _maxSize =from._maxSize; 00077 from._data = 0; from._size = 0; from._maxSize = 0; 00078 } 00079 00081 const Buffer& operator = ( Buffer& from ) 00082 { 00083 replace( from._data, from._size ); 00084 return *this; 00085 } 00086 00088 T& operator[]( const uint64_t position ) 00089 { EQASSERT( _size > position ); return _data[ position ]; } 00090 00092 const T& operator[]( const uint64_t position ) const 00093 { EQASSERT( _size > position ); return _data[ position ]; } 00094 00102 T* resize( const uint64_t newSize ) 00103 { 00104 _size = newSize; 00105 if( newSize <= _maxSize ) 00106 return _data; 00107 00108 // avoid excessive reallocs 00109 const uint64_t nElems = newSize + (newSize >> 3); 00110 const uint64_t nBytes = nElems * sizeof( T ); 00111 _data = static_cast< T* >( realloc( _data, nBytes )); 00112 _maxSize = nElems; 00113 return _data; 00114 } 00115 00122 void grow( const uint64_t newSize ) 00123 { 00124 if( newSize > _size ) 00125 resize( newSize ); 00126 } 00127 00135 T* reserve( const uint64_t newSize ) 00136 { 00137 if( newSize <= _maxSize ) 00138 return _data; 00139 00140 if( _data ) 00141 free( _data ); 00142 00143 _data = static_cast< T* >( malloc( newSize * sizeof( T ))); 00144 _maxSize = newSize; 00145 return _data; 00146 } 00147 00155 T* reset( const uint64_t newSize ) 00156 { 00157 reserve( newSize ); 00158 setSize( newSize ); 00159 return _data; 00160 } 00161 00163 void append( const T* data, const uint64_t size ) 00164 { 00165 EQASSERT( data ); 00166 EQASSERT( size ); 00167 00168 const uint64_t oldSize = _size; 00169 resize( oldSize + size ); 00170 memcpy( _data + oldSize, data, size * sizeof( T )); 00171 } 00172 00174 void append( const T& element ) 00175 { 00176 resize( _size + 1 ); 00177 _data[ _size - 1 ] = element; 00178 } 00179 00181 void replace( const void* data, const uint64_t size ) 00182 { 00183 EQASSERT( data ); 00184 EQASSERT( size ); 00185 00186 reserve( size ); 00187 memcpy( _data, data, size * sizeof( T )); 00188 _size = size; 00189 } 00190 00192 void swap( Buffer& buffer ) 00193 { 00194 T* tmpData = buffer._data; 00195 const uint64_t tmpSize = buffer._size; 00196 const uint64_t tmpMaxSize = buffer._maxSize; 00197 00198 buffer._data = _data; 00199 buffer._size = _size; 00200 buffer._maxSize = _maxSize; 00201 00202 _data = tmpData; 00203 _size = tmpSize; 00204 _maxSize = tmpMaxSize; 00205 } 00206 00208 T* getData() { return _data; } 00209 00211 const T* getData() const { return _data; } 00212 00221 bool setSize( const uint64_t size ) 00222 { 00223 EQASSERT( size <= _maxSize ); 00224 if( size > _maxSize ) 00225 return false; 00226 00227 _size = size; 00228 return true; 00229 } 00230 00232 uint64_t getSize() const { return _size; } 00233 00235 bool isEmpty() const { return (_size==0); } 00236 00238 uint64_t getMaxSize() const { return _maxSize; } 00239 00240 private: 00242 T* _data; 00243 00245 uint64_t _size; 00246 00248 uint64_t _maxSize; 00249 }; 00250 00251 typedef Buffer< uint8_t > Bufferb; 00252 } 00253 00254 } 00255 #endif //COBASE_BUFFER_H