EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Buffer Class Reference

Generic class for buffers. More...

#include "Buffer.h"

Classes

class  const_iterator
 Buffer's const iterator. More...
 
class  iterator
 Buffer's iterator. More...
 

Public Types

typedef size_t size_type
 Typedef for size type. More...
 

Public Member Functions

 Buffer (size_type len=0, const uint8 &fill=0)
 Creates buffer of given length. More...
 
template<typename Iter >
 Buffer (Iter first, Iter last)
 Creates buffer with given content. More...
 
 Buffer (const Buffer &oth)
 Copy constructor. More...
 
 ~Buffer ()
 Destructor; deletes buffer. More...
 
template<typename T >
iterator< T > begin ()
 
template<typename T >
const_iterator< T > begin () const
 
template<typename T >
iterator< T > end ()
 
template<typename T >
const_iterator< T > end () const
 
template<typename T >
T & Get (size_type index)
 Gets element from buffer. More...
 
template<typename T >
const T & Get (size_type index) const
 Gets const element from buffer. More...
 
uint8operator[] (size_type index)
 Overload of access operator[]. More...
 
const uint8operator[] (size_type index) const
 Overload of const access operator[]. More...
 
template<typename T >
void Append (const T &value)
 Appends a single value to buffer. More...
 
template<typename Iter >
void AppendSeq (Iter first, Iter last)
 Appends a sequence of elements to buffer. More...
 
template<typename T >
void Assign (const T &value)
 Assigns a single value to buffer. More...
 
template<typename Iter >
void AssignSeq (Iter first, Iter last)
 Assigns a sequence of elements to buffer. More...
 
template<typename T >
void AssignAt (const_iterator< T > index, const T &value)
 Assigns a single value to buffer at specific point. More...
 
template<typename Iter >
void AssignSeqAt (const_iterator< typename std::iterator_traits< Iter >::value_type > index, Iter first, Iter last)
 Assigns a sequence of elements to buffer at specific point. More...
 
template<typename T >
Bufferoperator<< (const T &value)
 Appends a value to buffer. More...
 
template<typename T >
Bufferoperator= (const T &value)
 Assigns new value to buffer. More...
 
Bufferoperator= (const Buffer &value)
 Copy operator. More...
 
size_type size () const
 
size_type capacity () const
 
template<typename T >
void Reserve (size_type requiredCount)
 Reserves (pre-allocates) memory for buffer. More...
 
template<typename T >
void ReserveAt (const_iterator< T > index, size_type requiredCount)
 Reserves (pre-allocates) memory for buffer at specific point. More...
 
template<typename T >
void Resize (size_type requiredCount, const uint8 &fill=0)
 Resizes buffer. More...
 
template<typename T >
void ResizeAt (const_iterator< T > index, size_type requiredCount, const uint8 &fill=0)
 Resizes buffer. More...
 

Protected Member Functions

template<typename T >
void _Resize (size_type requiredCount)
 Resizes buffer. More...
 
template<typename T >
void _ResizeAt (const_iterator< T > index, size_type requiredCount)
 Resizes buffer. More...
 
void _Reallocate (size_type requiredSize)
 Reallocates buffer. More...
 

Static Protected Member Functions

static size_type _CalcBufferCapacity (size_type currentCapacity, size_type requiredSize)
 Calculates buffer capacity. More...
 

Protected Attributes

uint8mBuffer
 Pointer to start of buffer. More...
 
size_type mSize
 Current size of buffer, in bytes. More...
 
size_type mCapacity
 Current capacity of buffer, in bytes. More...
 

Detailed Description

Generic class for buffers.

This class incorporates all stuff which may be handy when manipulating with buffers.

Author
Bloody.Rabbit

Definition at line 40 of file Buffer.h.

Member Typedef Documentation

typedef size_t Buffer::size_type

Typedef for size type.

Definition at line 44 of file Buffer.h.

Constructor & Destructor Documentation

Buffer::Buffer ( size_type  len = 0,
const uint8 fill = 0 
)
inline

