Files
externals/linux/include/ActivOneApi/FieldListBuilder.h
T

397 lines
14 KiB
C++

/**
* @file include/ActivOneApi/FieldListBuilder.h
*
* @brief Header file for the FieldListBuilder class.
*/
#if (!defined ACTIV_ONE_API_FIELD_LIST_BUILDER_H)
#define ACTIV_ONE_API_FIELD_LIST_BUILDER_H
#include "ActivOneApi/External.h"
#include "ActivOneApi/TypeDefinitions.h"
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
#include "ActivMiddleware/ActivBase/FieldTypes/All.h"
#include ACTIV_BOOST_INCLUDE(static_assert.hpp)
//! Activ namespace.
namespace Activ
{
//! OneApi namespace.
namespace OneApi
{
/**
* @brief FieldListBuilder class.
*
* This class provides an interface to build a list of field (id, value) pairs for transmission.
*/
class FieldListBuilder
{
public:
//! Default constructor.
FieldListBuilder();
/**
* @brief Constructor to build into an existing MessageBuilder.
*
* messageBuilder must remain in scope for the lifetime of this object.
*
* @param messageBuilder the message builder to build into.
*/
explicit FieldListBuilder(MessageBuilder &messageBuilder);
/**
* @brief Append an ACTIV field.
*
* If @a fieldValue is not initialized an exception is thrown.
*
* @tparam T the IFieldType type
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
template <typename T>
void Append(const FieldId fieldId, const T &fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined ACTIV field.
*
* @param fieldId id of the field to append.
* @param fieldValue the field value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
*/
void Append(const FieldId fieldId, const IFieldType &fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined SInt field.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendSInt(const FieldId fieldId, const int64_t fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined UInt field.
*
* @tparam T the basic unsigned int type (uint8_t, uint16_t, uint32_t)
*
* @param fieldId id of the field to append.
* @param fieldValue the uint8_t value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendUInt(const FieldId fieldId, const uint64_t fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined TextString field.
*
* If @a fieldValue is empty an exception is thrown.
*
* @param fieldId id of the field to append.
* @param pText the null terminated text value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendTextString(const FieldId fieldId, const char *pText, const bool shouldUpdate = true);
/**
* @brief Append a defined TextString field.
*
* If @a fieldValue is empty an exception is thrown.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendTextString(const FieldId fieldId, const String &fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined TextString field.
*
* If @a fieldValue is empty an exception is thrown.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendTextString(const FieldId fieldId, const CxxYZ::string_view &fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined BinaryString field.
*
* If @a length equals zero an exception is thrown.
*
* @param fieldId id of the field to append.
* @param pData pointer to the data.
* @param length the length of pData.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendBinaryString(const FieldId fieldId, const uint8_t * const pData, const size_t length,
const bool shouldUpdate = true);
/**
* @brief Append a defined Blob field.
*
* If @a length equals zero an exception is thrown.
*
* @param fieldId id of the field to append.
* @param pData pointer to the data.
* @param length the length of pData.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendBlob(const FieldId fieldId, const uint8_t * const pData, const size_t length, const bool shouldUpdate = true);
/**
* @brief Append a defined Boolean.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendBoolean(const FieldId fieldId, const bool fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined Double.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendDouble(const FieldId fieldId, const float fieldValue, const bool shouldUpdate = true);
/**
* @brief Append a defined Double.
*
* @param fieldId id of the field to append.
* @param fieldValue the value to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
* @throw std::exception
*/
void AppendDouble(const FieldId fieldId, const double fieldValue, const bool shouldUpdate = true);
/**
* @brief Append an undefined field.
*
* @param fieldId id of the field to append.
* @param shouldUpdate does this field update the last cached value.
*
* @throw Exception
*/
ACTIV_ONE_API void AppendUndefined(const FieldId fieldId, const bool shouldUpdate = true);
/**
* @brief Reset this object.
*
* Clears data in the object ready to build a new field list.
*/
void Reset();
/**
* @brief Get a pointer to the serialized data.
*
* @return The pointer to the serialized data.
*/
const void *GetData() const;
/**
* @brief Get length of serialized data.
*
* @return The length of the data returned by GetData().
*/
size_t GetLength() const;
private:
ACTIV_ONE_API void InternalAppendHeader(const FieldId fieldId, const FieldType fieldType, const bool shouldUpdate);
CxxYZ::unique_ptr<MessageBuilder> m_pAllocatedMessageBuilder;
MessageBuilder & m_messageBuilder;
const size_t m_originalLength;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline FieldListBuilder::FieldListBuilder() :
m_pAllocatedMessageBuilder(new MessageBuilder()),
m_messageBuilder(*m_pAllocatedMessageBuilder),
m_originalLength(0)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline FieldListBuilder::FieldListBuilder(MessageBuilder &messageBuilder) :
m_messageBuilder(messageBuilder),
m_originalLength(messageBuilder.GetLength())
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
template <typename T>
inline void FieldListBuilder::Append(const FieldId fieldId, const T &fieldValue, const bool shouldUpdate)
{
ACTIV_BOOST_STATIC_ASSERT(IsFieldType<T>::value);
InternalAppendHeader(fieldId, fieldValue.GetType(), shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, fieldValue.T::SerializeLengthAndBody(m_messageBuilder));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::Append(const FieldId fieldId, const IFieldType &fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, fieldValue.GetType(), shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, fieldValue.SerializeLengthAndBody(m_messageBuilder));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendSInt(const FieldId fieldId, const int64_t fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_SINT, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, SInt::SerializeValueLengthAndBody(m_messageBuilder, fieldValue));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendUInt(const FieldId fieldId, const uint64_t fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_UINT, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, UInt::SerializeValueLengthAndBody(m_messageBuilder, fieldValue));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendTextString(const FieldId fieldId, const char *pText, const bool shouldUpdate)
{
AppendTextString(fieldId, CxxYZ::string_view(pText), shouldUpdate);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendTextString(const FieldId fieldId, const String &fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_TEXT_STRING, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, TextString::SerializeLengthAndBody(m_messageBuilder,
TextString::GetMaxSerializedBodyLength(fieldValue.size()), reinterpret_cast<const uint8_t * const>(fieldValue.data()),
fieldValue.size()));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendTextString(const FieldId fieldId, const CxxYZ::string_view &fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_TEXT_STRING, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, TextString::SerializeLengthAndBody(m_messageBuilder,
TextString::GetMaxSerializedBodyLength(fieldValue.size()), reinterpret_cast<const uint8_t * const>(fieldValue.data()),
fieldValue.size()));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendBinaryString(const FieldId fieldId, const uint8_t * const pData, const size_t length,
const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_BINARY_STRING, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, BinaryString::SerializeLengthAndBody(m_messageBuilder,
BinaryString::GetMaxSerializedBodyLength(length), pData, length));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendBlob(const FieldId fieldId, const uint8_t * const pData, const size_t length,
const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_BLOB, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, Blob::SerializeLengthAndBody(m_messageBuilder,
Blob::GetMaxSerializedBodyLength(length), pData, length));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendBoolean(const FieldId fieldId, const bool fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_BOOLEAN, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, Boolean::SerializeValueLengthAndBody(m_messageBuilder, fieldValue));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendDouble(const FieldId fieldId, const float fieldValue, const bool shouldUpdate)
{
AppendDouble(fieldId, static_cast<double>(fieldValue), shouldUpdate);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::AppendDouble(const FieldId fieldId, const double fieldValue, const bool shouldUpdate)
{
InternalAppendHeader(fieldId, FIELD_TYPE_DOUBLE, shouldUpdate);
ACTIV_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, Double::SerializeValueLengthAndBody(m_messageBuilder, fieldValue));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void FieldListBuilder::Reset()
{
if (STATUS_CODE_SUCCESS != m_messageBuilder.SetLength(m_originalLength))
assert(false);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline const void *FieldListBuilder::GetData() const
{
return m_messageBuilder.GetMessage();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t FieldListBuilder::GetLength() const
{
return m_messageBuilder.GetLength();
}
} // namespace OneApi
} // namespace Activ
#endif // (!defined ACTIV_ONE_API_FIELD_LIST_BUILDER_H)