Files

266 lines
6.4 KiB
C++

/**
* @file include/ActivMiddleware/ActivBase/Uuid.h
*
* @brief Header file for the Uuid class.
*/
#if (!defined ACTIV_BASE_UUID_H)
#define ACTIV_BASE_UUID_H
#include "ActivMiddleware/ActivBase/External.h"
#include "ActivMiddleware/ActivBase/FieldTypes/BinaryString.h"
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
#include "ActivMiddleware/CxxYZ/functional.h"
//! Activ namespace.
namespace Activ
{
class MessageBuilder;
class MessageValidator;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Uuid class.
*/
class Uuid
{
public:
static const size_t UUID_SIZE = 16;
/**
* @brief Default constructor that initializes with a new uuid.
*
* @throw Exception
*/
ACTIV_BASE_API Uuid();
/**
* @brief Constructor that initializes the uuid from a string representation.
*
* @param uuidString string representation of the uuid.
*
* @throw Exception
*/
ACTIV_BASE_API Uuid(const String &uuidString);
/**
* @brief Constructor that initializes the uuid from a binary string representation.
*
* @param uuidString binary string representation of the uuid.
*
* @throw Exception
*/
ACTIV_BASE_API Uuid(const BinaryString &uuidString);
/**
* @brief Equality operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*/
ACTIV_BASE_API bool operator==(const Uuid &rhs) const;
/**
* @brief Inequality operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*
* @throw BaseException
*/
ACTIV_BASE_API bool operator!=(const Uuid &rhs) const;
/**
* @brief Less than operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*
* @throw Exception
*/
ACTIV_BASE_API bool operator<(const Uuid &rhs) const;
/**
* @brief Less than or equal to operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*
* @throw Exception
*/
ACTIV_BASE_API bool operator<=(const Uuid &rhs) const;
/**
* @brief Greater than operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*
* @throw Exception
*/
ACTIV_BASE_API bool operator>(const Uuid &rhs) const;
/**
* @brief Greater than or equal to operator.
*
* @param rhs the object to compare with.
*
* @return the result of the test.
*
* @throw Exception
*/
ACTIV_BASE_API bool operator>=(const Uuid &rhs) const;
/**
* @brief Set the value of the uuid from a string representation.
*
* @param uuidString string representation of the Uuid.
*
* @retval STATUS_CODE_SUCCESS
* @retval STATUS_CODE_INVALID_PARAMETER
*/
ACTIV_BASE_API StatusCode Set(const String &uuidString);
/**
* @brief Set the value of the uuid from a binary string representation.
*
* @param uuidString binary string representation of the Uuid.
*
* @return STATUS_CODE_SUCCESS or an error code.
*/
ACTIV_BASE_API StatusCode Set(const BinaryString &uuidString);
/**
* @brief Get a string representation of the Uuid.
*
* @return string representation of the Uuid.
*/
ACTIV_BASE_API String ToString() const;
/**
* @brief Get a binary string representation of the Uuid.
*
* @return binary string representation of the Uuid.
*/
ACTIV_BASE_API BinaryString ToBinaryString() const;
/**
* @brief Compare to another uuid.
*
* @param rhs
*
* @retval 0 (*this == rhs)
* @retval +ve (*this > rhs)
* @retval -ve (*this < rhs)
*/
ACTIV_BASE_API int Compare(const Uuid &rhs) const;
/**
* @brief Get the length of a serialized Uuid.
*
* @return UUID_SIZE
*/
static size_t GetSerializedLength();
/**
* @brief Serialize to a MessageBuilder.
*
* @param messageBuilder to serialize into.
*
* @return STATUS_CODE_SUCCESS or an error code.
*/
ACTIV_BASE_API StatusCode Serialize(MessageBuilder &messageBuilder) const;
/**
* @brief Deserialize from a MessageValidator into this object.
*
* @param messageValidator to deserialize from.
*
* @return STATUS_CODE_SUCCESS or an error code.
*/
ACTIV_BASE_API StatusCode Deserialize(MessageValidator &messageValidator);
/**
* @brief Serialize to a MessageBuilder as base64-encoded text.
*
* @param messageBuilder to serialize into.
*
* @return STATUS_CODE_SUCCESS or an error code.
*/
template<typename BASE64_CODEC>
StatusCode SerializeBase64(MessageBuilder &messageBuilder) const;
ACTIV_BASE_API static const Uuid emptyUuid; ///< Empty uuid object.
private:
static const Uuid MakeEmptyUuid();
byte_t m_uuid[UUID_SIZE];
friend std::size_t hash_value(const Activ::Uuid &uuid);
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t Uuid::GetSerializedLength()
{
return UUID_SIZE;
}
// ---------------------------------------------------------------------------------------------------------------------------------
template<typename BASE64_ENCODER>
StatusCode Uuid::SerializeBase64(MessageBuilder &messageBuilder) const
{
#if (defined ACTIV_PLATFORM_WINDOWS)
// Ensure endianness is consistent across platforms.
byte_t buff[UUID_SIZE];
MessageBuilder temporaryMessageBuilder(buff, sizeof(buff));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, Serialize(temporaryMessageBuilder));
return messageBuilder.AppendBase64<BASE64_ENCODER>(temporaryMessageBuilder.GetMessage(), temporaryMessageBuilder.GetLength());
#else
return messageBuilder.AppendBase64<BASE64_ENCODER>(m_uuid, sizeof(m_uuid));
#endif
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline std::size_t hash_value(const Uuid &uuid)
{
return Boost::hash_range(uuid.m_uuid, uuid.m_uuid + uuid.UUID_SIZE);
}
} // namespace Activ
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
namespace ACTIV_CXXYZ_HASH_NAMESPACE
{
template<>
struct hash<Activ::Uuid>
{
size_t operator()(const Activ::Uuid &uuid) const
{
return hash_value(uuid);
}
};
} // namespace ACTIV_CXXYZ_HASH_NAMESPACE
#endif // (!defined ACTIV_BASE_UUID_H)