Creates buffer of given length.

Resizes buffer to be len bytes long, filling it with value.

Parameters
[in]lenLength of buffer to be created.
[in]fillValue to fill the buffer with.

Definition at line 332 of file Buffer.h.

333  : mBuffer( NULL ),
334  mSize( 0 ),
335  mCapacity( 0 )
336  {
337  /* unfortunately, we cannot use template here
338  since it's not possible to explicitly instantiate
339  constructor .... assuming uint8 */
340  Resize< uint8 >( len, fill );
341  }
size_type mSize
Current size of buffer, in bytes.
Definition: Buffer.h:713
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
size_type mCapacity
Current capacity of buffer, in bytes.
Definition: Buffer.h:715
template<typename Iter >
Buffer::Buffer ( Iter  first,
Iter  last 
)
inline

Creates buffer with given content.

Fills buffer with content determined by iterators first and last.

Parameters
[in]firstIterator pointing to first element.
[in]lastIterator pointing to element after the last one.

Definition at line 352 of file Buffer.h.

References AssignSeq().

353  : mBuffer( NULL ),
354  mSize( 0 ),
355  mCapacity( 0 )
356  {
357  // assign the content
358  AssignSeq( first, last );
359  }
size_type mSize
Current size of buffer, in bytes.
Definition: Buffer.h:713
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
size_type mCapacity
Current capacity of buffer, in bytes.
Definition: Buffer.h:715
void AssignSeq(Iter first, Iter last)
Assigns a sequence of elements to buffer.
Definition: Buffer.h:504

Here is the call graph for this function:

Buffer::Buffer ( const Buffer oth)
inline

Copy constructor.

Definition at line 361 of file Buffer.h.

362  : mBuffer( NULL ),
363  mSize( 0 ),
364  mCapacity( 0 )
365  {
366  // Use assignment operator
367  *this = oth;
368  }
size_type mSize
Current size of buffer, in bytes.
Definition: Buffer.h:713
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
size_type mCapacity
Current capacity of buffer, in bytes.
Definition: Buffer.h:715
Buffer::~Buffer ( )
inline

Destructor; deletes buffer.

Definition at line 370 of file Buffer.h.

References mBuffer, and SafeFree().

371  {
372  // Free buffer
373  SafeFree( mBuffer );
374  }
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
void SafeFree(T *&p)
Frees and nullifies an array pointer.
Definition: SafeMem.h:111

Here is the call graph for this function:

Member Function Documentation

static size_type Buffer::_CalcBufferCapacity ( size_type  currentCapacity,
size_type  requiredSize 
)
inlinestaticprotected

Calculates buffer capacity.

Based on current capacity and required size of the buffer, this function calculates capacity of buffer to be allocated.

Parameters
[in]currentCapacityCurrent capacity of buffer, in bytes.
[in]requiredSizeRequired size of buffer, in bytes.
Returns
Capacity to be allocated.

Definition at line 791 of file Buffer.h.

References npowof2().

Referenced by _Reallocate().

792  {
793  size_type newCapacity = 0;
794 
795  // if more than 0x100 bytes required, return next power of 2
796  if( 0x100 < requiredSize )
797  newCapacity = (size_type)npowof2( requiredSize );
798  // else if non-zero, return 0x100 bytes
799  else if( 0 < requiredSize )
800  newCapacity = 0x100;
801  // else return 0 bytes
802 
803  /* if current capacity is sufficient and at the same time smaller
804  than the new capacity, return current one ... saves resources */
805  if( requiredSize <= currentCapacity && currentCapacity < newCapacity )
806  return currentCapacity;
807  else
808  return newCapacity;
809  }
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
int64 npowof2(int64 num)
Calculates next (greater or equal) power-of-two number.
Definition: misc.cpp:95

Here is the call graph for this function:

Here is the caller graph for this function:

void Buffer::_Reallocate ( size_type  requiredSize)
inlineprotected

Reallocates buffer.

Reallocates buffer so it can efficiently store given amount of data.

Parameters
[in]requiredSizeThe least required new size of buffer, in bytes.

Definition at line 763 of file Buffer.h.

References _CalcBufferCapacity(), and capacity().

Referenced by _ResizeAt(), and ReserveAt().

764  {
765  // calculate new capacity for required size
766  size_type newCapacity = _CalcBufferCapacity( capacity(), requiredSize );
767  // make sure new capacity is bigger than required size
768  assert( requiredSize <= newCapacity );
769 
770  // has the capacity changed?
771  if( newCapacity != capacity() )
772  {
773  // reallocate
774  mBuffer = (uint8*)realloc( mBuffer, newCapacity );
775  // set new capacity
776  mCapacity = newCapacity;
777  }
778  }
unsigned __int8 uint8
Definition: eve-compat.h:46
static size_type _CalcBufferCapacity(size_type currentCapacity, size_type requiredSize)
Calculates buffer capacity.
Definition: Buffer.h:791
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
size_type mCapacity
Current capacity of buffer, in bytes.
Definition: Buffer.h:715
size_type capacity() const
Definition: Buffer.h:612

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void Buffer::_Resize ( size_type  requiredCount)
inlineprotected

Resizes buffer.

Similar to Resize, but does not care about the gaps that may be created.

Parameters
[in]requiredCountThe number of elements to hold.

Definition at line 725 of file Buffer.h.

726  {
727  // resize at beginning
728  _ResizeAt< T >( begin< T >(), requiredCount );
729  }
template<typename T >
void Buffer::_ResizeAt ( const_iterator< T >  index,
size_type  requiredCount 
)
inlineprotected

Resizes buffer.

Similar to ResizeAt, but does not care about the gaps that may be created.

Parameters
[in]indexThe point at which the buffer should be resized.
[in]requiredCountThe number of elements to hold.

Definition at line 740 of file Buffer.h.

References _Reallocate().

741  {
742  // make sure we're not going off the bounds
743  assert( index <= end< T >() );
744 
745  // turn index into byte offset
746  const size_type _index = ( index.template As< uint8 >() - begin< uint8 >() );
747  // obtain required size in bytes
748  const size_type _requiredSize = sizeof( T ) * requiredCount;
749 
750  // reallocate
751  _Reallocate( _index + _requiredSize );
752  // set new size
753  mSize = ( _index + _requiredSize );
754  }
size_type mSize
Current size of buffer, in bytes.
Definition: Buffer.h:713
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
void _Reallocate(size_type requiredSize)
Reallocates buffer.
Definition: Buffer.h:763

Here is the call graph for this function:

template<typename T >
void Buffer::Append ( const T &  value)
inline
template<typename Iter >
void Buffer::AppendSeq ( Iter  first,
Iter  last 
)
inline

Appends a sequence of elements to buffer.

Parameters
[in]firstIterator pointing to first element.
[in]lastIterator pointing to element after the last one.

Definition at line 455 of file Buffer.h.

Referenced by StreamPacketizer::InputData(), MarshalDeflate(), MarshalStream::Put(), and MarshalStream::VisitPackedRow().

456  {
457  // we wish to append to the end
458  const const_iterator< typename std::iterator_traits< Iter >::value_type >
459  index = end< typename std::iterator_traits< Iter >::value_type >();
460 
461  // make enough room; we're going to fill the gap immediately
462  _ResizeAt< typename std::iterator_traits< Iter >::value_type >( index, last - first );
463 
464  // assign the value, filling the gap
465  AssignSeqAt< Iter >( index, first, last );
466  }

Here is the caller graph for this function:

template<typename T >
void Buffer::Assign ( const T &  value)
inline

Assigns a single value to buffer.

Parameters
[in]valueNew content.

Definition at line 474 of file Buffer.h.

