From d30c21e5f82aae1c302eadbefebae326555f120c Mon Sep 17 00:00:00 2001 From: Shane Grant Date: Wed, 4 Dec 2013 23:58:58 -0800 Subject: [PATCH] Serialize (save) working with versioning Simple case of making these functions for the rest of the output archive serialization functions and then adding it to load. Progress towards issue #8. --- include/cereal/cereal.hpp | 33 +++++++++++++++++++++++++++++++++ sandbox.cpp | 18 +++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/cereal/cereal.hpp b/include/cereal/cereal.hpp index 2606efc1..7465ac3c 100644 --- a/include/cereal/cereal.hpp +++ b/include/cereal/cereal.hpp @@ -217,6 +217,7 @@ namespace cereal unnecessary saves from taking place if multiple shared pointers point to the same data. + @internal @param addr The address (see shared_ptr get()) pointed to by the shared pointer @return A key that uniquely identifies the pointer */ std::uint32_t registerSharedPointer( void const * addr ) @@ -240,6 +241,7 @@ namespace cereal unnecessary saves of identifying strings used by the polymorphic support functionality. + @internal @param name The name to associate with a polymorphic type @return A key that uniquely identifies the polymorphic type name */ std::uint32_t registerPolymorphicType( char const * name ) @@ -273,6 +275,25 @@ namespace cereal self->process( std::forward( tail )... ); } + /*! @name Boost Transition Layer (private) + Specific private functionality and overrides for enabling the Boost Transition Layer */ + //! @{ + + //! Registers a class version with the archive and serializes it if necessary + /*! If this is the first time this class has been serialized, we will record its + version number and serialize that. + + @tparam T The type of the class being serialized + @param version The version number associated with it */ + template inline + void registerClassVersion( const std::uint32_t version ) + { + const auto insertResult = itsVersionedTypes.insert( std::type_index(typeid(T)).hash_code() ); + if( insertResult.second ) // insertion took place, serialize the version number + process( make_nvp("cereal_class_version", version) ); + } + + //! Serialization of a virtual_base_class wrapper /*! \sa virtual_base_class */ template inline @@ -307,6 +328,18 @@ namespace cereal return *self; } + //! Member serialization + /*! Boost Transition Layer version */ + template inline + typename std::enable_if::value && traits::has_member_versioned_serialize::value, + ArchiveType &>::type + processImpl(T const & t) + { + registerClassVersion( detail::Version::version ); + access::member_serialize(*self, const_cast(t), detail::Version::version); + return *self; + } + //! Non member serialization template inline typename std::enable_if::value || diff --git a/sandbox.cpp b/sandbox.cpp index 3ef92850..4fab041b 100644 --- a/sandbox.cpp +++ b/sandbox.cpp @@ -674,8 +674,24 @@ int main() std::cerr << "-------------------------" << std::endl; std::cout << cereal::traits::has_member_serialize() << std::endl; std::cout << cereal::traits::has_member_versioned_serialize() << std::endl; + std::cout << cereal::traits::is_output_serializable() << std::endl; + std::cout << cereal::traits::is_output_versioned() << std::endl; + + std::cout << (cereal::traits::is_output_serializable::value && + cereal::traits::has_member_serialize::value && + cereal::traits::is_output_versioned::value )<< std::endl; + + { + // Boost transition layer stuff + cereal::XMLOutputArchive ar(std::cout); + + BoostTransitionMS b(3); + + ar( b ); + ar( b ); + } return 0; } -CEREAL_CLASS_VERSION(std::vector, 1); +CEREAL_CLASS_VERSION(BoostTransitionMS, 1);