2451 lines
79 KiB
C++
2451 lines
79 KiB
C++
/**
|
|
* @file include/ActivMiddleware/ActivBase/MessageBuilder.h
|
|
*
|
|
* @brief Header file for the message builder class.
|
|
*/
|
|
|
|
#if (!defined ACTIV_BASE_MESSAGE_BUILDER_H)
|
|
#define ACTIV_BASE_MESSAGE_BUILDER_H
|
|
|
|
#include "ActivMiddleware/ActivBase/External.h"
|
|
#include "ActivMiddleware/ActivBase/MessageHandler.h"
|
|
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
|
|
|
|
#include "ActivMiddleware/Misc/Base64.h"
|
|
#include "ActivMiddleware/Misc/MemoryHelper.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(integer_traits.hpp)
|
|
#include ACTIV_BOOST_INCLUDE(move/move.hpp)
|
|
|
|
#include <cassert>
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
template<MessageHandler::Endian ENDIAN, size_t SIZE>
|
|
class MessageBuilderConstantLengthDispatch;
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Message builder class.
|
|
*/
|
|
class MessageBuilder : public MessageHandler
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Helper class to reset a MessageBuilder to the MessageBuilder's length at the point of construction.
|
|
*
|
|
* Usage:
|
|
*
|
|
* ScopedCleanup<ResetLengthHelper> scopedResetLength(messageBuilder);
|
|
* if (STATUS_CODE_SUCCESS != messageBuilder.Append(...)) return;
|
|
* if (STATUS_CODE_SUCCESS != messageBuilder.Append(...)) return;
|
|
* ...
|
|
* // Message built successfully.
|
|
* scopedResetLength.Clear();
|
|
*/
|
|
class ResetLengthHelper
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param messageBuilder
|
|
*/
|
|
ResetLengthHelper(MessageBuilder &messageBuilder);
|
|
|
|
/**
|
|
* @brief Reset length.
|
|
*/
|
|
void operator()();
|
|
|
|
private:
|
|
MessageBuilder &m_messageBuilder;
|
|
size_t m_originalLength;
|
|
};
|
|
|
|
/**
|
|
* @brief Default constructor.
|
|
*
|
|
* @param initialBufferSize the initial size of the message buffer.
|
|
* @param shouldAdjustBufferSize indicates whether the size of the message buffer should be adjusted (if required).
|
|
* @param messageVersion optional version.
|
|
*
|
|
* @throw StatusCodeException
|
|
*/
|
|
MessageBuilder(const size_t initialBufferSize = 0, const bool shouldAdjustBufferSize = true,
|
|
const MessageVersion messageVersion = MESSAGE_VERSION_UNDEFINED) :
|
|
MessageHandler(messageVersion),
|
|
m_pBuffer(ACTIV_NULLPTR),
|
|
m_bufferSize(0),
|
|
m_pTail(0),
|
|
m_pBufferEnd(0),
|
|
m_shouldAdjustBufferSize(shouldAdjustBufferSize),
|
|
m_pStaticBuffer(ACTIV_NULLPTR),
|
|
m_staticBufferSize(0)
|
|
{
|
|
if (0 != initialBufferSize)
|
|
{
|
|
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, != , AdjustBufferSize(initialBufferSize));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pBuffer the address of the message buffer.
|
|
* @param bufferSize the size of the message buffer.
|
|
* @param shouldAdjustBufferSize indicates whether the size of the message buffer should be adjusted (if required).
|
|
* @param messageVersion optional version.
|
|
*/
|
|
MessageBuilder(void * const pBuffer, const size_t bufferSize, const bool shouldAdjustBufferSize = true,
|
|
const MessageVersion messageVersion = MESSAGE_VERSION_UNDEFINED) :
|
|
MessageHandler(messageVersion),
|
|
m_pBuffer(static_cast<byte_t *>(pBuffer)),
|
|
m_bufferSize(bufferSize),
|
|
m_pTail(m_pBuffer),
|
|
m_pBufferEnd(m_pBuffer + m_bufferSize),
|
|
m_shouldAdjustBufferSize(shouldAdjustBufferSize),
|
|
m_pStaticBuffer(pBuffer),
|
|
m_staticBufferSize(bufferSize)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Copy constructor.
|
|
*
|
|
* @param rhs the value to assign to this object.
|
|
*
|
|
* @throw StatusCodeException
|
|
*/
|
|
MessageBuilder(const MessageBuilder &rhs) :
|
|
MessageHandler(rhs.GetMessageVersion()),
|
|
m_pBuffer(ACTIV_NULLPTR),
|
|
m_bufferSize(0),
|
|
m_pTail(0),
|
|
m_pBufferEnd(0),
|
|
m_shouldAdjustBufferSize(rhs.m_shouldAdjustBufferSize),
|
|
m_pStaticBuffer(ACTIV_NULLPTR),
|
|
m_staticBufferSize(0)
|
|
{
|
|
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, InternalAppendBuffer<false>(rhs.GetMessage(), rhs.GetLength()));
|
|
}
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param rhs the value to move to this object.
|
|
*/
|
|
MessageBuilder(ACTIV_BOOST_RV_REF(MessageBuilder) rhs) :
|
|
MessageHandler(rhs.GetMessageVersion()),
|
|
m_pBuffer(rhs.m_pBuffer),
|
|
m_bufferSize(rhs.m_bufferSize),
|
|
m_pTail(rhs.m_pTail),
|
|
m_pBufferEnd(rhs.m_pBufferEnd),
|
|
m_shouldAdjustBufferSize(rhs.m_shouldAdjustBufferSize),
|
|
m_pStaticBuffer(rhs.m_pStaticBuffer),
|
|
m_staticBufferSize(rhs.m_staticBufferSize)
|
|
{
|
|
rhs.Uninitialize();
|
|
}
|
|
|
|
//! Destructor.
|
|
~MessageBuilder();
|
|
|
|
/**
|
|
* @brief Addition operator.
|
|
*
|
|
* @param rhs the value to add to this object.
|
|
*
|
|
* @return a reference to this object.
|
|
*
|
|
* @throw StatusCodeException
|
|
*/
|
|
const MessageBuilder operator+(const MessageBuilder &rhs) const;
|
|
|
|
/**
|
|
* @brief Addition assignment operator.
|
|
*
|
|
* @param rhs the value to add to this object.
|
|
*
|
|
* @return a reference to this object.
|
|
*
|
|
* @throw StatusCodeException
|
|
*/
|
|
MessageBuilder &operator+=(const MessageBuilder &rhs);
|
|
|
|
/**
|
|
* @brief Copy assignment operator.
|
|
*
|
|
* @param rhs the value to assign to this object.
|
|
*
|
|
* @return a reference to this object.
|
|
*
|
|
* @throw StatusCodeException
|
|
*/
|
|
MessageBuilder &operator=(ACTIV_BOOST_COPY_ASSIGN_REF(MessageBuilder) rhs)
|
|
{
|
|
if (this != &rhs)
|
|
{
|
|
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, != , SetLength(0));
|
|
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, != , InternalAppendBuffer<false>(rhs.GetMessage(), rhs.GetLength()));
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Move assignment operator.
|
|
*
|
|
* @param rhs the value to move to this object.
|
|
*
|
|
* @return a reference to this object.
|
|
*/
|
|
MessageBuilder &operator=(ACTIV_BOOST_RV_REF(MessageBuilder) rhs)
|
|
{
|
|
if (this != &rhs)
|
|
{
|
|
FreeInternalBuffer();
|
|
|
|
SetMessageVersion(rhs.GetMessageVersion());
|
|
m_pBuffer = rhs.m_pBuffer;
|
|
m_bufferSize = rhs.m_bufferSize;
|
|
m_pTail = rhs.m_pTail;
|
|
m_pBufferEnd = rhs.m_pBufferEnd;
|
|
m_shouldAdjustBufferSize = rhs.m_shouldAdjustBufferSize;
|
|
m_pStaticBuffer = rhs.m_pStaticBuffer;
|
|
m_staticBufferSize = rhs.m_staticBufferSize;
|
|
|
|
rhs.Uninitialize();
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Initialize.
|
|
*
|
|
* @param initialBufferSize the initial size of the message buffer.
|
|
* @param shouldAdjustBufferSize indicates whether the size of the message buffer should be adjusted (if required).
|
|
* @param messageVersion optional version.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Initialize(const size_t initialBufferSize, const bool shouldAdjustBufferSize = true,
|
|
const MessageVersion messageVersion = MESSAGE_VERSION_UNDEFINED);
|
|
|
|
/**
|
|
* @brief Initialize.
|
|
*
|
|
* @param pBuffer the address of the message buffer.
|
|
* @param bufferSize the size of the message buffer.
|
|
* @param shouldAdjustBufferSize indicates whether the size of the message buffer should be adjusted (if required).
|
|
* @param messageVersion optional version.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
*/
|
|
StatusCode Initialize(void * const pBuffer, const size_t bufferSize, const bool shouldAdjustBufferSize = true,
|
|
const MessageVersion messageVersion = MESSAGE_VERSION_UNDEFINED);
|
|
|
|
/**
|
|
* @brief Reset.
|
|
*
|
|
* @param messageVersion optional version.
|
|
*/
|
|
void Reset(const MessageVersion messageVersion = MESSAGE_VERSION_UNDEFINED);
|
|
|
|
/**
|
|
* @brief Get the address of the message.
|
|
*
|
|
* @return the address of the message.
|
|
*/
|
|
const void *GetMessage() const;
|
|
|
|
/**
|
|
* @brief Get the length of the message.
|
|
*
|
|
* @return the length of the message.
|
|
*/
|
|
size_t GetLength() const;
|
|
|
|
/**
|
|
* @brief Set the length of the message.
|
|
*
|
|
* Can be used to truncate a message.
|
|
*
|
|
* @param length the length of the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_PARAMETER @a length is greater than the current length of the message.
|
|
*/
|
|
StatusCode SetLength(const size_t length);
|
|
|
|
/**
|
|
* @brief Get the message buffer size.
|
|
*
|
|
* @return the size of the message buffer.
|
|
*/
|
|
size_t GetBufferSize() const;
|
|
|
|
/**
|
|
* @brief Set the message buffer size.
|
|
*
|
|
* The function will fail if the provided size is smaller than the current message length.
|
|
*
|
|
* @param bufferSize the size of the message buffer.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode SetBufferSize(const size_t bufferSize);
|
|
|
|
/**
|
|
* @brief Extend the message by a number of bytes.
|
|
*
|
|
* This is similar to Append() below, except that you have no control over the bytes appended to the message; they
|
|
* will be arbitrary as a result of whatever is in the internal buffer.
|
|
*
|
|
* @param numberOfBytes number of bytes to append to the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Extend(const size_t numberOfBytes);
|
|
|
|
/**
|
|
* @brief Append a buffer.
|
|
*
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode AppendBuffer(const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Format and append a printf-style buffer.
|
|
*
|
|
* Note this differs from AppendPrintfString() in that no length is inlined in to the message; the pure formatted
|
|
* buffer is appended only.
|
|
*
|
|
* Note no NUL is appended.
|
|
*
|
|
* @param pFormat pointer to a printf-style format string.
|
|
* @param ... variable arguments for the format string.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
ACTIV_PRINTF_FUNCTION(printf, 2, 3)
|
|
ACTIV_BASE_API StatusCode AppendPrintf(const char * const pFormat, ...);
|
|
|
|
/**
|
|
* @brief Format and append a printf-style buffer.
|
|
*
|
|
* @param pFormat pointer to a printf-style format string.
|
|
* @param args variable arguments for the format string.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
ACTIV_PRINTF_FUNCTION(printf, 2, 0)
|
|
ACTIV_BASE_API StatusCode AppendPrintfV(const char * const pFormat, std::va_list args);
|
|
|
|
/**
|
|
* @brief Append the contents of another MessageBuilder to this.
|
|
*
|
|
* @param messageBuilder
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Append(const MessageBuilder &messageBuilder);
|
|
|
|
/**
|
|
* @brief Append a byte.
|
|
*
|
|
* @param value the byte to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Append(const byte_t value);
|
|
|
|
/**
|
|
* @brief Append a character.
|
|
*
|
|
* @param character the character to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Append(const char character);
|
|
|
|
/**
|
|
* @brief Append characters.
|
|
*
|
|
* @param character the character to place in the message.
|
|
* @param count the number of character to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode AppendCount(const char character, const size_t count);
|
|
|
|
/**
|
|
* @brief Append a string.
|
|
*
|
|
* @param str the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Append(const String &str);
|
|
|
|
/**
|
|
* @brief Append a c-string.
|
|
*
|
|
* The terminating nul in @a pStr is not stored in the message.
|
|
*
|
|
* @param pStr the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Append(const char * const pStr);
|
|
|
|
/**
|
|
* @brief Format and append a printf-style string.
|
|
*
|
|
* Note this differs from AppendPrintf() in that a length is inlined in to the message in a similar vein to
|
|
* the other string Append()s (and therefore a string appended with this function is deserializable with
|
|
* MessageValidator::ValidateString()).
|
|
*
|
|
* Note no NUL is appended.
|
|
*
|
|
* @param pFormat pointer to a printf-style format string.
|
|
* @param ... variable arguments for the format string.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
ACTIV_PRINTF_FUNCTION(printf, 2, 3)
|
|
ACTIV_BASE_API StatusCode AppendPrintfString(const char * const pFormat, ...);
|
|
|
|
/**
|
|
* @brief Append a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template <Endian ENDIAN, typename T>
|
|
StatusCode Append(const T value);
|
|
|
|
/**
|
|
* @brief Append a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param value the value to place in the message.
|
|
* @param size the number of bytes of @a value to append.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a size > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a size bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template <Endian ENDIAN, typename T>
|
|
StatusCode Append(const T value, const size_t size);
|
|
|
|
/**
|
|
* @brief Append a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* SIZE the number of bytes of @a value to append.
|
|
* T is the built-in type.
|
|
*
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a SIZE > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a SIZE bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template <Endian ENDIAN, size_t SIZE, typename T>
|
|
StatusCode Append(const T value);
|
|
|
|
/**
|
|
* @brief Append a boolean value.
|
|
*
|
|
* @param value the boolean value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode AppendBoolean(const bool value);
|
|
|
|
/**
|
|
* @brief Append a binary stream encoded as base64.
|
|
*
|
|
* BASE64_ENCODER the Base64 class to encode with.
|
|
*
|
|
* @param pData
|
|
* @param dataLength
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<typename BASE64_ENCODER>
|
|
StatusCode AppendBase64(const void * const pData, const size_t dataLength);
|
|
|
|
/**
|
|
* @brief Append a binary stream encoded as base64.
|
|
*
|
|
* BASE64_ENCODER the Base64 class to encode with.
|
|
* ITERATOR_HELPER is a class that should provide static members size_t GetLength(iterator &) and
|
|
* void *GetData(iterator &) in order to access the length and data pointer from a buffer iterator.
|
|
* ITERATOR an iterator to a buffer.
|
|
*
|
|
* @param begin iterator to the first buffer.
|
|
* @param end iterator to passed the last buffer.
|
|
* @param offset offset within the buffer range begin,end at which to start encoding.
|
|
* @param dataLength the amount of data starting at offset @a offset within the buffer range to encode.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<typename BASE64_ENCODER, typename ITERATOR_HELPER, typename ITERATOR>
|
|
StatusCode AppendBase64(const ITERATOR begin, const ITERATOR end, const size_t offset,
|
|
const size_t dataLength);
|
|
|
|
/**
|
|
* @brief Erase a buffer / binary integral value.
|
|
*
|
|
* @param offset the offset in the message from which to erase.
|
|
* @param length the length of the buffer to erase from the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
*/
|
|
StatusCode Erase(const size_t offset, const size_t length);
|
|
|
|
/**
|
|
* @brief Insert a buffer.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode InsertBuffer(const size_t offset, const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Insert a byte.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Insert(const size_t offset, const byte_t value);
|
|
|
|
/**
|
|
* @brief Insert a character.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param character the character to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Insert(const size_t offset, const char character);
|
|
|
|
/**
|
|
* @brief Insert characters.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param character the character to place in the message.
|
|
* @param count the number of characters to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode InsertCount(const size_t offset, const char character, const size_t count);
|
|
|
|
/**
|
|
* @brief Insert a string.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param str the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Insert(const size_t offset, const String &str);
|
|
|
|
/**
|
|
* @brief Insert a nul terminated string.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param pStr the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Insert(const size_t offset, const char * const pStr);
|
|
|
|
/**
|
|
* @brief Insert a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T>
|
|
StatusCode Insert(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Insert a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
* @param size the number of bytes of @a value to append.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a size > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a size bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T>
|
|
StatusCode Insert(const size_t offset, const T value, const size_t size);
|
|
|
|
/**
|
|
* @brief Insert a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* SIZE the number of bytes of @a value to append.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a SIZE > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a SIZE bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, size_t SIZE, typename T>
|
|
StatusCode Insert(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Insert a boolean value.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the boolean value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode InsertBoolean(const size_t offset, const bool value);
|
|
|
|
/**
|
|
* @brief Overwrite a buffer.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode OverwriteBuffer(const size_t offset, const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Overwrite a byte.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Overwrite(const size_t offset, const byte_t value);
|
|
|
|
/**
|
|
* @brief Overwrite a character.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param character the character to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Overwrite(const size_t offset, const char character);
|
|
|
|
/**
|
|
* @brief Overwrite characters.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param character the character to place in the message.
|
|
* @param count the number of character to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode OverwriteCount(const size_t offset, const char character, const size_t count);
|
|
|
|
/**
|
|
* @brief Overwrite a string.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param str the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Overwrite(const size_t offset, const String &str);
|
|
|
|
/**
|
|
* @brief Overwrite a nul terminated string.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param pStr the string to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode Overwrite(const size_t offset, const char * const pStr);
|
|
|
|
/**
|
|
* @brief Overwrite a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T>
|
|
StatusCode Overwrite(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Overwrite a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the value to place in the message.
|
|
* @param size the number of bytes of @a value to append.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a size > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a size bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T>
|
|
StatusCode Overwrite(const size_t offset, const T value, const size_t size);
|
|
|
|
/**
|
|
* @brief Overwrite a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* SIZE the number of bytes of @a value to append.
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER for instance if @a SIZE > sizeof(T).
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @a value will not fit in @a SIZE bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, size_t SIZE, typename T>
|
|
StatusCode Overwrite(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Overwrite a boolean value.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the boolean value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
StatusCode OverwriteBoolean(const size_t offset, const bool value);
|
|
|
|
private:
|
|
//! Free any internal buffer.
|
|
void FreeInternalBuffer();
|
|
|
|
//! Cleardown after a move-away.
|
|
void Uninitialize();
|
|
|
|
/**
|
|
* @brief Append a variable length binary integral value with a given endian ordering.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
*
|
|
* @param pValue the address of the value to place in the message.
|
|
* @param size the size of the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN>
|
|
StatusCode InternalAppendInEndian(const void * const pValue, const size_t size);
|
|
|
|
/**
|
|
* @brief Append a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
* N is the length to serialize to.
|
|
*
|
|
* @param value the value to place in the message.
|
|
* @param endian the endian to copy the value in.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @value will not fit in N bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T, size_t N>
|
|
StatusCode InternalAppendInRange(const T value);
|
|
|
|
/**
|
|
* @brief Append a built-in type.
|
|
*
|
|
* T is the built-in type.
|
|
*
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<typename T>
|
|
StatusCode InternalAppendBuiltIn(const T value);
|
|
|
|
/**
|
|
* @brief Append a buffer.
|
|
*
|
|
* @param REVERSE_BYTE_ORDER whether the byte order should be reversed.
|
|
*
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
StatusCode InternalAppendBuffer(const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Erase a buffer.
|
|
*
|
|
* @param offset the offset at which to erase in the message.
|
|
* @param length the length of the buffer to erase in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
*/
|
|
ACTIV_BASE_API StatusCode InternalEraseBuffer(const size_t offset, const size_t length);
|
|
|
|
/**
|
|
* @brief Insert a variable length binary integral value with a given endian ordering.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param pValue the address of the value to place in the message.
|
|
* @param size the size of the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN>
|
|
StatusCode InternalInsertInEndian(const size_t offset, const void * const pValue, const size_t size);
|
|
|
|
/**
|
|
* @brief Insert a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
* N is the length to serialize to.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @value will not fit in N bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T, size_t N>
|
|
StatusCode InternalInsertInRange(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Insert a built-in type.
|
|
*
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<typename T>
|
|
StatusCode InternalInsertBuiltIn(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Insert a buffer.
|
|
*
|
|
* @param REVERSE_BYTE_ORDER whether the byte order should be reversed.
|
|
*
|
|
* @param offset the offset at which to insert in the message.
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
StatusCode InternalInsertBuffer(const size_t offset, const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Overwrite a variable length binary integral value with a given endian ordering.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param pValue the address of the value to place in the message.
|
|
* @param size the size of the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN>
|
|
StatusCode InternalOverwriteInEndian(const size_t offset, const void * const pValue, const size_t size);
|
|
|
|
/**
|
|
* @brief Overwrite a built-in type.
|
|
*
|
|
* ENDIAN the endian to copy the value in.
|
|
* T is the built-in type.
|
|
* N is the length to serialize to.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_OUT_OF_RANGE if @value will not fit in N bytes.
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<Endian ENDIAN, typename T, size_t N>
|
|
StatusCode InternalOverwriteInRange(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Overwrite a built-in type.
|
|
*
|
|
* T is the built-in type.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param value the value to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<typename T>
|
|
StatusCode InternalOverwriteBuiltIn(const size_t offset, const T value);
|
|
|
|
/**
|
|
* @brief Overwrite a buffer.
|
|
*
|
|
* REVERSE_BYTE_ORDER whether the byte order should be reversed.
|
|
*
|
|
* @param offset the offset at which to overwrite in the message.
|
|
* @param pBuffer the address of the buffer to place in the message.
|
|
* @param length the length of the buffer to place in the message.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW
|
|
* @retval STATUS_CODE_INVALID_PARAMETER
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
StatusCode InternalOverwriteBuffer(const size_t offset, const void * const pBuffer, const size_t length);
|
|
|
|
/**
|
|
* @brief Adjusts the size of the message buffer dependent on shouldAdjustBufferSize passed to the constructor.
|
|
*
|
|
* @param additionalSize
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
* @retval STATUS_CODE_BUFFER_OVERFLOW shouldAdjustBufferSize was false.
|
|
*/
|
|
ACTIV_BASE_API StatusCode MaybeAdjustBufferSize(const size_t additionalSize);
|
|
|
|
/**
|
|
* @brief Adjusts the size of the message buffer.
|
|
*
|
|
* @param bufferSize the size of the message buffer.
|
|
*
|
|
* @retval STATUS_CODE_SUCCESS
|
|
* @retval STATUS_CODE_HEAP_EMPTY
|
|
*/
|
|
ACTIV_BASE_API StatusCode AdjustBufferSize(const size_t bufferSize);
|
|
|
|
/**
|
|
* @brief Get buffer size required to hold additional bytes.
|
|
*
|
|
* @param additionalSize the amount of additional space required in the message.
|
|
*
|
|
* @return The buffer size.
|
|
*/
|
|
size_t GetAdjustedBufferSize(const size_t additionalSize) const;
|
|
|
|
byte_t *m_pBuffer; ///< The address of the message buffer.
|
|
size_t m_bufferSize; ///< The size of the message buffer.
|
|
byte_t *m_pTail; ///< Current end of the built message.
|
|
byte_t *m_pBufferEnd; ///< End of the available message buffer.
|
|
|
|
bool m_shouldAdjustBufferSize; ///< Indicates whether the size of the message buffer should be adjusted.
|
|
|
|
void * m_pStaticBuffer; ///< The address of the static message buffer.
|
|
size_t m_staticBufferSize; ///< The size of the static message buffer.
|
|
|
|
ACTIV_BOOST_COPYABLE_AND_MOVABLE(MessageBuilder)
|
|
|
|
template<MessageHandler::Endian ENDIAN, size_t SIZE>
|
|
friend class MessageBuilderConstantLengthDispatch;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Class template to check a value is in the acceptable range of a variable number of bytes.
|
|
*
|
|
* Explicit specializations need to be at namespace scope.
|
|
*/
|
|
template<bool IS_SIGNED>
|
|
class MessageBuilderVariableLengthRangeCheck
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderVariableLengthRangeCheck for signed values.
|
|
template<>
|
|
class MessageBuilderVariableLengthRangeCheck<true>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param value the value to check.
|
|
* @param numberOfBytes the number of bytes to check that @a value fits in.
|
|
*
|
|
* @return true if @a value is in range.
|
|
*/
|
|
template<typename T>
|
|
bool operator()(const T value, const size_t numberOfBytes) const
|
|
{
|
|
assert(numberOfBytes <= sizeof(T));
|
|
|
|
const T max = (T(1) << ((numberOfBytes * CHAR_BIT) - 1)) - 1;
|
|
const T min = (-max) - 1;
|
|
|
|
return ((value >= min) && (value <= max));
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderVariableLengthRangeCheck for unsigned values.
|
|
template<>
|
|
class MessageBuilderVariableLengthRangeCheck<false>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param value the value to check.
|
|
* @param numberOfBytes the number of bytes to check that @a value fits in.
|
|
*
|
|
* @return true if @a value is in range.
|
|
*/
|
|
template<typename T>
|
|
bool operator()(const T value, const size_t numberOfBytes) const
|
|
{
|
|
assert(numberOfBytes <= sizeof(T));
|
|
|
|
const T max = static_cast<T>(Boost::integer_traits<T>::const_max >> ((sizeof(T) - numberOfBytes) * CHAR_BIT));
|
|
|
|
return (value <= max);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Class template to check a value is in the acceptable range of a constant number of bytes.
|
|
*
|
|
* Explicit specializations need to be at namespace scope.
|
|
*/
|
|
template<bool IS_SIGNED, size_t NUMBER_OF_BYTES>
|
|
class MessageBuilderConstLengthRangeCheck
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderConstLengthRangeCheck for signed values.
|
|
template<size_t NUMBER_OF_BYTES>
|
|
class MessageBuilderConstLengthRangeCheck<true, NUMBER_OF_BYTES>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param value the value to check.
|
|
*
|
|
* @return true if @a value is in range.
|
|
*/
|
|
template<typename T>
|
|
bool operator()(const T value) const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(NUMBER_OF_BYTES <= sizeof(T));
|
|
|
|
const T max = (T(1) << ((NUMBER_OF_BYTES * CHAR_BIT) - 1)) - 1;
|
|
const T min = (-max) - 1;
|
|
|
|
return ((value >= min) && (value <= max));
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderConstLengthRangeCheck for unsigned values.
|
|
template<size_t NUMBER_OF_BYTES>
|
|
class MessageBuilderConstLengthRangeCheck<false, NUMBER_OF_BYTES>
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param value the value to check.
|
|
*
|
|
* @return true if @a value is in range.
|
|
*/
|
|
template<typename T>
|
|
bool operator()(const T value) const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(NUMBER_OF_BYTES <= sizeof(T));
|
|
|
|
const T max = static_cast<T>(Boost::integer_traits<T>::const_max >> ((sizeof(T) - NUMBER_OF_BYTES) * CHAR_BIT));
|
|
|
|
return (value <= max);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Class template to swap the endian of a value if the requested endian does not match the local machine.
|
|
template<MessageBuilder::Endian ENDIAN>
|
|
class MessageBuilderMaybeByteSwap
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderMaybeByteSwap to not reverse the endian of a value.
|
|
template<>
|
|
class MessageBuilderMaybeByteSwap<MessageBuilder::ENDIAN_LOCAL>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
T operator()(const T value)
|
|
{
|
|
return value;
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderMaybeByteSwap to reverse the endian of a value.
|
|
template<>
|
|
class MessageBuilderMaybeByteSwap<MessageBuilder::ENDIAN_ALIEN>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
T operator()(const T value)
|
|
{
|
|
return ByteSwap(value);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Class template to copy memory and potentially reverse the byte order.
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
class MessageBuilderMaybeMemoryReverseCopy
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderMaybeByteSwap to not reverse memory when copying.
|
|
template<>
|
|
class MessageBuilderMaybeMemoryReverseCopy<false>
|
|
{
|
|
public:
|
|
void operator()(void * const pDest, const void * const pBuffer, const size_t length) const
|
|
{
|
|
MemoryCopy(pDest, pBuffer, length);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! Specialization of MessageBuilderMaybeByteSwap to reverse memory when copying.
|
|
template<>
|
|
class MessageBuilderMaybeMemoryReverseCopy<true>
|
|
{
|
|
public:
|
|
void operator()(void * const pDest, const void * const pBuffer, const size_t length) const
|
|
{
|
|
MemoryReverseCopy(pDest, pBuffer, length);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Helper for dispatching the appropriate Append/Overwrite/Insert call for a compile-time length.
|
|
*/
|
|
template<MessageHandler::Endian ENDIAN, size_t SIZE>
|
|
class MessageBuilderConstantLengthDispatch
|
|
{
|
|
public:
|
|
template<typename T>
|
|
StatusCode Append(MessageBuilder &messageBuilder, const T value) const
|
|
{
|
|
if (ACTIV_UNLIKELY((!MessageBuilderConstLengthRangeCheck<Boost::integer_traits<T>::is_signed, SIZE>()(value))))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.InternalAppendInEndian<ENDIAN>(&value, SIZE));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
messageBuilder.InternalAppendInEndian<ENDIAN>(reinterpret_cast<const byte_t *>(&value) + sizeof(T) - SIZE, SIZE));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Insert(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
if (ACTIV_UNLIKELY((!MessageBuilderConstLengthRangeCheck<Boost::integer_traits<T>::is_signed, SIZE>()(value))))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
messageBuilder.InternalInsertInEndian<ENDIAN>(offset, &value, SIZE));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.InternalInsertInEndian<ENDIAN>(offset,
|
|
reinterpret_cast<const byte_t *>(&value) + sizeof(T) - SIZE, SIZE));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Overwrite(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
if (ACTIV_UNLIKELY((!MessageBuilderConstLengthRangeCheck<Boost::integer_traits<T>::is_signed, SIZE>()(value))))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
messageBuilder.InternalOverwriteInEndian<ENDIAN>(offset, &value, SIZE));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.InternalOverwriteInEndian<ENDIAN>(offset,
|
|
reinterpret_cast<const byte_t *>(&value) + sizeof(T) - SIZE, SIZE));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageHandler::Endian ENDIAN>
|
|
class MessageBuilderConstantLengthDispatch<ENDIAN, 1>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
StatusCode Append(MessageBuilder &messageBuilder, const T value) const
|
|
{
|
|
return messageBuilder.InternalAppendInRange<ENDIAN, T, 1>(value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Insert(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalInsertInRange<ENDIAN, T, 1>(offset, value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Overwrite(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalOverwriteInRange<ENDIAN, T, 1>(offset, value);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageHandler::Endian ENDIAN>
|
|
class MessageBuilderConstantLengthDispatch<ENDIAN, 2>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
StatusCode Append(MessageBuilder &messageBuilder, const T value) const
|
|
{
|
|
return messageBuilder.InternalAppendInRange<ENDIAN, T, 2>(value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Insert(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalInsertInRange<ENDIAN, T, 2>(offset, value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Overwrite(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalOverwriteInRange<ENDIAN, T, 2>(offset, value);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageHandler::Endian ENDIAN>
|
|
class MessageBuilderConstantLengthDispatch<ENDIAN, 4>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
StatusCode Append(MessageBuilder &messageBuilder, const T value) const
|
|
{
|
|
return messageBuilder.InternalAppendInRange<ENDIAN, T, 4>(value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Insert(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalInsertInRange<ENDIAN, T, 4>(offset, value);
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Overwrite(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
return messageBuilder.InternalOverwriteInRange<ENDIAN, T, 4>(offset, value);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageHandler::Endian ENDIAN>
|
|
class MessageBuilderConstantLengthDispatch<ENDIAN, 8>
|
|
{
|
|
public:
|
|
template<typename T>
|
|
StatusCode Append(MessageBuilder &messageBuilder, const T value) const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return messageBuilder.Append<ENDIAN>(
|
|
static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Insert(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return messageBuilder.Insert<ENDIAN>(offset,
|
|
static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
}
|
|
|
|
template<typename T>
|
|
StatusCode Overwrite(MessageBuilder &messageBuilder, const size_t offset, const T value) const
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return messageBuilder.Overwrite<ENDIAN>(offset,
|
|
static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MessageBuilder::~MessageBuilder()
|
|
{
|
|
FreeInternalBuffer();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const MessageBuilder MessageBuilder::operator+(const MessageBuilder &rhs) const
|
|
{
|
|
return MessageBuilder(*this) += rhs;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MessageBuilder &MessageBuilder::operator+=(const MessageBuilder &rhs)
|
|
{
|
|
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, InternalAppendBuffer<false>(rhs.GetMessage(), rhs.GetLength()));
|
|
|
|
return *this;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Initialize(const size_t initialBufferSize, const bool shouldAdjustBufferSize,
|
|
const MessageVersion messageVersion)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, AdjustBufferSize(0));
|
|
|
|
m_pBuffer = ACTIV_NULLPTR;
|
|
m_bufferSize = 0;
|
|
m_pBufferEnd = 0;
|
|
m_shouldAdjustBufferSize = shouldAdjustBufferSize;
|
|
m_pStaticBuffer = ACTIV_NULLPTR;
|
|
m_staticBufferSize = 0;
|
|
|
|
if (0 != initialBufferSize)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, AdjustBufferSize(initialBufferSize));
|
|
}
|
|
|
|
Reset(messageVersion);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Initialize(void * const pBuffer, const size_t bufferSize, const bool shouldAdjustBufferSize,
|
|
const MessageVersion messageVersion)
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, AdjustBufferSize(0));
|
|
|
|
m_pBuffer = static_cast<byte_t *>(pBuffer);
|
|
m_bufferSize = bufferSize;
|
|
m_pBufferEnd = m_pBuffer + m_bufferSize;
|
|
m_shouldAdjustBufferSize = shouldAdjustBufferSize;
|
|
m_pStaticBuffer = pBuffer;
|
|
m_staticBufferSize = bufferSize;
|
|
|
|
Reset(messageVersion);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MessageBuilder::Reset(const MessageVersion messageVersion)
|
|
{
|
|
SetMessageVersion(messageVersion);
|
|
m_pTail = m_pBuffer;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline const void *MessageBuilder::GetMessage() const
|
|
{
|
|
return m_pBuffer;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t MessageBuilder::GetLength() const
|
|
{
|
|
return (m_pTail - m_pBuffer);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::SetLength(const size_t length)
|
|
{
|
|
byte_t * const pNewTail = m_pBuffer + length;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pTail))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline size_t MessageBuilder::GetBufferSize() const
|
|
{
|
|
return m_bufferSize;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::SetBufferSize(const size_t bufferSize)
|
|
{
|
|
if (ACTIV_UNLIKELY(bufferSize < GetLength()))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
return AdjustBufferSize(bufferSize);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Extend(const size_t numberOfBytes)
|
|
{
|
|
byte_t *pNewTail = m_pTail + numberOfBytes;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(numberOfBytes));
|
|
pNewTail = m_pTail + numberOfBytes;
|
|
}
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::AppendBuffer(const void * const pBuffer, const size_t length)
|
|
{
|
|
return InternalAppendBuffer<false>(pBuffer, length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Append(const MessageBuilder &messageBuilder)
|
|
{
|
|
return AppendBuffer(messageBuilder.GetMessage(), messageBuilder.GetLength());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Append(const byte_t value)
|
|
{
|
|
return InternalAppendBuiltIn<byte_t>(value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Append(const char character)
|
|
{
|
|
return InternalAppendBuiltIn<int8_t>(character);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::AppendCount(const char character, const size_t count)
|
|
{
|
|
byte_t *pNewTail = m_pTail + count;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(count));
|
|
pNewTail = m_pTail + count;
|
|
}
|
|
|
|
std::memset(m_pTail, character, count);
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Append(const String &str)
|
|
{
|
|
if (ACTIV_UNLIKELY(str.length() > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const StringLength length = static_cast<StringLength>(str.length());
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Append<ENDIAN_LITTLE, StringLength>(length)));
|
|
|
|
return InternalAppendBuffer<false>(str.c_str(), length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Append(const char * const pStr)
|
|
{
|
|
if (ACTIV_UNLIKELY(0 == pStr))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const size_t strLength = ::strlen(pStr);
|
|
|
|
if (ACTIV_UNLIKELY(strLength > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const StringLength length = static_cast<StringLength>(strLength);
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Append<ENDIAN_LITTLE, StringLength>(length)));
|
|
|
|
return InternalAppendBuffer<false>(pStr, strLength);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
inline StatusCode MessageBuilder::Append(const T value)
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return InternalAppendBuiltIn<Target>(static_cast<Target>(MessageBuilderMaybeByteSwap<ENDIAN>()(value)));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
StatusCode MessageBuilder::Append(const T value, const size_t size)
|
|
{
|
|
// Assert T is an integral type, as we cast it to an ACTIV integral type (or use its address).
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
|
|
if (ACTIV_UNLIKELY(size > sizeof(T)))
|
|
{
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
}
|
|
else if (ACTIV_UNLIKELY(size == sizeof(T)))
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return Append<ENDIAN>(static_cast<Target>(value));
|
|
}
|
|
|
|
switch (size)
|
|
{
|
|
case 1:
|
|
return InternalAppendInRange<ENDIAN, T, 1>(value);
|
|
|
|
case 2:
|
|
return InternalAppendInRange<ENDIAN, T, 2>(value);
|
|
|
|
case 4:
|
|
return InternalAppendInRange<ENDIAN, T, 4>(value);
|
|
|
|
case 8:
|
|
// GCC throws up silly warnings if we go the range-check route with a 64-bit target, so avoid the check
|
|
// and assert the source type isn't bigger than 64-bits...
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return Append<ENDIAN>(static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (ACTIV_UNLIKELY(!MessageBuilderVariableLengthRangeCheck<Boost::integer_traits<T>::is_signed>()(value, size)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, InternalAppendInEndian<ENDIAN>(&value, size));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
InternalAppendInEndian<ENDIAN>(reinterpret_cast<const byte_t *>(&value) + sizeof(T) - size, size));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <MessageBuilder::Endian ENDIAN, size_t SIZE, typename T>
|
|
inline StatusCode MessageBuilder::Append(const T value)
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
ACTIV_BOOST_STATIC_ASSERT(SIZE <= sizeof(T));
|
|
|
|
return MessageBuilderConstantLengthDispatch<ENDIAN, SIZE>().Append(*this, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::AppendBoolean(const bool value)
|
|
{
|
|
return InternalAppendBuiltIn<uint8_t>(static_cast<uint8_t>(value ? 1 : 0));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename BASE64_ENCODER>
|
|
StatusCode MessageBuilder::AppendBase64(const void * const pData, const size_t dataLength)
|
|
{
|
|
const size_t maxEncodedLength = BASE64_ENCODER::GetMaximumEncodedLength(dataLength);
|
|
byte_t *pNewTail = m_pTail + maxEncodedLength;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(maxEncodedLength));
|
|
pNewTail = m_pTail + maxEncodedLength;
|
|
}
|
|
|
|
size_t encodedLength = m_bufferSize - GetLength();
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
BASE64_ENCODER::Encode(pData, dataLength, reinterpret_cast<char *>(m_pTail), encodedLength));
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename BASE64_ENCODER, typename ITERATOR_HELPER, typename ITERATOR>
|
|
StatusCode MessageBuilder::AppendBase64(const ITERATOR begin, const ITERATOR end, const size_t offset,
|
|
const size_t dataLength)
|
|
{
|
|
const size_t maxEncodedLength = BASE64_ENCODER::GetMaximumEncodedLength(dataLength);
|
|
byte_t *pNewTail = m_pTail + maxEncodedLength;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(maxEncodedLength));
|
|
pNewTail = m_pTail + maxEncodedLength;
|
|
}
|
|
|
|
size_t encodedLength = m_bufferSize - GetLength();
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
(BASE64_ENCODER::template Encode<ITERATOR_HELPER, ITERATOR>(begin, end, offset, dataLength,
|
|
reinterpret_cast<char *>(m_pTail), encodedLength)));
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Erase(const size_t offset, const size_t length)
|
|
{
|
|
return InternalEraseBuffer(offset, length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::InsertBuffer(const size_t offset, const void * const pBuffer, const size_t length)
|
|
{
|
|
return InternalInsertBuffer<false>(offset, pBuffer, length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const byte_t value)
|
|
{
|
|
return InternalInsertBuiltIn<byte_t>(offset, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const char character)
|
|
{
|
|
return InternalInsertBuiltIn<int8_t>(offset, character);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::InsertCount(const size_t offset, const char character, const size_t count)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
byte_t *pNewTail = m_pTail + count;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(count));
|
|
pNewTail = m_pTail + count;
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
std::memmove(reinterpret_cast<byte_t *>(pDest) + count, pDest, originalLength - offset);
|
|
std::memset(pDest, character, count);
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const String &str)
|
|
{
|
|
if (ACTIV_UNLIKELY(str.length() > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
StringLength length = static_cast<StringLength>(str.length());
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Insert<ENDIAN_LITTLE, StringLength>(offset, length)));
|
|
|
|
return InternalInsertBuffer<false>(offset + sizeof(length), str.c_str(), length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const char * const pStr)
|
|
{
|
|
if (ACTIV_UNLIKELY(0 == pStr))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const size_t strLength = ::strlen(pStr);
|
|
|
|
if (ACTIV_UNLIKELY(strLength > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const StringLength length = static_cast<StringLength>(strLength);
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Insert<ENDIAN_LITTLE, StringLength>(offset, length)));
|
|
|
|
return InternalInsertBuffer<false>(offset + sizeof(length), pStr, strLength);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const T value)
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return InternalInsertBuiltIn<Target>(offset, static_cast<Target>(MessageBuilderMaybeByteSwap<ENDIAN>()(value)));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
StatusCode MessageBuilder::Insert(const size_t offset, const T value, const size_t size)
|
|
{
|
|
// Assert T is an integral type, as we cast it to an ACTIV integral type (or use its address).
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
|
|
if (ACTIV_UNLIKELY(size > sizeof(T)))
|
|
{
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
}
|
|
else if (ACTIV_UNLIKELY(size == sizeof(T)))
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return Insert<ENDIAN>(offset, static_cast<Target>(value));
|
|
}
|
|
|
|
switch (size)
|
|
{
|
|
case 1:
|
|
return InternalInsertInRange<ENDIAN, T, 1>(offset, value);
|
|
|
|
case 2:
|
|
return InternalInsertInRange<ENDIAN, T, 2>(offset, value);
|
|
|
|
case 4:
|
|
return InternalInsertInRange<ENDIAN, T, 4>(offset, value);
|
|
|
|
case 8:
|
|
// GCC throws up silly warnings if we go the range-check route with a 64-bit target, so avoid the check
|
|
// and assert the source type isn't bigger than 64-bits...
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return Insert<ENDIAN>(offset,
|
|
static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (ACTIV_UNLIKELY(!MessageBuilderVariableLengthRangeCheck<Boost::integer_traits<T>::is_signed>()(value, size)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, InternalInsertInEndian<ENDIAN>(offset, &value, size));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
InternalInsertInEndian<ENDIAN>(offset, reinterpret_cast<const byte_t *>(&value) + sizeof(T) - size, size));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template <MessageBuilder::Endian ENDIAN, size_t SIZE, typename T>
|
|
inline StatusCode MessageBuilder::Insert(const size_t offset, const T value)
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
ACTIV_BOOST_STATIC_ASSERT(SIZE <= sizeof(T));
|
|
|
|
return MessageBuilderConstantLengthDispatch<ENDIAN, SIZE>().Insert(*this, offset, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::InsertBoolean(const size_t offset, const bool value)
|
|
{
|
|
return InternalInsertBuiltIn<uint8_t>(offset, static_cast<uint8_t>(value ? 1 : 0));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::OverwriteBuffer(const size_t offset, const void * const pBuffer, const size_t length)
|
|
{
|
|
return InternalOverwriteBuffer<false>(offset, pBuffer, length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Overwrite(const size_t offset, const byte_t value)
|
|
{
|
|
return InternalOverwriteBuiltIn<byte_t>(offset, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Overwrite(const size_t offset, const char character)
|
|
{
|
|
return InternalOverwriteBuiltIn<int8_t>(offset, character);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::OverwriteCount(const size_t offset, const char character, const size_t count)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
if (ACTIV_UNLIKELY((offset + count) > originalLength))
|
|
{
|
|
const size_t additionalLength = offset + count - originalLength;
|
|
byte_t *pNewTail = m_pTail + additionalLength;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(additionalLength));
|
|
pNewTail = m_pTail + additionalLength;
|
|
}
|
|
|
|
m_pTail = pNewTail;
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
|
|
std::memset(pDest, character, count);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Overwrite(const size_t offset, const String &str)
|
|
{
|
|
if (ACTIV_UNLIKELY(str.length() > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
StringLength length = static_cast<StringLength>(str.length());
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Overwrite<ENDIAN_LITTLE, StringLength>(offset, length)));
|
|
|
|
return InternalOverwriteBuffer<false>(offset + sizeof(length), str.c_str(), length);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::Overwrite(const size_t offset, const char * const pStr)
|
|
{
|
|
if (ACTIV_UNLIKELY(0 == pStr))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const size_t strLength = ::strlen(pStr);
|
|
|
|
if (ACTIV_UNLIKELY(strLength > Boost::integer_traits<StringLength>::const_max))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
const StringLength length = static_cast<StringLength>(strLength);
|
|
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (Overwrite<ENDIAN_LITTLE, StringLength>(offset, length)));
|
|
|
|
return InternalOverwriteBuffer<false>(offset + sizeof(length), pStr, strLength);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
inline StatusCode MessageBuilder::Overwrite(const size_t offset, const T value)
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return InternalOverwriteBuiltIn<Target>(offset, static_cast<Target>(MessageBuilderMaybeByteSwap<ENDIAN>()(value)));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T>
|
|
StatusCode MessageBuilder::Overwrite(const size_t offset, const T value, const size_t size)
|
|
{
|
|
// Assert T is an integral type, as we cast it to an ACTIV integral type (or use its address).
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
|
|
if (ACTIV_UNLIKELY(size > sizeof(T)))
|
|
{
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
}
|
|
else if (ACTIV_UNLIKELY(size == sizeof(T)))
|
|
{
|
|
typedef typename TypeToIntegralType<T>::Type Target;
|
|
|
|
return Overwrite<ENDIAN>(offset, static_cast<Target>(value));
|
|
}
|
|
|
|
switch (size)
|
|
{
|
|
case 1:
|
|
return InternalOverwriteInRange<ENDIAN, T, 1>(offset, value);
|
|
|
|
case 2:
|
|
return InternalOverwriteInRange<ENDIAN, T, 2>(offset, value);
|
|
|
|
case 4:
|
|
return InternalOverwriteInRange<ENDIAN, T, 4>(offset, value);
|
|
|
|
case 8:
|
|
// GCC throws up silly warnings if we go the range-check route with a 64-bit target, so avoid the check
|
|
// and assert the source type isn't bigger than 64-bits...
|
|
ACTIV_BOOST_STATIC_ASSERT(sizeof(T) <= 8);
|
|
return Overwrite<ENDIAN>(offset,
|
|
static_cast<typename LengthToIntegralType<8, Boost::integer_traits<T>::is_signed>::Type>(value));
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
if (ACTIV_UNLIKELY(!MessageBuilderVariableLengthRangeCheck<Boost::integer_traits<T>::is_signed>()(value, size)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
#if (defined ACTIV_LITTLE_ENDIAN)
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, InternalOverwriteInEndian<ENDIAN>(offset, &value, size));
|
|
#else
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=,
|
|
InternalOverwriteInEndian<ENDIAN>(offset, reinterpret_cast<const byte_t *>(&value) + sizeof(T) - size, size));
|
|
#endif
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, size_t SIZE, typename T>
|
|
StatusCode MessageBuilder::Overwrite(const size_t offset, const T value)
|
|
{
|
|
ACTIV_BOOST_STATIC_ASSERT(Boost::is_integral<T>::value);
|
|
ACTIV_BOOST_STATIC_ASSERT(SIZE <= sizeof(T));
|
|
|
|
return MessageBuilderConstantLengthDispatch<ENDIAN, SIZE>().Overwrite(*this, offset, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline StatusCode MessageBuilder::OverwriteBoolean(const size_t offset, const bool value)
|
|
{
|
|
return InternalOverwriteBuiltIn<uint8_t>(offset, static_cast<uint8_t>(value ? 1 : 0));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MessageBuilder::FreeInternalBuffer()
|
|
{
|
|
if ((m_pStaticBuffer != m_pBuffer) && (ACTIV_NULLPTR != m_pBuffer))
|
|
std::free(m_pBuffer);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MessageBuilder::Uninitialize()
|
|
{
|
|
m_pBuffer = ACTIV_NULLPTR;
|
|
m_bufferSize = 0;
|
|
m_pTail = ACTIV_NULLPTR;
|
|
m_pBufferEnd = ACTIV_NULLPTR;
|
|
m_pStaticBuffer = ACTIV_NULLPTR;
|
|
m_staticBufferSize = 0;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename MessageBuilder::Endian ENDIAN>
|
|
inline StatusCode MessageBuilder::InternalAppendInEndian(const void * const pValue, const size_t size)
|
|
{
|
|
return InternalAppendBuffer<ENDIAN != ENDIAN_LOCAL>(pValue, size);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T, size_t N>
|
|
inline StatusCode MessageBuilder::InternalAppendInRange(const T value)
|
|
{
|
|
typedef typename LengthToIntegralType<N, Boost::integer_traits<T>::is_signed>::Type Target;
|
|
|
|
if (ACTIV_UNLIKELY(!InRangeOfIntegralType<Target>()(value)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
return Append<ENDIAN>(static_cast<Target>(value));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
ACTIV_FORCE_INLINE StatusCode MessageBuilder::InternalAppendBuiltIn(const T value)
|
|
{
|
|
byte_t *pNewTail = m_pTail + sizeof(T);
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(sizeof(T)));
|
|
pNewTail = m_pTail + sizeof(T);
|
|
}
|
|
|
|
CopyValueToUnalignedPointer<T>(m_pTail, value);
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
inline StatusCode MessageBuilder::InternalAppendBuffer(const void * const pBuffer, const size_t length)
|
|
{
|
|
byte_t *pNewTail = m_pTail + length;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(length));
|
|
pNewTail = m_pTail + length;
|
|
}
|
|
|
|
MessageBuilderMaybeMemoryReverseCopy<REVERSE_BYTE_ORDER>()(m_pTail, pBuffer, length);
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN>
|
|
inline StatusCode MessageBuilder::InternalInsertInEndian(const size_t offset, const void * const pValue, const size_t size)
|
|
{
|
|
return InternalInsertBuffer<ENDIAN != ENDIAN_LOCAL>(offset, pValue, size);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T, size_t N>
|
|
inline StatusCode MessageBuilder::InternalInsertInRange(const size_t offset, const T value)
|
|
{
|
|
typedef typename LengthToIntegralType<N, Boost::integer_traits<T>::is_signed>::Type Target;
|
|
|
|
if (ACTIV_UNLIKELY(!InRangeOfIntegralType<Target>()(value)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
return Insert<ENDIAN>(offset, static_cast<Target>(value));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline StatusCode MessageBuilder::InternalInsertBuiltIn(const size_t offset, const T value)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
byte_t *pNewTail = m_pTail + sizeof(T);
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(sizeof(T)));
|
|
pNewTail = m_pTail + sizeof(T);
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
|
|
std::memmove(reinterpret_cast<byte_t *>(pDest) + sizeof(T), pDest, originalLength - offset);
|
|
CopyValueToUnalignedPointer<T>(pDest, value);
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
inline StatusCode MessageBuilder::InternalInsertBuffer(const size_t offset, const void * const pBuffer, const size_t length)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
byte_t *pNewTail = m_pTail + length;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(length));
|
|
pNewTail = m_pTail + length;
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
|
|
std::memmove(reinterpret_cast<byte_t *>(pDest) + length, pDest, originalLength - offset);
|
|
MessageBuilderMaybeMemoryReverseCopy<REVERSE_BYTE_ORDER>()(pDest, pBuffer, length);
|
|
|
|
m_pTail = pNewTail;
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN>
|
|
inline StatusCode MessageBuilder::InternalOverwriteInEndian(const size_t offset, const void * const pValue, const size_t size)
|
|
{
|
|
return InternalOverwriteBuffer<ENDIAN != ENDIAN_LOCAL>(offset, pValue, size);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<MessageBuilder::Endian ENDIAN, typename T, size_t N>
|
|
inline StatusCode MessageBuilder::InternalOverwriteInRange(const size_t offset, const T value)
|
|
{
|
|
typedef typename LengthToIntegralType<N, Boost::integer_traits<T>::is_signed>::Type Target;
|
|
|
|
if (ACTIV_UNLIKELY(!InRangeOfIntegralType<Target>()(value)))
|
|
return STATUS_CODE_OUT_OF_RANGE;
|
|
|
|
return Overwrite<ENDIAN>(offset, static_cast<Target>(value));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
inline StatusCode MessageBuilder::InternalOverwriteBuiltIn(const size_t offset, const T value)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
if (ACTIV_UNLIKELY((offset + sizeof(T)) > originalLength))
|
|
{
|
|
const size_t additionalLength = offset + sizeof(T) - originalLength;
|
|
byte_t *pNewTail = m_pTail + additionalLength;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(additionalLength));
|
|
pNewTail = m_pTail + additionalLength;
|
|
}
|
|
|
|
m_pTail = pNewTail;
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
|
|
CopyValueToUnalignedPointer<T>(pDest, value);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<bool REVERSE_BYTE_ORDER>
|
|
inline StatusCode MessageBuilder::InternalOverwriteBuffer(const size_t offset, const void * const pBuffer, const size_t length)
|
|
{
|
|
const size_t originalLength = GetLength();
|
|
|
|
if (ACTIV_UNLIKELY(offset > originalLength))
|
|
return STATUS_CODE_INVALID_PARAMETER;
|
|
|
|
if (ACTIV_UNLIKELY((offset + length) > originalLength))
|
|
{
|
|
const size_t additionalLength = offset + length - originalLength;
|
|
byte_t *pNewTail = m_pTail + additionalLength;
|
|
|
|
if (ACTIV_UNLIKELY(pNewTail > m_pBufferEnd))
|
|
{
|
|
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, MaybeAdjustBufferSize(additionalLength));
|
|
pNewTail = m_pTail + additionalLength;
|
|
}
|
|
|
|
m_pTail = pNewTail;
|
|
}
|
|
|
|
void * const pDest = reinterpret_cast<byte_t *>(m_pBuffer) + offset;
|
|
|
|
MessageBuilderMaybeMemoryReverseCopy<REVERSE_BYTE_ORDER>()(pDest, pBuffer, length);
|
|
|
|
return STATUS_CODE_SUCCESS;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline MessageBuilder::ResetLengthHelper::ResetLengthHelper(MessageBuilder &messageBuilder) :
|
|
m_messageBuilder(messageBuilder),
|
|
m_originalLength(messageBuilder.GetLength())
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void MessageBuilder::ResetLengthHelper::operator()()
|
|
{
|
|
(void) m_messageBuilder.SetLength(m_originalLength);
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_BASE_MESSAGE_BUILDER_H)
|