Files

174 lines
4.7 KiB
C++

/**
* @file include/ActivMiddleware/ActivFramework/MessageKey.h
*
* @brief Header file for message key class.
*/
#if (!defined ACTIV_FRAMEWORK_MESSAGE_KEY_H)
#define ACTIV_FRAMEWORK_MESSAGE_KEY_H
#include "ActivMiddleware/ActivFramework/BasicTypes.h"
#include "ActivMiddleware/Misc/BitFields.h"
#include ACTIV_BOOST_INCLUDE(utility/explicit_operator_bool.hpp)
//! Activ namespace.
namespace Activ
{
/**
* @brief Message key class.
*/
class MessageKey
{
private:
class InternalBitmap : public Bitmap<64, MsbOrigin>
{
public:
typedef BitField<InternalBitmap, 1> IsPrivate;
typedef BitField<IsPrivate, 15> HeapId;
typedef TerminalBitField<HeapId> MessageId;
};
ACTIV_BOOST_STATIC_ASSERT(sizeof(MessageId) >= sizeof(void *));
ACTIV_BOOST_STATIC_ASSERT(sizeof(void *) <= sizeof(InternalBitmap::Type));
public:
static const MessageId INVALID_MESSAGE_ID = InternalBitmap::MessageId::maxValue;
//! Default constructor.
MessageKey();
/**
* @brief Constructor.
*
* @param messageId index of message within heap.
* @param heapId heap id.
* @param isPrivate is the heap in private memory?
*/
MessageKey(const MessageId messageId, const HeapId heapId, const bool isPrivate);
//! Clear the message key.
void Clear();
/**
* @brief Equality operator.
*
* @return whether the two objects are equal.
*/
bool operator==(const MessageKey &rhs) const;
/**
* @brief Inequality operator.
*
* @return whether the two objects are not equal.
*/
bool operator!=(const MessageKey &rhs) const;
/**
* @brief Assign a new message id.
*
* @param messageId
*/
void Assign(const MessageId messageId);
//! Get message id.
MessageId GetMessageId() const;
//! Get heap id.
HeapId GetHeapId() const;
//! Is this a key for a private memory message?
bool IsPrivate() const;
//! Is a message attached to this object?
ACTIV_BOOST_EXPLICIT_OPERATOR_BOOL()
//! Is a message not attached to this object?
bool operator!() const;
private:
InternalBitmap::Type m_internalBitmap; ///< Encoded heap id and message id.
};
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageKey::MessageKey()
{
Clear();
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageKey::MessageKey(const MessageId messageId, const HeapId heapId, const bool isPrivate)
{
InternalBitmap::IsPrivate::Set(m_internalBitmap, isPrivate);
InternalBitmap::HeapId::Set(m_internalBitmap, heapId);
Assign(messageId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void MessageKey::Clear()
{
m_internalBitmap = Boost::integer_traits<InternalBitmap::Type>::const_max;
// The same as:
// InternalBitmap::HeapId::Set(m_internalBitmap, INVALID_HEAP_ID);
// Assign(MessageKey::INVALID_MESSAGE_ID);
// Don't worry about IsPrivate bit.
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool MessageKey::operator==(const MessageKey &rhs) const
{
return (m_internalBitmap == rhs.m_internalBitmap);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool MessageKey::operator!=(const MessageKey &rhs) const
{
return !((*this) == rhs);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline void MessageKey::Assign(const MessageId messageId)
{
InternalBitmap::MessageId::Set(m_internalBitmap, messageId);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline MessageId MessageKey::GetMessageId() const
{
return InternalBitmap::MessageId::GetShifted(m_internalBitmap);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline HeapId MessageKey::GetHeapId() const
{
return InternalBitmap::HeapId::GetShifted(m_internalBitmap);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool MessageKey::IsPrivate() const
{
return InternalBitmap::IsPrivate::IsSet(m_internalBitmap);
}
// ---------------------------------------------------------------------------------------------------------------------------------
inline bool MessageKey::operator!() const
{
return (INVALID_MESSAGE_ID == GetMessageId());
}
} // namespace Activ
#endif // (!defined ACTIV_FRAMEWORK_MESSAGE_KEY_H)