LogCabin
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
LogCabin::Core::Buffer Class Reference

A container for opaque data. More...

#include <Buffer.h>

List of all members.

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.
Bufferoperator= (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 &)
Bufferoperator= (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.

Detailed Description

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.

Definition at line 30 of file Buffer.h.


Member Typedef Documentation

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.

Parameters:
dataThe memory that should be reclaimed.

Definition at line 40 of file Buffer.h.


Constructor & Destructor Documentation

Default constructor.

Definition at line 21 of file Buffer.cc.

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().

Parameters:
dataA pointer to the first byte of data to be contained in the Buffer.
lengthThe length in bytes of data.
deleterDescribes 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.

Definition at line 28 of file Buffer.cc.

Move constructor.

Definition at line 35 of file Buffer.cc.

Destructor.

The memory for the existing data, if any, will be reclaimed according to its deleter.

Definition at line 45 of file Buffer.cc.

LogCabin::Core::Buffer::Buffer ( const Buffer ) [private]

Member Function Documentation

template<typename T >
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.

Parameters:
dataThe memory that should be reclaimed.
Template Parameters:
TThe type of the object as it was allocated with new.

Definition at line 52 of file Buffer.h.

template<typename T >
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.

Parameters:
dataThe memory that should be reclaimed.
Template Parameters:
TThe type of each element of the array as it was allocated with new[].

Definition at line 67 of file Buffer.h.

Buffer & LogCabin::Core::Buffer::operator= ( Buffer &&  other)

Move assignment.

Definition at line 52 of file Buffer.cc.

void* LogCabin::Core::Buffer::getData ( ) [inline]

Return a pointer to the first byte of data.

Definition at line 110 of file Buffer.h.

const void* LogCabin::Core::Buffer::getData ( ) const [inline]

Return a pointer to the first byte of data (const variant).

Definition at line 115 of file Buffer.h.

uint64_t LogCabin::Core::Buffer::getLength ( ) const [inline]

Return the number of bytes that make up the data.

Definition at line 120 of file Buffer.h.

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.

Parameters:
dataA pointer to the first byte of data to be contained in the Buffer.
lengthThe length in bytes of data.
deleterDescribes 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.

Definition at line 66 of file Buffer.cc.

Empty the Buffer.

The memory for the existing data, if any, will be reclaimed according to its deleter.

Definition at line 76 of file Buffer.cc.

Buffer& LogCabin::Core::Buffer::operator= ( const Buffer ) [private]

Member Data Documentation

A pointer to the data or NULL if none has been set.

Definition at line 148 of file Buffer.h.

uint64_t LogCabin::Core::Buffer::length [private]

The number of bytes that make up data or 0 if none has been set.

Definition at line 153 of file Buffer.h.

Describes how to release the memory for 'data' when this Buffer is destroyed or its data is replaced.

This may be NULL, in which case it is not called (the memory is managed externally).

Definition at line 161 of file Buffer.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines