GRUTinizer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Private Member Functions | Private Attributes | List of all members
TSmartBuffer Class Reference

Holds a char buffer and manages its memory. More...

#include <TSmartBuffer.h>

Public Member Functions

 TSmartBuffer ()
 Constructs an empty TSmartBuffer.
 TSmartBuffer (char *buffer, size_t size)
 Constructs a TSmartBuffer from a C-style array.
 ~TSmartBuffer ()
 Destructs the TSmartBuffer, free-ing the array, if necessary.
 TSmartBuffer (const TSmartBuffer &other)
 Copy constructor.
TSmartBufferoperator= (TSmartBuffer other)
 Assignment operator.
 TSmartBuffer (TSmartBuffer &&other)
 Move constructor.
void Clear ()
 Clears the object.
void SetBuffer (char *buffer, size_t size)
 Sets the current buffer.
const char * GetData () const
 Returns the data pointer.
size_t GetSize () const
 Returns the size of the array.
TSmartBuffer BufferSubset (size_t pos, size_t length=-1) const
 Returns a subset of the current array.
void Advance (size_t dist)
 Advances the pointer by the amount given.
void Print (Option_t *opt="") const
 Prints a short description of the TSmartBuffer.

Private Member Functions

void swap (TSmartBuffer &other)
 Swaps the contents of two TSmartBuffers.
 ClassDef (TSmartBuffer, 0)

Private Attributes

char * fAllocatedLocation
 The pointer that was allocated by malloc().
char * fData
 The pointer to the current data.
size_t fSize
 The size of the current subset of the data.
std::atomic_int * fReferenceCount
 The location of the reference count.

Detailed Description

Holds a char buffer and manages its memory.

TSmartBuffer holds a char buffer, and deletes it at the appropriate time. This is done in a threadsafe manner. Subsets of the buffer can be made without copying the underlying C-style array. The underlying C-style array will be free'd only when no references to the buffer, nor to subsets of the buffer, exist.

TSmartBuffer is intended to be lightweight, and cheap to pass by value.

Constructor & Destructor Documentation

TSmartBuffer::TSmartBuffer ( )

Constructs an empty TSmartBuffer.

The data pointer will be set to NULL The size will be equal to zero.

TSmartBuffer::TSmartBuffer ( char *  buffer,
size_t  size 
)

Constructs a TSmartBuffer from a C-style array.

The buffer must have been allocated using malloc. The buffer must not be managed by another TSmartBuffer. Once constructed, the buffer belongs to the object, which will call free() on it accordingly.

TSmartBuffer::~TSmartBuffer ( )

Destructs the TSmartBuffer, free-ing the array, if necessary.

The TSmartBuffer will call free() on the underlying C-style array if it is the last reference to that array.

TSmartBuffer::TSmartBuffer ( const TSmartBuffer other)

Copy constructor.

TSmartBuffer::TSmartBuffer ( TSmartBuffer &&  other)

Move constructor.

Member Function Documentation

void TSmartBuffer::Advance ( size_t  dist)

Advances the pointer by the amount given.

Parameters
distThe distance by which the pointer should be advanced.

If the buffer cannot be advanced that far, the pointer will be advanced to the end of the buffer.

TSmartBuffer TSmartBuffer::BufferSubset ( size_t  pos,
size_t  length = -1 
) const

Returns a subset of the current array.

Parameters
posThe position of the start of the subset of the buffer. Should range from 0 to GetSize()-1.
lengthThe length of the subset of the buffer. If equal to -1, then the remainder of the buffer is used.

The object returned will point to a subset of the the existing buffer. The underlying array is not copied. free() will only be called on the underlying array once no references to the array or any of its subsets remain.

TSmartBuffer::ClassDef ( TSmartBuffer  ,
 
)
private
void TSmartBuffer::Clear ( )

Clears the object.

The object will be left in the same state as if it had just been constructed by TSmartBuffer(). This does not need to be called prior to the object begin destructed.

const char* TSmartBuffer::GetData ( ) const
inline

Returns the data pointer.

If the object does not currently hold a buffer, returns NULL. Otherwise, the result is the location of the data buffer. Reads from the location of GetData() to GetData()+GetSize()-1 are valid.

size_t TSmartBuffer::GetSize ( ) const
inline

Returns the size of the array.

The size returned is the size of the current buffer. This may not correspond to the size of the buffer passed in, depending on the use of BufferSubset.

TSmartBuffer & TSmartBuffer::operator= ( TSmartBuffer  other)

Assignment operator.

void TSmartBuffer::Print ( Option_t *  opt = "") const

Prints a short description of the TSmartBuffer.

void TSmartBuffer::SetBuffer ( char *  buffer,
size_t  size 
)

Sets the current buffer.

The object will be left in the same state as if it had just been constructed by TSmartBuffer(buffer, size).

void TSmartBuffer::swap ( TSmartBuffer other)
private

Swaps the contents of two TSmartBuffers.

For internal use only.

Member Data Documentation

char* TSmartBuffer::fAllocatedLocation
private

The pointer that was allocated by malloc().

free() will be called on this location only when the last reference to it is deleted. All buffer subsets share this value.

char* TSmartBuffer::fData
private

The pointer to the current data.

std::atomic_int* TSmartBuffer::fReferenceCount
private

The location of the reference count.

size_t TSmartBuffer::fSize
private

The size of the current subset of the data.


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