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.
| AlignData (const vector<uint8_t>& buffer)
|buffer||the buffer with the data.|
| ~AlignData ()
|const A* payload ()
Get the aligned payload from the beginning of the buffer.
Returns: the aligned payload from the beginning of the buffer.
|const A* payload_by_offset (size_t offset)
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.
|offset||the offset from the beginning of the buffer.|
Returns: the aligned payload by given offset from the beginning of the buffer.