xorp

AlignData< A > Class Template Reference

A template class for aligning buffer data with a particular data type. More...

#include <utils.hh>

List of all members.

Public Member Functions

 AlignData (const vector< uint8_t > &buffer)
 Constructor.
 ~AlignData ()
 Destructor.
const A * payload () const
 Get the aligned payload from the beginning of the buffer.
const A * payload_by_offset (size_t offset) const
 Get the aligned payload by given offset from the beginning of the buffer.

Private Attributes

void * _data
const void * _const_data
const A * _payload

Static Private Attributes

static const bool _data_is_copied = false

Detailed Description

template<typename A>
class AlignData< A >

A template class for aligning buffer data with a particular data type.

The technically correct solution is to allocate (using malloc()) new buffer and copy the original data to it. By definition, the malloc()-ed data is aligned, and therefore it can be casted to the desired type.

The more efficient solution (but probably technically incorrect), is to assume that the first byte of "vector<uint8_t>" buffer is aligned similar to malloc()-ed data, and therefore it can be casted to the desired type without creating a copy of it.

The desired behavior can be chosen by setting the AlignData::_data_is_copied constant to true or false. Note that the constant is predefined for all AlignData instances. If necessary, the constant can become a variable that can have different value for each AlignData instance.


Constructor & Destructor Documentation

template<typename A >
AlignData< A >::AlignData ( const vector< uint8_t > &  buffer) [inline]

Constructor.

Parameters:
bufferthe buffer with the data.

Member Function Documentation

template<typename A >
const A* AlignData< A >::payload ( ) const [inline]

Get the aligned payload from the beginning of the buffer.

Returns:
the aligned payload from the beginning of the buffer.
template<typename A >
const A* AlignData< A >::payload_by_offset ( size_t  offset) const [inline]

Get the aligned payload by given offset from the beginning of the buffer.

Note that the given offset itself is suppose to point to aligned location.

Parameters:
offsetthe offset from the beginning of the buffer.
Returns:
the aligned payload by given offset from the beginning of the buffer.

The documentation for this class was generated from the following file:
 All Classes Namespaces Functions Variables Typedefs Enumerations