475  {
476  // we wish to assign to beginning
477  const const_iterator< T > index = begin< T >();
478 
479  // do we have enough space?
480  if( 1 <= end< T >() - index )
481  {
482  // yes, we do: assign the value
483  AssignAt< T >( index, value );
484 
485  // shrink the buffer; no gap will be created
486  _ResizeAt< T >( index, 1 );
487  }
488  else
489  {
490  // no, we don't: make enough room; we're going to fill the gap immediately
491  _ResizeAt< T >( index, 1 );
492 
493  // assign the value, filling the gap
494  AssignAt< T >( index, value );
495  }
496  }
template<typename T >
void Buffer::AssignAt ( const_iterator< T >  index,
const T &  value 
)
inline

Assigns a single value to buffer at specific point.

Parameters
[in]indexPoint at which the value should be assigned.
[in]valueNew content.

Definition at line 536 of file Buffer.h.

References mBuffer.

537  {
538  // make sure we're not going off the bounds
539  assert( 1 <= end< T >() - index );
540 
541  // turn iterator into byte offset
542  const size_type _index = ( index.template As< uint8 >() - begin< uint8 >() );
543  // assign the value
544  *(T*)&mBuffer[ _index ] = value;
545  }
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
template<typename Iter >
void Buffer::AssignSeq ( Iter  first,
Iter  last 
)
inline

Assigns a sequence of elements to buffer.

Parameters
[in]firstIterator pointing to first element.
[in]lastIterator pointing to element after the last one.

Definition at line 504 of file Buffer.h.

References end().

Referenced by Buffer(), operator=(), StreamPacketizer::Process(), and TCPConnection::SendData().

505  {
506  // we wish to assign to beginning
507  const const_iterator< typename std::iterator_traits< Iter >::value_type >
508  index = begin< typename std::iterator_traits< Iter >::value_type >();
509 
510  // do we have enough space?
511  if( last - first <= end< typename std::iterator_traits< Iter >::value_type >() - index )
512  {
513  // yes, we do: assign the value
514  AssignSeqAt< Iter >( index, first, last );
515 
516  // shrink the buffer; no gap will be created
517  _ResizeAt< typename std::iterator_traits< Iter >::value_type >( index, last - first );
518  }
519  else
520  {
521  // no, we don't: make enough room; we're going to fill the gap immediately
522  _ResizeAt< typename std::iterator_traits< Iter >::value_type >( index, last - first );
523 
524  // assign the value, filling the gap
525  AssignSeqAt< Iter >( index, first, last );
526  }
527  }
iterator< T > end()
Definition: Buffer.h:387

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename Iter >
void Buffer::AssignSeqAt ( const_iterator< typename std::iterator_traits< Iter >::value_type >  index,
Iter  first,
Iter  last 
)
inline

Assigns a sequence of elements to buffer at specific point.

Parameters
[in]indexPoint at which the sequence of elements should be assigned.
[in]firstIterator pointing to first element.
[in]lastIterator pointing to element after the last one.

Definition at line 554 of file Buffer.h.

References end(), and mBuffer.

555  {
556  // make sure we're not going off the bounds
557  assert( last - first <= end< typename std::iterator_traits< Iter >::value_type >() - index );
558 
559  // is there anything to assign?
560  if( first != last )
561  {
562  // turn the iterator into byte offset
563  const size_type _index = ( index.template As< uint8 >() - begin< uint8 >() );
564  // obtain byte length of input data
565  const size_type _len = sizeof( typename std::iterator_traits< Iter >::value_type ) * ( last - first );
566  // assign the content
567  memmove( &mBuffer[ _index ], &*first, _len );
568  }
569  }
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
iterator< T > end()
Definition: Buffer.h:387

Here is the call graph for this function:

template<typename T >
iterator< T > Buffer::begin ( )
inline
template<typename T >
const_iterator< T > Buffer::begin ( ) const
inline
Returns
const_iterator to begin.

Definition at line 384 of file Buffer.h.

384 { return const_iterator< T >( this, 0 ); }
size_type Buffer::capacity ( ) const
inline
Returns
Current capacity of buffer, in bytes.

Definition at line 612 of file Buffer.h.

References mCapacity.

Referenced by _Reallocate(), and ReserveAt().

