UTBuffer.h

Go to the documentation of this file.
00001 //==================================================================================================
00002 // Copyright (C) 2010  Brian Tietz    sdbtietz at yahoo dot com
00003 //
00004 // This program is free software; you can redistribute it and/or modify it under the terms of the
00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the
00006 // License.
00007 //
00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010 // General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU General Public License along with this program; if
00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00014 // 02110-1301, USA.
00015 //
00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com)
00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that
00018 // license as applied to the UT library in exchange for either substantial contributions to the
00019 // development of the UT library or other forms of compensation.  Any such waiver must be
00020 // established in writing between the copyright holder and the commercial entity obtaining such a
00021 // waiver.
00022 //==================================================================================================
00023 
00024 
00025 #ifndef _UT_BUFFER_H_
00026 #define _UT_BUFFER_H_
00027 
00028 
00029 //==================================================================================================
00030 //=== Project headers
00031 //==================================================================================================
00032 #include "UT.h"
00033 
00034 
00035 //==================================================================================================
00036 class UT_EXPORT Buffer_t
00037 //==================================================================================================
00047 {
00048     //----------------------------------------------------------------------------------------------
00049     public:
00050     //----------------------------------------------------------------------------------------------
00051     inline              Buffer_t();
00053 
00054     inline              Buffer_t( byte* stack_buffer, int stack_buffer_size );
00058 
00059     inline              ~Buffer_t();
00063 
00064     inline byte*        Buffer( int size, bool expect_more_growth = false );
00072 
00073     inline void         InstallStackBuffer( byte* stack_buffer, int stack_buffer_size );
00076 
00077     inline const byte*  ReadOnlyBuffer() const;
00082 
00083     inline byte&        operator [](int index);
00089 
00090     inline byte         ByteAt(int index) const;
00095 
00096     inline int          ValidSize() const;
00100 
00101     inline void         SetValidSize(int size);
00111 
00112     inline int          AllocatedSize() const;
00114 
00115     inline void         GrowIfNeeded( int min_allocated_bytes, bool expect_more_growth = false );
00119 
00120     //----------------------------------------------------------------------------------------------
00121     private:
00122     //----------------------------------------------------------------------------------------------
00123     void                Reallocate( int size, bool expect_more_growth );
00124                             // Used internally to reallocate the buffer.  The buffer size must be
00125                             // greater than what is presently allocated.
00126 
00127     //----------------------------------------------------------------------------------------------
00128     private:
00129     //----------------------------------------------------------------------------------------------
00130     byte*               m_buffer;
00131     int                 m_size;
00132     int                 m_valid;
00133     bool                m_buffer_from_stack;
00134 };
00135 
00136 
00137 //==================================================================================================
00138 template <class T>
00139 class SimpleTypedBuffer_t
00140 //==================================================================================================
00141 : private Buffer_t
00149 {
00150     //----------------------------------------------------------------------------------------------
00151     public:
00152     //----------------------------------------------------------------------------------------------
00153     inline              SimpleTypedBuffer_t();
00155 
00156     inline              SimpleTypedBuffer_t( T* stack_buffer, int num_elements );
00160 
00161     inline void         InstallStackBuffer( T* stack_buffer, int num_elements );
00164 
00165     inline T*           ItemBuffer( int num_elements, bool expect_more_growth = false );
00173 
00174     inline const T*     ReadOnlyItemBuffer() const;
00179 
00180     inline T&           operator [](int index);
00186 
00187     inline const T&     ItemAt(int index) const;
00192 
00193     inline int          NumValidItems() const;
00198 
00199     inline void         SetNumValidItems(int count);
00209 
00210     inline int          AllocatedItems() const;
00213 
00214     inline void         GrowIfNeeded( int min_allocated_items, bool expect_more_growth = false );
00218 };
00219 
00220 
00221 
00222 
00223 //==================================================================================================
00224 //==================================================================================================
00225 //===
00226 //=== Inline function implementations: class Buffer_t
00227 //===
00228 //==================================================================================================
00229 //==================================================================================================
00230 inline
00231 Buffer_t::Buffer_t()
00232 {
00233     m_buffer = NULL;
00234     m_size = 0;
00235     m_valid = 0;
00236     m_buffer_from_stack = false;
00237 }
00238 
00239 
00240 inline
00241 Buffer_t::Buffer_t( byte* stack_buffer, int stack_buffer_size )
00242 {
00243     m_buffer = stack_buffer;
00244     m_size = stack_buffer_size;
00245     m_valid = 0;
00246     m_buffer_from_stack = true;
00247 }
00248 
00249 
00250 inline
00251 Buffer_t::~Buffer_t()
00252 {
00253     if(m_buffer && !m_buffer_from_stack)
00254         delete[] m_buffer;
00255 }
00256 
00257 
00258 inline byte*
00259 Buffer_t::Buffer( int size, bool expect_more_growth )
00260 {
00261     if(size > m_size)
00262         Reallocate( size, expect_more_growth );
00263     if(m_valid < size)
00264         m_valid = size;
00265     return m_buffer;
00266 }
00267 
00268 
00269 inline void
00270 Buffer_t::InstallStackBuffer( byte* stack_buffer, int stack_buffer_size )
00271 {
00272     if(m_buffer && !m_buffer_from_stack)
00273         delete[] m_buffer;
00274     m_buffer = stack_buffer;
00275     m_size = stack_buffer_size;
00276     m_valid = 0;
00277     m_buffer_from_stack = true;
00278 }
00279 
00280 
00281 inline const byte*
00282 Buffer_t::ReadOnlyBuffer() const
00283 {
00284     return m_buffer;
00285 }
00286 
00287 
00288 inline byte&
00289 Buffer_t::operator [](int index)
00290 {
00291     if(index >= m_size)
00292         Reallocate( index+1, true );
00293     if(m_valid <= index)
00294         m_valid = index+1;
00295     return m_buffer[index];
00296 }
00297 
00298 
00299 inline byte
00300 Buffer_t::ByteAt(int index) const
00301 {
00302     assert(index < m_valid);
00303     return m_buffer[index];
00304 }
00305 
00306 
00307 inline int
00308 Buffer_t::ValidSize() const
00309 {
00310     return m_valid;
00311 }
00312 
00313 
00314 inline void
00315 Buffer_t::SetValidSize(int size)
00316 {
00317     assert(size >= 0 && size <= m_size);
00318     m_valid = size;
00319 }
00320 
00321 
00322 inline int
00323 Buffer_t::AllocatedSize() const
00324 {
00325     return m_size;
00326 }
00327 
00328 
00329 inline void
00330 Buffer_t::GrowIfNeeded( int min_allocated_bytes, bool expect_more_growth )
00331 {
00332     if(m_size < min_allocated_bytes)
00333         Reallocate( min_allocated_bytes, expect_more_growth );
00334 }
00335 
00336 
00337 
00338 
00339 //==================================================================================================
00340 //==================================================================================================
00341 //===
00342 //=== Template function implementations: class SimpleTypedBuffer_t
00343 //===
00344 //==================================================================================================
00345 //==================================================================================================
00346 template <class T>
00347 inline
00348 SimpleTypedBuffer_t<T>::SimpleTypedBuffer_t()
00349 : Buffer_t()
00350 { }
00351 
00352 
00353 template <class T>
00354 inline
00355 SimpleTypedBuffer_t<T>::SimpleTypedBuffer_t( T* stack_buffer, int num_elements )
00356 : Buffer_t( reinterpret_cast<byte*>(stack_buffer), num_elements * sizeof(T) )
00357 { }
00358 
00359 
00360 template <class T>
00361 inline void
00362 SimpleTypedBuffer_t<T>::InstallStackBuffer( T* stack_buffer, int num_elements )
00363 {
00364     Buffer_t::InstallStackBuffer( reinterpret_cast<byte*>(stack_buffer), num_elements * sizeof(T) );
00365 }
00366 
00367 
00368 template <class T>
00369 inline T*
00370 SimpleTypedBuffer_t<T>::ItemBuffer( int num_elements, bool expect_more_growth )
00371 {
00372     return reinterpret_cast<T*>( Buffer( num_elements * sizeof(T), expect_more_growth ) );
00373 }
00374 
00375 
00376 template <class T>
00377 inline const T*
00378 SimpleTypedBuffer_t<T>::ReadOnlyItemBuffer() const
00379 {
00380     return reinterpret_cast<const T*>( Buffer_t::ReadOnlyBuffer() );
00381 }
00382 
00383 
00384 template <class T>
00385 inline T&
00386 SimpleTypedBuffer_t<T>::operator [](int index)
00387 {
00388     return reinterpret_cast<T*>( Buffer( (index+1) * sizeof(T), true ) )[index];
00389 }
00390 
00391 
00392 template <class T>
00393 inline const T&
00394 SimpleTypedBuffer_t<T>::ItemAt(int index) const
00395 {
00396     assert( ValidSize() >= (index+1) * sizeof(T) );
00397     return reinterpret_cast<T*>( ReadOnlyBuffer() )[index];
00398 }
00399 
00400 
00401 template <class T>
00402 inline int
00403 SimpleTypedBuffer_t<T>::NumValidItems() const
00404 {
00405     return ValidSize() / sizeof(T);
00406 }
00407 
00408 
00409 template <class T>
00410 inline void
00411 SimpleTypedBuffer_t<T>::SetNumValidItems(int count)
00412 {
00413     Buffer_t::SetValidSize( count * sizeof(T) );
00414 }
00415 
00416 
00417 template <class T>
00418 inline int
00419 SimpleTypedBuffer_t<T>::AllocatedItems() const
00420 {
00421     return Buffer_t::AllocatedSize() / sizeof(T);
00422 }
00423 
00424 
00425 template <class T>
00426 inline void
00427 SimpleTypedBuffer_t<T>::GrowIfNeeded( int min_allocated_items, bool expect_more_growth )
00428 {
00429     Buffer_t::GrowIfNeeded( min_allocated_items * sizeof(T), expect_more_growth );
00430 }
00431 
00432 
00433 #endif // _UT_BUFFER_H_

Generated on Tue Dec 14 22:35:05 2010 for UT library by  doxygen 1.6.1