OpenTTD
Data Structures | Public Types | Public Member Functions | Static Public Attributes
CBlobT< T > Class Template Reference

Blob - simple dynamic T array. More...

#include <blob.hpp>

Inheritance diagram for CBlobT< T >:
ByteBlob

Data Structures

struct  OnTransfer

Public Types

typedef ByteBlob base

Public Member Functions

 CBlobT ()
 Default constructor - makes new Blob ready to accept any data.
 CBlobT (const OnTransfer &ot)
 Take ownership constructor.
 ~CBlobT ()
 Destructor - ensures that allocated memory (if any) is freed.
void CheckIdx (size_t index) const
 Check the validity of item index (only in debug mode)
T * Data ()
 Return pointer to the first data item - non-const version.
const T * Data () const
 Return pointer to the first data item - const version.
T * Data (size_t index)
 Return pointer to the index-th data item - non-const version.
const T * Data (size_t index) const
 Return pointer to the index-th data item - const version.
size_t Size () const
 Return number of items in the Blob.
size_t MaxSize () const
 Return total number of items that can fit in the Blob without buffer reallocation.
size_t GetReserve () const
 Return number of additional items that can fit in the Blob without buffer reallocation.
T * GrowSizeNC (size_t num_items)
 Grow number of data items in Blob by given number - doesn't construct items.
T * MakeFreeSpace (size_t num_items)
 Ensures that given number of items can be added to the end of Blob.
OnTransfer Transfer ()
- Public Member Functions inherited from ByteBlob
 ByteBlob ()
 default constructor - initializes empty blob
 ByteBlob (const ByteBlob &src)
 copy constructor
 ByteBlob (BlobHeader *const &src)
 move constructor - take ownership of blob data
 ~ByteBlob ()
 destructor
bool IsEmpty () const
 return true if blob doesn't contain valid data
size_t Length () const
 return the number of valid data bytes in the blob
size_t Capacity () const
 return the current blob capacity in bytes
byte * Begin ()
 return pointer to the first byte of data - non-const version
const byte * Begin () const
 return pointer to the first byte of data - const version
void Clear ()
 invalidate blob's data - doesn't free buffer
void Free ()
 free the blob's memory
void AppendRaw (const void *p, size_t num_bytes)
 append new bytes at the end of existing data bytes - reallocates if necessary
void AppendRaw (const ByteBlob &src)
 append bytes from given source blob to the end of existing data bytes - reallocates if necessary
byte * Prepare (size_t num_bytes)
 Reallocate if there is no free space for num_bytes bytes.
byte * Append (size_t num_bytes)
 Increase Length() by num_bytes.
void SmartAlloc (size_t new_size)
 reallocate blob data if needed
void FixTail () const
 fixing the four bytes at the end of blob data - useful when blob is used to hold string

Static Public Attributes

static const size_t type_size = sizeof(T)
- Static Public Attributes inherited from ByteBlob
static const size_t tail_reserve = 4
 four extra bytes will be always allocated and zeroed at the end
static const size_t header_size = sizeof(BlobHeader)

Additional Inherited Members

- Protected Member Functions inherited from ByteBlob
void InitEmpty ()
 initialize the empty blob
void Init (BlobHeader *src)
 initialize blob by attaching it to the given header followed by data
BlobHeaderHdr ()
 blob header accessor - use it rather than using the pointer arithmetics directly - non-const version
const BlobHeaderHdr () const
 blob header accessor - use it rather than using the pointer arithmetics directly - const version
size_t & LengthRef ()
 return reference to the actual blob size - used when the size needs to be modified
- Static Protected Member Functions inherited from ByteBlob
static BlobHeaderRawAlloc (size_t num_bytes)
 all allocation should happen here
static BlobHeaderZero ()
 Return header pointer to the static BlobHeader with both items and capacity containing zero.
static size_t AllocPolicy (size_t min_alloc)
 simple allocation policy - can be optimized later
static void RawFree (BlobHeader *p)
 all deallocations should happen here
- Protected Attributes inherited from ByteBlob
union {
   byte *   data
 ptr to the first byte of data
   BlobHeader *   header
 ptr just after the BlobHeader holding items and capacity
}; 
 type used as class member

Detailed Description

template<typename T>
class CBlobT< T >

Blob - simple dynamic T array.

T (template argument) is a placeholder for any type. T can be any integral type, pointer, or structure. Using Blob instead of just plain C array simplifies the resource management in several ways:

  1. When adding new item(s) it automatically grows capacity if needed.
  2. When variable of type Blob comes out of scope it automatically frees the data buffer.
  3. Takes care about the actual data size (number of used items).
  4. Dynamically constructs only used items (as opposite of static array which constructs all items)

Definition at line 308 of file blob.hpp.

Member Function Documentation

template<typename T>
T* CBlobT< T >::MakeFreeSpace ( size_t  num_items)
inline

Ensures that given number of items can be added to the end of Blob.

Returns pointer to the first free (unused) item

Definition at line 411 of file blob.hpp.


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