612 { return mCapacity; }
size_type mCapacity
Current capacity of buffer, in bytes.
Definition: Buffer.h:715

Here is the caller graph for this function:

template<typename T >
iterator< T > Buffer::end ( )
inline
Returns
iterator to end.

Definition at line 387 of file Buffer.h.

References size().

Referenced by AssignSeq(), AssignSeqAt(), DeflateData(), EVETCPConnection::DumpBuffer(), InflateData(), StreamPacketizer::InputData(), MarshalDeflate(), Buffer::const_iterator< uint8 >::operator*(), operator=(), StreamPacketizer::Process(), EVETCPConnection::QueueRep(), TCPConnection::SendData(), MailDB::SendMail(), MarshalStream::VisitPackedRow(), and MarshalStream::VisitSubStream().

387 { return iterator< T >( this, size() ); }
size_type size() const
Definition: Buffer.h:610

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
const_iterator< T > Buffer::end ( ) const
inline
Returns
const_iterator to end.

Definition at line 390 of file Buffer.h.

References size().

390 { return const_iterator< T >( this, size() ); }
size_type size() const
Definition: Buffer.h:610

Here is the call graph for this function:

template<typename T >
T& Buffer::Get ( size_type  index)
inline

Gets element from buffer.

Parameters
[in]indexIndex of element in the buffer.
Returns
Reference to element.

Definition at line 400 of file Buffer.h.

400 { return *( begin< T >() + index ); }
template<typename T >
const T& Buffer::Get ( size_type  index) const
inline

Gets const element from buffer.

Parameters
[in]indexIndex of element in the buffer.
Returns
Const reference to element.

Definition at line 409 of file Buffer.h.

409 { return *( begin< T >() + index ); }
template<typename T >
Buffer& Buffer::operator<< ( const T &  value)
inline

Appends a value to buffer.

Parameters
[in]valueValue to be appended.

Definition at line 577 of file Buffer.h.

578  {
579  // append the value
580  Append< T >( value );
581  // return ourselves
582  return *this;
583  }
template<typename T >
Buffer& Buffer::operator= ( const T &  value)
inline

Assigns new value to buffer.

Parameters
[in]valueNew content.

Definition at line 590 of file Buffer.h.

591  {
592  // assign the value
593  Assign< T >( value );
594  // return ourselves
595  return *this;
596  }
Buffer& Buffer::operator= ( const Buffer value)
inline

Copy operator.

Definition at line 598 of file Buffer.h.

References AssignSeq(), begin(), and end().

599  {
600  // assign new content
601  AssignSeq( value.begin< uint8 >(), value.end< uint8 >() );
602  // return ourselves
603  return *this;
604  }
unsigned __int8 uint8
Definition: eve-compat.h:46
iterator< T > begin()
Definition: Buffer.h:381
void AssignSeq(Iter first, Iter last)
Assigns a sequence of elements to buffer.
Definition: Buffer.h:504
iterator< T > end()
Definition: Buffer.h:387

Here is the call graph for this function:

uint8& Buffer::operator[] ( size_type  index)
inline

Overload of access operator[].

Parameters
[in]indexIndex of byte to be returned.
Returns
Reference to required byte.

Definition at line 418 of file Buffer.h.

418 { return Get< uint8 >( index ); }
const uint8& Buffer::operator[] ( size_type  index) const
inline

Overload of const access operator[].

Parameters
[in]indexIndex of byte to be returned.
Returns
Const reference to required byte.

Definition at line 426 of file Buffer.h.

426 { return Get< uint8 >( index ); }
template<typename T >
void Buffer::Reserve ( size_type  requiredCount)
inline

Reserves (pre-allocates) memory for buffer.

Pre-allocates memory for buffer to hold at least requiredCount number of elements.

Should be used in cases where lazy reallocating can negatively affect performance.

Parameters
[in]requiredCountThe least reserved number of elements.

Definition at line 626 of file Buffer.h.

Referenced by MarshalStream::VisitPackedRow().

