/** * @file include/ActivMiddleware/ActivFramework/Subject.h * * @brief Subject type header file. */ #if (!defined ACTIV_FRAMEWORK_SUBJECT_H) #define ACTIV_FRAMEWORK_SUBJECT_H #include "ActivMiddleware/ActivFramework/External.h" #include "ActivMiddleware/ActivBase/MessageBuilder.h" #include "ActivMiddleware/ActivBase/MessageValidator.h" #include "ActivMiddleware/Misc/MemoryHelper.h" //! Activ namespace. namespace Activ { class MessageBuilder; class MessageValidator; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Subject class. */ class Subject { public: typedef byte_t SubjectLength; ///< Type of the length. static const SubjectLength MAX_LENGTH = 48; ///< Maximum subject length. static const char SEPARATOR_CHAR = '/'; ///< Subject section separator character. //! Default constructor. Subject(); /** * @brief Constructor for binary subject. * * @param pSubjectData subject data. * @param subjectDataLength subject data length. * * @throw StatusCodeException */ Subject(const void *pSubjectData, const size_t subjectDataLength); /** * @brief Constructor for c-string subject. * * @param pSubject textual subject. * * @throw StatusCodeException */ Subject(const char *pSubject); /** * @brief Constructor for stl string subject. * * @param subject subject string. * * @throw StatusCodeException */ Subject(const String &subject); //! Clear the subject down. void Clear(); /** * @brief Serialize a subject. * * @param messageBuilder * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode Serialize(MessageBuilder &messageBuilder) const; /** * @brief Deserialize a subject. * * @param messageValidator * * @retval STATUS_CODE_SUCCESS * @retval ... */ StatusCode Deserialize(MessageValidator &messageValidator); //! Get the length of the subject. size_t GetLength() const; //! Get the length of the subject when serialized. size_t GetSerializedLength() const; /** * @brief Set subject. * * @param pSubject textual subject. * * @throw StatusCodeException */ void Set(const char *pSubject); /** * @brief Set subject. * * @param pSubjectData subject data. * @param subjectDataLength subject data length. * * @throw StatusCodeException */ void Set(const void *pSubjectData, const size_t subjectDataLength); /** * @brief Set subject from a string. * * @param subject subject string. * * @throw StatusCodeException */ void Set(const String &subject); /** * @brief Set subject from another Subject object. * * @param subject Subject to assign from. */ void Set(const Subject &subject); //! Get pointer to raw subject data. const void *Get() const; /** * @brief Convert the subject to a string. * * @return A string containing the subject. Note if the subject is not printable, nor will the returned string be! */ String ToString() const; /** * @brief Equality operator. * * @param subject subject to compare. * * @return boolean. */ bool operator==(const Subject &subject) const; /** * @brief Inequality operator. * * @param subject subject to compare. * * @return boolean. */ bool operator!=(const Subject &subject) const; /** * @brief Less than operator. * * @param subject subject to compare. * * @return boolean. */ bool operator<(const Subject &subject) const; /** * @brief Greater than operator. * * @param subject subject to compare. * * @return boolean. */ bool operator>(const Subject &subject) const; /** * @brief Less than or equal operator. * * @param subject subject to compare. * * @return boolean. */ bool operator<=(const Subject &subject) const; /** * @brief Greater than or equal operator. * * @param subject subject to compare. * * @return boolean. */ bool operator>=(const Subject &subject) const; private: /** * @brief Initialize method. * * @param pSubjectData subject data. * @param subjectDataLength subject data length. * * @throw StatusCodeException */ void Initialize(const void *pSubjectData, const size_t subjectDataLength); /** * @brief Compare two subjects. * * @param subject Subject to compare. * * @retval 0 if the subjects are identical. * @retval +ve if this is lexicographically greater than \a subject. * @retval -ve if this is lexicographically less than \a subject. */ int Compare(const Subject &subject) const; #pragma pack(1) byte_t m_subject[MAX_LENGTH]; ///< Subject. SubjectLength m_length; ///< Length of the subject. #pragma pack() }; // --------------------------------------------------------------------------------------------------------------------------------- inline Subject::Subject() { Clear(); ::memset(m_subject, 0, sizeof(m_subject)); } // --------------------------------------------------------------------------------------------------------------------------------- inline Subject::Subject(const void *pSubjectData, const size_t subjectDataLength) { Initialize(pSubjectData, subjectDataLength); } // --------------------------------------------------------------------------------------------------------------------------------- inline Subject::Subject(const char *pSubject) { Initialize(pSubject, ::strlen(pSubject)); } // --------------------------------------------------------------------------------------------------------------------------------- inline Subject::Subject(const String &subject) { Initialize(subject.c_str(), subject.size()); } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Clear() { m_length = 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline StatusCode Subject::Serialize(MessageBuilder &messageBuilder) const { ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.Append(m_length)); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageBuilder.AppendBuffer(m_subject, m_length)); return STATUS_CODE_SUCCESS; } // --------------------------------------------------------------------------------------------------------------------------------- inline StatusCode Subject::Deserialize(MessageValidator &messageValidator) { ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange(m_length))); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, messageValidator.ValidateBytesCopy(m_length, &m_subject)); return STATUS_CODE_SUCCESS; } // --------------------------------------------------------------------------------------------------------------------------------- inline size_t Subject::GetLength() const { return m_length; } // --------------------------------------------------------------------------------------------------------------------------------- inline size_t Subject::GetSerializedLength() const { return sizeof(m_length) + m_length; } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Set(const char *pSubject) { Initialize(pSubject, ::strlen(pSubject)); } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Set(const void *pSubjectData, const size_t subjectDataLength) { Initialize(pSubjectData, subjectDataLength); } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Set(const String &subject) { Initialize(subject.c_str(), subject.size()); } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Set(const Subject &subject) { m_length = subject.m_length; MemoryCopy(m_subject, subject.m_subject, m_length); } // --------------------------------------------------------------------------------------------------------------------------------- inline const void *Subject::Get() const { return m_subject; } // --------------------------------------------------------------------------------------------------------------------------------- inline String Subject::ToString() const { return String(reinterpret_cast(m_subject), m_length); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator==(const Subject &subject) const { return 0 == Compare(subject); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator!=(const Subject &subject) const { return 0 != Compare(subject); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator<(const Subject &subject) const { return Compare(subject) < 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator>(const Subject &subject) const { return Compare(subject) > 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator<=(const Subject &subject) const { return Compare(subject) <= 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline bool Subject::operator>=(const Subject &subject) const { return Compare(subject) >= 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline void Subject::Initialize(const void *pSubjectData, const size_t subjectDataLength) { if (0 == pSubjectData) { m_length = 0; } else { if (subjectDataLength > MAX_LENGTH) ACTIV_THROW(StatusCodeException, STATUS_CODE_INVALID_LENGTH); m_length = static_cast(subjectDataLength); MemoryCopy(m_subject, pSubjectData, m_length); } ::memset(m_subject + m_length, 0, sizeof(m_subject) - m_length); } // --------------------------------------------------------------------------------------------------------------------------------- inline int Subject::Compare(const Subject &subject) const { if (m_length != subject.m_length) { const size_t compareLength = std::min(m_length, subject.m_length); const int compareValue = ::memcmp(m_subject, subject.m_subject, compareLength); if (0 == compareValue) return (m_length == compareLength) ? -1 : 1; else return compareValue; } else { return ::memcmp(m_subject, subject.m_subject, m_length); } } } // namespace Activ #endif // (!defined ACTIV_FRAMEWORK_SUBJECT_H)