Files

243 lines
6.0 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/ScopedAddress.h
*
* @brief ScopedAddress header file.
*/
#if (!defined ACTIV_FRAMEWORK_SCOPED_ADDRESS_H)
#define ACTIV_FRAMEWORK_SCOPED_ADDRESS_H
#include "ActivMiddleware/ActivFramework/External.h"
#include "ActivMiddleware/ActivFramework/Address.h"
#include "ActivMiddleware/ActivBase/Exception.h"
#include ACTIV_BOOST_INCLUDE(move/move.hpp)
//! Activ namespace.
namespace Activ
{
class Component;
// ---------------------------------------------------------------------------------------------------------------------------------
/**
* @brief Scoped address.
*
* When a ScopedAddress goes out of scope, or is assigned a new value, a SYSTEM_MESSAGE_CLOSE_ADDRESS message is sent to the
* current address (if valid).
*/
class ScopedAddress
{
public:
/**
* @brief Constructor.
*
* @param component reference to the Component used to post a SYSTEM_MESSAGE_CLOSE_ADDRESS when this object goes out of scope.
*/
ScopedAddress(const Component &component);
/**
* @brief Constructor.
*
* @param component reference to the Component used to post a SYSTEM_MESSAGE_CLOSE_ADDRESS when this object goes out of scope.
* @param address initial Address value to assign.
*/
ScopedAddress(const Component &component, const Address &address);
/**
* @brief Move constructor.
*
* @param rhs ScopedAddress to move into this.
*/
ScopedAddress(ACTIV_BOOST_RV_REF(ScopedAddress) rhs) :
m_component(rhs.m_component),
m_address(rhs.m_address)
{
// Inline here for G++.
rhs.Release();
}
/**
* @brief Destructor.
*
* If the underlying address is valid, a SYSTEM_MESSAGE_CLOSE_ADDRESS is posted to it.
*/
~ScopedAddress();
/**
* @brief Clear the current address.
*
* If the current address is valid, a SYSTEM_MESSAGE_CLOSE_ADDRESS is posted to it.
*/
ACTIV_FRAMEWORK_API
void Clear();
/**
* @brief Clear the current address, but do not post a message to it.
*/
void Release();
/**
* @brief Convert the address to a string.
*
* @return String representation of the address, in the form "endPointId:portId".
*/
String ToString() const;
/**
* @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 Equality test operator.
*
* @param rhs Address object to compare with.
*
* @return Boolean.
*/
bool operator==(const Address &rhs) const;
/**
* @brief Inequality test operator.
*
* @param rhs Address object to compare with.
*
* @return Boolean.
*/
bool operator!=(const Address &rhs) const;
/**
* @brief Assignment from Address operator.
*
* If the current address is valid, a SYSTEM_MESSAGE_CLOSE_ADDRESS is posted to it before assigning the new value.
*
* @param rhs the Address to assign from.
*
* @return *this.
*/
ScopedAddress &operator=(const Address &rhs);
/**
* @brief Move assignment from ScopedAddress operator.
*
* If the current address is valid, a SYSTEM_MESSAGE_CLOSE_ADDRESS is posted to it before assigning the new value.
* The Address stored in rhs is released.
*
* @param rhs the Address to assign from.
*
* @return *this.
*/
ScopedAddress &operator=(ACTIV_BOOST_RV_REF(ScopedAddress) rhs)
{
if (this != &rhs)
{
// Inline here for G++.
Clear();
m_address = rhs.m_address;
rhs.Release();
}
return *this;
}
/**
* @brief Const Address reference cast operator.
*
* @return Const reference to the underlying Address operator.
*/
operator const Address &() const;
private:
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ScopedAddress)
const Component & m_component;
Address m_address;
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedAddress::ScopedAddress(const Component &component) :
m_component(component)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedAddress::ScopedAddress(const Component &component, const Address &address) :
m_component(component),
m_address(address)
{
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedAddress::~ScopedAddress()
{
Clear();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void ScopedAddress::Release()
{
m_address.Clear();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline String ScopedAddress::ToString() const
{
return m_address.ToString();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool ScopedAddress::IsValid() const
{
return m_address.IsValid();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool ScopedAddress::operator==(const Address &rhs) const
{
return (m_address == rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool ScopedAddress::operator!=(const Address &rhs) const
{
return (m_address != rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedAddress &ScopedAddress::operator=(const Address &rhs)
{
if (rhs != m_address)
{
Clear();
m_address = rhs;
}
return *this;
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline ScopedAddress::operator const Address &() const
{
return m_address;
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_SCOPED_ADDRESS_H)