GRUTinizer
|
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. | |
TSmartBuffer & | operator= (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. |
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.
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.
void TSmartBuffer::Advance | ( | size_t | dist | ) |
Advances the pointer by the amount given.
dist | The 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.
pos | The position of the start of the subset of the buffer. Should range from 0 to GetSize()-1. |
length | The 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.
|
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.
|
inline |
|
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).
|
private |
Swaps the contents of two TSmartBuffers.
For internal use only.
|
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.
|
private |
The pointer to the current data.
|
private |
The location of the reference count.
|
private |
The size of the current subset of the data.