Files

342 lines
9.2 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/Address.h
*
* @brief Address header file.
*/
#if (!defined ACTIV_FRAMEWORK_ADDRESS_H)
#define ACTIV_FRAMEWORK_ADDRESS_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivBase/MessageBuilder.h"
#include "ActivMiddleware/ActivBase/MessageValidator.h"
#include "ActivMiddleware/ActivBase/ActivBase.h"
#include "ActivMiddleware/CxxYZ/functional.h"
#include ACTIV_BOOST_INCLUDE(functional/hash.hpp)
//! Activ namespace.
namespace Activ
{
/**
* @brief Address.
*/
class Address
{
public:
/**
* @brief Default constructor.
*
* The endpoint id is set to INVALID_ENDPOINT_ID and the port id to INVALID_PORT_ID.
*/
Address();
/**
* @brief Constructor.
*
* @param endPointId
* @param portId
*/
Address(const EndPointId endPointId, const PortId portId);
/**
* @brief Serialize the address.
*
* @param messageBuilder
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
StatusCode Serialize(MessageBuilder &messageBuilder) const;
/**
* @brief Deserialize the address.
*
* @param messageValidator
*
* @return STATUS_CODE_SUCCESS or an error status code.
*/
StatusCode Deserialize(MessageValidator &messageValidator);
/**
* @brief Get the serialized length of an Address object.
*
* @return The serialized length in bytes.
*/
static size_t GetSerializedLength();
/**
* @brief Clear the address.
*
* The endpoint id is set to INVALID_ENDPOINT_ID and the port id to INVALID_PORT_ID.
*/
void Clear();
/**
* @brief Convert the address to a string.
*
* @return String representation of the address, in the form "endPointId:portId".
*/
ACTIV_FRAMEWORK_API const String ToString() const;
/**
* @brief Initialize the address from a string.
*
* @param str a string in the format returned by ToString().
*
* @return True if the conversion was successful.
*/
ACTIV_FRAMEWORK_API bool FromString(const String &str);
/**
* @brief Is this a valid address?
*
* A valid address is one where endPointId is not INVALID_ENDPOINT_ID and portId is not INVALID_PORT_ID.
*
* @return Boolean.
*/
bool IsValid() const;
/**
* @brief Get the endpoint id.
*
* @return the endpoint id.
*/
EndPointId GetEndPointId() const;
/**
* @brief Set the endpoint id.
*
* @param endPointId the endpoint id.
*/
void SetEndPointId(const EndPointId endPointId);
/**
* @brief Get the port id.
*
* @return the port id.
*/
PortId GetPortId() const;
/**
* @brief Set the port id.
*
* @param portId the port id.
*/
void SetPortId(const PortId portId);
/**
* @brief Set the endpoint and port id.
*
* @param endPointId the endpoint id.
* @param portId the port id.
*/
void Set(const EndPointId endPointId, const PortId portId);
/**
* @brief '==' operator.
*
* @param rhs Address to compare.
*
* @return Boolean.
*/
bool operator==(const Address &rhs) const;
/**
* @brief '!=' operator.
*
* @param rhs Address to compare.
*
* @return Boolean.
*/
bool operator!=(const Address &rhs) const;
/**
* @brief '<' operator.
*
* @param rhs Address to compare.
*
* @return Boolean.
*/
bool operator<(const Address &rhs) const;
private:
EndPointId m_endPointId;
PortId m_portId;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline Address::Address()
{
Clear();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline Address::Address(const EndPointId endPointId, const PortId portId)
{
SetEndPointId(endPointId);
SetPortId(portId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Address::Serialize(MessageBuilder &messageBuilder) const
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.Append<MessageBuilder::ENDIAN_LITTLE>(m_endPointId));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.Append<MessageBuilder::ENDIAN_LITTLE>(m_portId));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline StatusCode Address::Deserialize(MessageValidator &messageValidator)
{
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegral<sizeof(m_endPointId),
MessageValidator::ENDIAN_LITTLE>(m_endPointId)));
ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegral<sizeof(m_portId),
MessageValidator::ENDIAN_LITTLE>(m_portId)));
return STATUS_CODE_SUCCESS;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline size_t Address::GetSerializedLength()
{
return ACTIV_FIELD_SIZE(Address, m_endPointId) + ACTIV_FIELD_SIZE(Address, m_portId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
ACTIV_FORCE_INLINE void Address::Clear()
{
// HACK. Small performance win over two member sets. Various asserts to be safe(r).
ACTIV_BOOST_STATIC_ASSERT(sizeof(Address) == (sizeof(EndPointId) + sizeof(PortId)));
ACTIV_BOOST_STATIC_ASSERT(INVALID_PORT_ID == INVALID_ENDPOINT_ID);
ACTIV_BOOST_STATIC_ASSERT(INVALID_PORT_ID == uint16_t(0xffff));
::memset(this, 0xff, sizeof(Address));
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline EndPointId Address::GetEndPointId() const
{
return m_endPointId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Address::SetEndPointId(EndPointId endPointId)
{
m_endPointId = endPointId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline PortId Address::GetPortId() const
{
return m_portId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Address::SetPortId(PortId portId)
{
m_portId = portId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void Address::Set(const EndPointId endPointId, const PortId portId)
{
m_endPointId = endPointId;
m_portId = portId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Address::IsValid() const
{
return (INVALID_ENDPOINT_ID != m_endPointId) && (INVALID_PORT_ID != m_portId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Address::operator==(const Address &rhs) const
{
return (m_endPointId == rhs.m_endPointId) && (m_portId == rhs.m_portId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Address::operator!=(const Address &rhs) const
{
return !(*this == rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool Address::operator<(const Address &rhs) const
{
if (m_endPointId < rhs.m_endPointId)
return true;
if (m_endPointId > rhs.m_endPointId)
return false;
return m_portId < rhs.m_portId;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
inline std::size_t hash_value(const Address &address)
{
size_t seed = 0;
Boost::hash_combine(seed, address.GetEndPointId());
Boost::hash_combine(seed, address.GetPortId());
return seed;
}
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Global operator<< to dump an Address to an ostream.
*
* @param os
* @param address
*
* @return The ostream @a os.
*/
inline std::ostream &operator<<(std::ostream &os, const Activ::Address &address)
{
os << address.ToString();
return os;
}
} // namespace Activ
// ---------------------------------------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------------------
namespace ACTIV_CXXYZ_HASH_NAMESPACE
{
template<>
struct hash<Activ::Address>
{
size_t operator()(const Activ::Address &address) const
{
return hash_value(address);
}
};
} // namespace ACTIV_CXXYZ_HASH_NAMESPACE
#endif // (!defined ACTIV_FRAMEWORK_ADDRESS_H)