LogCabin
|
A container for opaque data. More...
#include <Buffer.h>
Public Types | |
typedef void(* | Deleter )(void *data) |
A deleter is a function that can free the memory contained in the Buffer. | |
Public Member Functions | |
Buffer () | |
Default constructor. | |
Buffer (void *data, uint64_t length, Deleter deleter) | |
Construct a non-empty buffer. | |
Buffer (Buffer &&other) | |
Move constructor. | |
~Buffer () | |
Destructor. | |
Buffer & | operator= (Buffer &&other) |
Move assignment. | |
void * | getData () |
Return a pointer to the first byte of data. | |
const void * | getData () const |
Return a pointer to the first byte of data (const variant). | |
uint64_t | getLength () const |
Return the number of bytes that make up the data. | |
void | setData (void *data, uint64_t length, Deleter deleter) |
Replace the data contained in this Buffer. | |
void | reset () |
Empty the Buffer. | |
Static Public Member Functions | |
template<typename T > | |
static void | deleteObjectFn (void *data) |
A Deleter that uses C++'s delete keyword. | |
template<typename T > | |
static void | deleteArrayFn (void *data) |
A Deleter that uses C++'s array delete[] keyword. | |
Private Member Functions | |
Buffer (const Buffer &) | |
Buffer & | operator= (const Buffer &) |
Private Attributes | |
void * | data |
A pointer to the data or NULL if none has been set. | |
uint64_t | length |
The number of bytes that make up data or 0 if none has been set. | |
Deleter | deleter |
Describes how to release the memory for 'data' when this Buffer is destroyed or its data is replaced. |
A container for opaque data.
This makes it easy for code to operate on data without having to explicitly pass around its length and whether and how to free its memory.
typedef void(* LogCabin::Core::Buffer::Deleter)(void *data) |
A deleter is a function that can free the memory contained in the Buffer.
Examples include C's free() function for memory allocated with malloc(), and deleteObjectFn() and deleteArrayFn() defined in this class.
data | The memory that should be reclaimed. |
LogCabin::Core::Buffer::Buffer | ( | void * | data, |
uint64_t | length, | ||
Deleter | deleter | ||
) |
Construct a non-empty buffer.
This is equivalent to using the default constructor and then calling setData().
data | A pointer to the first byte of data to be contained in the Buffer. |
length | The length in bytes of data. |
deleter | Describes how to release the memory for 'data' when this Buffer is destroyed or its data is replaced. NULL indicates that the caller owns the memory and guarantees its lifetime will extend beyond that of the Buffer. |
LogCabin::Core::Buffer::Buffer | ( | Buffer && | other | ) |
LogCabin::Core::Buffer::Buffer | ( | const Buffer & | ) | [private] |
static void LogCabin::Core::Buffer::deleteObjectFn | ( | void * | data | ) | [inline, static] |
A Deleter that uses C++'s delete keyword.
This should be used with data that was allocated with C++'s new keyword. For new[] and delete[], see deleteArrayFn.
data | The memory that should be reclaimed. |
T | The type of the object as it was allocated with new. |
static void LogCabin::Core::Buffer::deleteArrayFn | ( | void * | data | ) | [inline, static] |
A Deleter that uses C++'s array delete[] keyword.
This should be used with data that was allocated with C++'s array new[] keyword. For new and delete on individual objects, see deleteArray.
data | The memory that should be reclaimed. |
T | The type of each element of the array as it was allocated with new[]. |
void* LogCabin::Core::Buffer::getData | ( | ) | [inline] |
const void* LogCabin::Core::Buffer::getData | ( | ) | const [inline] |
uint64_t LogCabin::Core::Buffer::getLength | ( | ) | const [inline] |
void LogCabin::Core::Buffer::setData | ( | void * | data, |
uint64_t | length, | ||
Deleter | deleter | ||
) |
Replace the data contained in this Buffer.
The memory for the previously existing data, if any, will be reclaimed according to the previous deleter.
data | A pointer to the first byte of data to be contained in the Buffer. |
length | The length in bytes of data. |
deleter | Describes how to release the memory for 'data' when this Buffer is destroyed or its data is replaced. NULL indicates that the caller owns the memory and guarantees its lifetime will extend beyond that of the Buffer. |
void LogCabin::Core::Buffer::reset | ( | ) |
void* LogCabin::Core::Buffer::data [private] |
uint64_t LogCabin::Core::Buffer::length [private] |
Deleter LogCabin::Core::Buffer::deleter [private] |