627  {
628  // reserve at beginning
629  ReserveAt< T >( begin< T >(), requiredCount );
630  }

Here is the caller graph for this function:

template<typename T >
void Buffer::ReserveAt ( const_iterator< T >  index,
size_type  requiredCount 
)
inline

Reserves (pre-allocates) memory for buffer at specific point.

Pre-allocates memory for buffer to hold at least requiredCount number of elements, counting from index.

Should be used in cases where lazy reallocating can negatively affect performance.

Parameters
[in]indexThe point at which the memory should be reserved.
[in]requiredCountThe least reserved number of elements.

Definition at line 645 of file Buffer.h.

References _Reallocate(), and capacity().

646  {
647  // make sure we're not going off the bounds
648  assert( index <= end< T >() );
649 
650  // turn iterator into byte offset
651  const size_type _index = ( index.template As< uint8 >() - begin< uint8 >() );
652  // obtain required size in bytes
653  const size_type _requiredSize = sizeof( T ) * requiredCount;
654 
655  // reallocate if necessary
656  if( _index + _requiredSize > capacity() )
657  _Reallocate( _index + _requiredSize );
658  }
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
void _Reallocate(size_type requiredSize)
Reallocates buffer.
Definition: Buffer.h:763
size_type capacity() const
Definition: Buffer.h:612

Here is the call graph for this function:

template<typename T >
void Buffer::Resize ( size_type  requiredCount,
const uint8 fill = 0 
)
inline

Resizes buffer.

Changes size of buffer to hold requiredCount number of elements, possibly reallocating it.

Parameters
[in]requiredCountThe number of elements to hold.
[in]fillDuring buffer expansion the gap will be filled by this value.

Definition at line 670 of file Buffer.h.

Referenced by TCPConnection::RecvData().

671  {
672  // Resize at beginning
673  ResizeAt< T >( begin< T >(), requiredCount, fill );
674  }

Here is the caller graph for this function:

template<typename T >
void Buffer::ResizeAt ( const_iterator< T >  index,
size_type  requiredCount,
const uint8 fill = 0 
)
inline

Resizes buffer.

Changes size of buffer to hold requiredCount number of elements, counting from index, possibly reallocating it.

Parameters
[in]indexThe point at which the buffer should be resized.
[in]requiredCountThe number of elements to hold.
[in]fillDuring buffer expansion the gap will be filled by this value.

Definition at line 688 of file Buffer.h.

References mBuffer, and size().

Referenced by DeflateData(), InflateData(), and EVETCPConnection::QueueRep().

689  {
690  // make sure we're not going off the bounds
691  assert( index <= end< T >() );
692 
693  // keep old size
694  const size_type _oldSize = size();
695  // do actual resize
696  _ResizeAt< T >( index, requiredCount );
697 
698  // turn iterator into byte offset
699  const size_type _index = ( index.template As< uint8 >() - begin< uint8 >() );
700  // obtain required size in bytes
701  const size_type _requiredSize = sizeof( T ) * requiredCount;
702 
703  // has a gap been created?
704  if( _index + _requiredSize > _oldSize )
705  // fill it with value
706  memset( &mBuffer[ _oldSize ], fill, _index + _requiredSize - _oldSize );
707  }
size_t size_type
Typedef for size type.
Definition: Buffer.h:44
uint8 * mBuffer
Pointer to start of buffer.
Definition: Buffer.h:711
size_type size() const
Definition: Buffer.h:610

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

uint8* Buffer::mBuffer
protected

Pointer to start of buffer.

Definition at line 711 of file Buffer.h.

Referenced by AssignAt(), AssignSeqAt(), Buffer::const_iterator< uint8 >::operator*(), ResizeAt(), and ~Buffer().

size_type Buffer::mCapacity
protected

Current capacity of buffer, in bytes.

Definition at line 715 of file Buffer.h.

Referenced by capacity().

size_type Buffer::mSize
protected

Current size of buffer, in bytes.

Definition at line 713 of file Buffer.h.

Referenced by size().


The documentation for this class was generated from the following file: