/** * @file include/ActivMiddleware/Misc/Detail/SerializableContainerHelperSpecializations.h * * @brief Classes to help serialize STL containers. */ #if (!defined ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_SPECIALIZATIONS_H) #define ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_SPECIALIZATIONS_H #include "ActivMiddleware/Misc/StlHelper.h" #include ACTIV_BOOST_INCLUDE(container/list.hpp) #include ACTIV_BOOST_INCLUDE(container/map.hpp) #include ACTIV_BOOST_INCLUDE(container/set.hpp) #include ACTIV_BOOST_INCLUDE(container/vector.hpp) //! Activ namespace. namespace Activ { /** * @brief SerializableContainerHelper specialization for std::vector containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef std::vector Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a vector. * * @param messageBuilder * @param vector container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &vector) { return SerializeContainer(messageBuilder, vector); } /** * @brief Deserialize a vector. * * @param messageValidator * @param vector container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &vector, const bool shouldClearContainer) { return DeserializeVector(messageValidator, vector, shouldClearContainer); } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief SerializableContainerHelper specialization for Boost::container::vector containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef Boost::container::vector Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a vector. * * @param messageBuilder * @param vector container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &vector) { return SerializeContainer(messageBuilder, vector); } /** * @brief Deserialize a vector. * * @param messageValidator * @param vector container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &vector, const bool shouldClearContainer) { return DeserializeVector(messageValidator, vector, shouldClearContainer); } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for list containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef std::list Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a list. * * @param messageBuilder * @param list container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &list) { return SerializeContainer(messageBuilder, list); } /** * @brief Deserialize a list. * * @param messageValidator * @param list container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &list, const bool shouldClearContainer) { Count count; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange(count))); // TODO could probably be cleverer here and deserialize into existing members if shouldClearContainer. if (shouldClearContainer) list.clear(); const size_t originalSize = list.size(); StatusCode statusCode; for (size_t i = 0; i < count; ++i) { list.push_back(OBJECT_TYPE()); // Deserialize directly into the object in the list. statusCode = ObjectHelper::Deserialize(messageValidator, list.back()); if (STATUS_CODE_SUCCESS != statusCode) { list.resize(originalSize); return statusCode; } } return STATUS_CODE_SUCCESS; } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for Boost::container::list containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef Boost::container::list Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a list. * * @param messageBuilder * @param list container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &list) { return SerializeContainer(messageBuilder, list); } /** * @brief Deserialize a list. * * @param messageValidator * @param list container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &list, const bool shouldClearContainer) { Count count; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange(count))); // TODO could probably be cleverer here and deserialize into existing members if shouldClearContainer. if (shouldClearContainer) list.clear(); const size_t originalSize = list.size(); StatusCode statusCode; for (size_t i = 0; i < count; ++i) { list.push_back(OBJECT_TYPE()); // Deserialize directly into the object in the list. statusCode = ObjectHelper::Deserialize(messageValidator, list.back()); if (STATUS_CODE_SUCCESS != statusCode) { list.resize(originalSize); return statusCode; } } return STATUS_CODE_SUCCESS; } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for set containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef std::set Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a set. * * @param messageBuilder * @param set container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &set) { return SerializeContainer(messageBuilder, set); } /** * @brief Deserialize a set. * * @param messageValidator * @param set container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &set, const bool shouldClearContainer) { Count count; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange(count))); if (shouldClearContainer) set.clear(); for (Count i = 0; i < count; ++i) { typename Container::value_type object; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, ObjectHelper::Deserialize(messageValidator, object)); (void) set.insert(object); } return STATUS_CODE_SUCCESS; } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for Boost::container::set containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef Boost::container::set Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a set. * * @param messageBuilder * @param set container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &set) { return SerializeContainer(messageBuilder, set); } /** * @brief Deserialize a set. * * @param messageValidator * @param set container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &set, const bool shouldClearContainer) { Count count; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, (messageValidator.ValidateBinaryIntegralRange(count))); if (shouldClearContainer) set.clear(); for (Count i = 0; i < count; ++i) { typename Container::value_type object; ACTIV_RETURN_RESULT_IF_UNLIKELY(STATUS_CODE_SUCCESS, !=, ObjectHelper::Deserialize(messageValidator, object)); (void) set.insert(object); } return STATUS_CODE_SUCCESS; } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for map containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef std::map Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a map. * * @param messageBuilder * @param map container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &map) { return SerializeAssociativeContainer(messageBuilder, map); } /** * @brief Deserialize a map. * * @param messageValidator * @param map container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &map, const bool shouldClearContainer) { return DeserializeAssociativeContainer(messageValidator, map, shouldClearContainer); } }; // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Specialization for Boost::container::map containers. */ template class SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, OBJECT_HELPER> { public: typedef Boost::container::map Container; typedef COUNT_TYPE Count; typedef OBJECT_HELPER ObjectHelper; /** * @brief Serialize a map. * * @param messageBuilder * @param map container to serialize. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Serialize(MessageBuilder &messageBuilder, const Container &map) { return SerializeAssociativeContainer(messageBuilder, map); } /** * @brief Deserialize a map. * * @param messageValidator * @param map container to deserialize into. * @param shouldClearContainer whether to empty the container first, or just add new entries. * * @retval STATUS_CODE_SUCCESS * @retval ... */ static StatusCode Deserialize(MessageValidator &messageValidator, Container &map, const bool shouldClearContainer) { return DeserializeAssociativeContainer(messageValidator, map, shouldClearContainer); } }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Partial specialization of SerializableObjectContainerHelper to serialize maps of objects. */ template class SerializableObjectContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableObjectHelper > { }; // --------------------------------------------------------------------------------------------------------------------------------- template class SerializableObjectContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableObjectHelper > { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Partial specialization of SerializableRawPointerContainerHelper to serialize maps of raw pointers to objects. * * Note that currently the key type must not be a pointer, the mapped type should be a raw pointer. */ template class SerializableRawPointerContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableRawPointerHelper > { }; // --------------------------------------------------------------------------------------------------------------------------------- template class SerializableRawPointerContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableRawPointerHelper > { }; // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- /** * @brief Partial specialization of helper class to serialize maps of smart pointers to objects. * * Note that currently the key type must not be a pointer, the mapped type should be a smart pointer. */ template class SerializableSmartPointerContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableSmartPointerHelper > { }; // --------------------------------------------------------------------------------------------------------------------------------- template class SerializableSmartPointerContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH> : public SerializableContainerHelper, COUNT_TYPE, MIN_LENGTH, MAX_LENGTH, SerializableSmartPointerHelper > { }; } // namespace Activ #endif // (!defined ACTIV_MISC_DETAIL_SERIALIZABLE_CONTAINER_HELPER_SPECIALIZATIONS_H)