/** * @file include/ActivMiddleware/ActivBase/GenericMessages/MessageStorage.h * * @brief Header for MessageStorage class. */ #if (!defined ACTIV_BASE_GENERIC_MESSAGES_MESSAGE_STORAGE_H) #define ACTIV_BASE_GENERIC_MESSAGES_MESSAGE_STORAGE_H #include "ActivMiddleware/ActivBase/GenericMessages/Formats/Common/FieldDispatcher.h" #include "ActivMiddleware/ActivBase/GenericMessages/Detail/OptionalFieldsHelper.h" #include ACTIV_BOOST_INCLUDE(mpl/inherit_linearly.hpp) #include ACTIV_BOOST_INCLUDE(mpl/inherit.hpp) #include ACTIV_BOOST_INCLUDE(mpl/transform.hpp) #include ACTIV_BOOST_INCLUDE(mpl/contains.hpp) //! Activ namespace. namespace Activ { //! GenericMessages namespace. namespace GenericMessages { namespace Detail { // Metafunction to select the Storage type from a Field class. template class FieldStorageAccessor { public: typedef typename FIELD::Storage type; }; } // namespace Detail // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- // Undefined. Specialization exists for each format. template class MessageStorageFormat; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Container with concrete storage for a message's fields. * * MessageStorage includes storage for each field. * * @tparam FIELD_LIST is a Boost::mpl sequence of Field classes defining the message. * @tparam NAME an optional StringLiteral for the name of the message. */ template class MessageStorage : public Boost::mpl::inherit_linearly >::type, Boost::mpl::inherit >::type { typedef GenericMessages::Definition Definition; typedef typename Boost::mpl::inherit_linearly >::type, Boost::mpl::inherit >::type Base; public: //! Default constructor. MessageStorage() : m_presentFieldIdBitmap(Definition::highestFieldId + 1, false), m_defaultedFieldIdBitmap(Definition::highestFieldId + 1, false) { } #if (!defined ACTIV_BOOST_NO_CXX11_DEFAULTED_FUNCTIONS) // Default copy is sufficient. MessageStorage(const MessageStorage &) = default; MessageStorage &operator=(const MessageStorage &) = default; //! Move constructor. MessageStorage(ACTIV_BOOST_RV_REF(MessageStorage) rhs) : Base(CxxYZ::move(rhs)), // Note copies of bitmaps such that the rhs size remains Definition::highestFieldId + 1, hence =default not sufficient. m_presentFieldIdBitmap(rhs.m_presentFieldIdBitmap), m_defaultedFieldIdBitmap(rhs.m_defaultedFieldIdBitmap), m_presentFieldIdList(CxxYZ::move(rhs.m_presentFieldIdList)), m_unknownFieldIdList(CxxYZ::move(rhs.m_unknownFieldIdList)) { } //! Move assignment. MessageStorage &operator=(MessageStorage &&rhs) { if (this != CxxYZ::addressof(rhs)) { Base::operator=(CxxYZ::move(rhs)); // Note copies of bitmaps such that the rhs size remains Definition::highestFieldId + 1, hence =default not sufficient. m_presentFieldIdBitmap = rhs.m_presentFieldIdBitmap; m_defaultedFieldIdBitmap = rhs.m_defaultedFieldIdBitmap; m_presentFieldIdList = CxxYZ::move(rhs.m_presentFieldIdList); m_unknownFieldIdList = CxxYZ::move(rhs.m_unknownFieldIdList); } return *this; } #else private: class CopyField { public: CopyField(MessageStorage &lhs, const MessageStorage &rhs) : m_lhs(lhs), m_rhs(rhs) { } template void operator()(Identity) const { ACTIV_BOOST_STATIC_ASSERT((Boost::mpl::contains::type::value)); typedef typename FIELD::Storage FieldStorage; FieldStorage &lhsField(static_cast(m_lhs)); const FieldStorage &rhsField(static_cast(m_rhs)); lhsField = rhsField; } private: MessageStorage & m_lhs; const MessageStorage & m_rhs; }; class MoveField { public: MoveField(MessageStorage &lhs, MessageStorage &rhs) : m_lhs(lhs), m_rhs(rhs) { } template void operator()(Identity) const { ACTIV_BOOST_STATIC_ASSERT((Boost::mpl::contains::type::value)); typedef typename FIELD::Storage FieldStorage; FieldStorage &lhsField(static_cast(m_lhs)); FieldStorage &rhsField(static_cast(m_rhs)); lhsField = CxxYZ::move(rhsField); } private: MessageStorage &m_lhs; MessageStorage &m_rhs; }; public: //! Copy constructor. MessageStorage(const MessageStorage &rhs) : Base(rhs), m_presentFieldIdBitmap(rhs.m_presentFieldIdBitmap), m_defaultedFieldIdBitmap(rhs.m_defaultedFieldIdBitmap), m_presentFieldIdList(rhs.m_presentFieldIdList), m_unknownFieldIdList(rhs.m_unknownFieldIdList) { } //! Move constructor. MessageStorage(ACTIV_BOOST_RV_REF(MessageStorage) rhs) : // Note copies of bitmaps such that the rhs size remains Definition::highestFieldId + 1. m_presentFieldIdBitmap(rhs.m_presentFieldIdBitmap), m_defaultedFieldIdBitmap(rhs.m_defaultedFieldIdBitmap), m_presentFieldIdList(CxxYZ::move(rhs.m_presentFieldIdList)), m_unknownFieldIdList(CxxYZ::move(rhs.m_unknownFieldIdList)) { // Note no call to Base's move constructor. MPL has no support for ACTIV_BOOST_MOVE_BASE, so we have to do it ourselves. // Paying the cost of the default construction of all the bases. MoveField moveField(*this, rhs); Boost::mpl::for_each >(moveField); } // Copy assignment. MessageStorage &operator=(ACTIV_BOOST_COPY_ASSIGN_REF(MessageStorage) rhs) { if (this != CxxYZ::addressof(rhs)) { // Base::operator=() not available, have to do it ourselves. CopyField copyField(*this, rhs); Boost::mpl::for_each >(copyField); m_presentFieldIdBitmap = rhs.m_presentFieldIdBitmap; m_defaultedFieldIdBitmap = rhs.m_defaultedFieldIdBitmap; m_presentFieldIdList = rhs.m_presentFieldIdList; m_unknownFieldIdList = rhs.m_unknownFieldIdList; } return *this; } // Move assignment. MessageStorage &operator=(ACTIV_BOOST_RV_REF(MessageStorage) rhs) { if (this != CxxYZ::addressof(rhs)) { // Base::operator=() not available, have to do it ourselves. MoveField moveField(*this, rhs); Boost::mpl::for_each >(moveField); // Note copies of bitmaps such that the rhs size remains Definition::highestFieldId + 1. m_presentFieldIdBitmap = rhs.m_presentFieldIdBitmap; m_defaultedFieldIdBitmap = rhs.m_defaultedFieldIdBitmap; m_presentFieldIdList = CxxYZ::move(rhs.m_presentFieldIdList); m_unknownFieldIdList = CxxYZ::move(rhs.m_unknownFieldIdList); } return *this; } #endif /** * @brief Set a field. * * FIELD must be a member of FIELD_LIST. * * @param value */ template void Set(typename Detail::FieldStorageAccessor::type::StorageParamType value) { ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); static_cast::type &>(*this).Set(value); SetFieldPresent(FIELD::id); } /** * @brief Set a field. * * FIELD must be a member of FIELD_LIST. * * @param value */ template typename CxxYZ::enable_if< CxxYZ::is_reference::type::StorageParamType>::value>::type Set(ACTIV_BOOST_RV_REF(typename Detail::FieldStorageAccessor::type::StorageType) value) { // Note not enabled if the field is trivial and can be passed by value to FieldStorage::Set() (will be ambiguous). ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); static_cast::type &>(*this).Set(CxxYZ::move(value)); SetFieldPresent(FIELD::id); } /** * @brief Is a field present in the message? * * FIELD must be a member of FIELD_LIST. */ template bool IsFieldPresent() const { ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); return m_presentFieldIdBitmap[FIELD::id]; } /** * @brief Get the list of unknown field id received after deserialization. * * @return Const reference to a FieldIdList. */ const FieldIdList &GetUnknownFieldIdList() const { return m_unknownFieldIdList; } /** * @brief Serialize all the fields that have been Set() in this message to a particular format. * * @tparam FORMAT the format to serialize to. * @tparam T the serializer type (MessageBuilder, rapidjson::writer, etc.). * * @param serializer * * @return STATUS_CODE_SUCCESS or error status. */ template StatusCode Serialize(T &serializer) const { return MessageStorageFormat::Serialize(serializer, *this); } /** * @brief Serialize all the fields that have been Set() in a message to a particular format. * * @tparam FORMAT the format to serialize to. * @tparam T the serializer type (MessageBuilder, rapidjson::writer, etc.). * * @param serializer * @param messageStorage * * @return STATUS_CODE_SUCCESS or error status. */ template static StatusCode Serialize(T &serializer, const MessageStorage &messageStorage) { // Note the static version is provided such that generic code can call this function taking MessageStorage // or a Serialize(messageBuilder, regular-c++-object). return messageStorage.Serialize(serializer); } /** * @brief Deserialize a message. * * @tparam FORMAT the format the serialized data is in. * @tparam T the serialized data type (MessageValidator, Json::Value, etc.). * * @param messageValidator * * @return STATUS_CODE_SUCCESS or error status. */ template StatusCode Deserialize(T &messageValidator) { typedef typename FieldDispatcherSingleton::type FieldDispatcherSingleton; const FieldDispatcherSingleton &fieldDispatcherSingleton(FieldDispatcherSingleton::GetInstance()); ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (MessageStorageFormat::Deserialize(messageValidator, *this, fieldDispatcherSingleton))); if (ACTIV_LIKELY(fieldDispatcherSingleton.AreAllMandatoryFieldsPresent(this->m_presentFieldIdBitmap))) SetOptionalFieldValues(fieldDispatcherSingleton); else return STATUS_CODE_MISSING_FIELDS; return STATUS_CODE_SUCCESS; } /** * @brief Deserialize a message. * * @tparam FORMAT the format the serialized data is in. * @tparam T the serialized data type (MessageValidator, Json::Value, etc.). * * @param messageValidator * @param messageStorage * * @return STATUS_CODE_SUCCESS or error status. */ template static StatusCode Deserialize(T &messageValidator, MessageStorage &messageStorage) { // Note the static version is such that generic code can call this function taking MessageStorage // or a Deserialize(messageValidator, regular-c++-object). return messageStorage.Deserialize(messageValidator); } /** * @brief Serialize all the fields that have been Set() in the message to ACTIV format. * * @param messageBuilder * * @return STATUS_CODE_SUCCESS or error status. */ StatusCode Serialize(MessageBuilder &messageBuilder) const { return Serialize(messageBuilder); } /** * @brief Deserialize a message in ACTIV format. * * @param messageValidator * * @return STATUS_CODE_SUCCESS or error status. */ StatusCode Deserialize(MessageValidator &messageValidator) { return Deserialize(messageValidator); } /** * @brief Get a field's value. * * FIELD must be a member of FIELD_LIST. * * @return Const reference to the field's value (for non-built in types), or the field's value (for built-in types). * * @throw StatusCodeException if the field is not set in the message. */ template typename FIELD::Storage::StorageParamType Get() const { ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); if (ACTIV_LIKELY(this->m_presentFieldIdBitmap[FIELD::id])) return static_cast(*this).Get(); else ACTIV_THROW(StatusCodeException, STATUS_CODE_UNDEFINED_FIELD); } /** * @brief Get a field's value. * * @return Reference to the field's value. * * @throw StatusCodeException if the field is not set in the message. */ template typename FIELD::Storage::StorageType &Get() { ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); if (ACTIV_LIKELY(this->m_presentFieldIdBitmap[FIELD::id])) return static_cast(*this).Get(); else ACTIV_THROW(StatusCodeException, STATUS_CODE_UNDEFINED_FIELD); } /** * @brief Is a field defaulted? * * FIELD must be a member of FIELD_LIST. * * @return true if the (optional) field was not present in a serialized message and has been set to its default value. */ template bool IsFieldDefaulted() const { ACTIV_BOOST_STATIC_ASSERT_MSG((Boost::mpl::contains::type::value), "Field being accessed must be a member of the message definition"); return m_defaultedFieldIdBitmap[FIELD::id]; } /** * @brief Clear down the message storage. * * Note field values are not altered; the message is simply marked as having no fields present. */ void Reset() { m_presentFieldIdBitmap.reset(); m_defaultedFieldIdBitmap.reset(); m_presentFieldIdList.clear(); m_unknownFieldIdList.clear(); } //! Equality operator. Defaulted fields do not take part in the comparison. bool operator==(const MessageStorage &rhs) const { return CompareFields(rhs); } //! Inequality operator. Defaulted fields do not take part in the comparison. bool operator!=(const MessageStorage &rhs) const { return !operator==(rhs); } private: template class T> class FieldComparator { public: FieldComparator(const MessageStorage &lhs, const MessageStorage &rhs) : m_lhs(lhs), m_rhs(rhs) { // Passing lhs and rhs to operator() and bind()ing them at the call site blows up VS2015 with alarming regularity, // hence passing to the constructor. } template void operator()(Identity) const { ACTIV_BOOST_STATIC_ASSERT((Boost::mpl::contains::type::value)); if (m_lhs.m_presentFieldIdBitmap[FIELD::id] && m_rhs.m_presentFieldIdBitmap[FIELD::id] && !m_lhs.IsFieldDefaulted() && !m_rhs.IsFieldDefaulted()) { typedef typename FIELD::Storage FieldStorage; const FieldStorage &lhsField(static_cast(m_lhs)); const FieldStorage &rhsField(static_cast(m_rhs)); if (!T()(lhsField, rhsField)) throw false; } } private: const MessageStorage &m_lhs; const MessageStorage &m_rhs; }; template class T> bool CompareFields(const MessageStorage &rhs) const { try { FieldComparator fieldComparator(*this, rhs); Boost::mpl::for_each >(fieldComparator); } ACTIV_CATCH_ALL() { return false; } return true; } template class SetOptionalField { public: SetOptionalField(MessageStorage &messageStorage, const T &fieldDispatcherSingleton) : m_messageStorage(messageStorage), m_fieldDispatcherSingleton(fieldDispatcherSingleton) { } void operator()(const FieldId fieldId) const { const typename T::IFieldDispatcher * const pIFieldDispatcher(m_fieldDispatcherSingleton.GetFieldDispatcher(fieldId)); if (ACTIV_LIKELY(ACTIV_NULLPTR != pIFieldDispatcher)) { pIFieldDispatcher->SetDefault(m_messageStorage); m_messageStorage.SetFieldPresent(fieldId); m_messageStorage.m_defaultedFieldIdBitmap[fieldId] = true; } else { assert(false); } } private: MessageStorage &m_messageStorage; const T & m_fieldDispatcherSingleton; }; //! Mark a field as being present in the message. void SetFieldPresent(const FieldId id) { if (!m_presentFieldIdBitmap[id]) { m_presentFieldIdList.push_back(id); m_presentFieldIdBitmap[id] = true; } } //! Set the values for any optional fields not present after a Deserialize(). Or SetOptional takes format. template void SetOptionalFieldValues(const T &fieldDispatcherSingleton) { SetOptionalField setOptionalField(*this, fieldDispatcherSingleton); Detail::ForNotPresentOptionalFields(this->m_presentFieldIdBitmap, setOptionalField); } FieldIdBitmap m_presentFieldIdBitmap; FieldIdBitmap m_defaultedFieldIdBitmap; FieldIdList m_presentFieldIdList; FieldIdList m_unknownFieldIdList; template friend class MessageStorageFormat; ACTIV_BOOST_COPYABLE_AND_MOVABLE(MessageStorage) }; } // namespace GenericMessages } // namespace Activ #endif // (!defined ACTIV_BASE_GENERIC_MESSAGES_MESSAGE_STORAGE_H)