From 9a3b1a339c9cbf50c3e26cd992c2976fdcf33b37 Mon Sep 17 00:00:00 2001 From: Shane Grant Date: Sun, 8 Dec 2013 13:28:39 -0800 Subject: [PATCH] Boost transition layer saving working See issue #8 --- include/cereal/cereal.hpp | 110 +++++++++++++++++++++++++------------- sandbox.cpp | 87 ++++++++++++++++++++++++------ 2 files changed, 144 insertions(+), 53 deletions(-) diff --git a/include/cereal/cereal.hpp b/include/cereal/cereal.hpp index 7465ac3c..163b8f58 100644 --- a/include/cereal/cereal.hpp +++ b/include/cereal/cereal.hpp @@ -220,7 +220,7 @@ namespace cereal @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 ) + inline std::uint32_t registerSharedPointer( void const * addr ) { // Handle null pointers by just returning 0 if(addr == 0) return 0; @@ -244,7 +244,7 @@ namespace cereal @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 ) + inline std::uint32_t registerPolymorphicType( char const * name ) { auto id = itsPolymorphicTypeMap.find( name ); if( id == itsPolymorphicTypeMap.end() ) @@ -275,24 +275,6 @@ 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 */ @@ -328,18 +310,6 @@ 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 || @@ -401,6 +371,74 @@ namespace cereal return *self; } + /*! @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) ); + } + + //! 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 + /*! Boost Transition Layer version */ + template inline + typename std::enable_if::value && traits::has_non_member_versioned_serialize::value, + ArchiveType &>::type + processImpl(T const & t) + { + registerClassVersion( detail::Version::version ); + serialize(*self, const_cast(t), detail::Version::version); + return *self; + } + + //! Member split (save) + /*! Boost Transition Layer version */ + template inline + typename std::enable_if::value && traits::has_member_versioned_save::value, + ArchiveType &>::type + processImpl(T const & t) + { + registerClassVersion( detail::Version::version ); + access::member_save(*self, t, detail::Version::version); + return *self; + } + + //! Non member split (save) + /*! Boost Transition Layer version */ + template inline + typename std::enable_if::value && traits::has_non_member_versioned_save::value, + ArchiveType &>::type + processImpl(T const & t) + { + registerClassVersion( detail::Version::version ); + save(*self, t, detail::Version::version); + return *self; + } + + //! @} + private: ArchiveType * const self; @@ -491,7 +529,7 @@ namespace cereal @param id The unique id that was serialized for the pointer @return A shared pointer to the data */ - std::shared_ptr getSharedPointer(std::uint32_t const id) + inline std::shared_ptr getSharedPointer(std::uint32_t const id) { if(id == 0) return std::shared_ptr(nullptr); @@ -508,7 +546,7 @@ namespace cereal @param id The unique identifier for the shared pointer @param ptr The actual shared pointer */ - void registerSharedPointer(std::uint32_t const id, std::shared_ptr ptr) + inline void registerSharedPointer(std::uint32_t const id, std::shared_ptr ptr) { std::uint32_t const stripped_id = id & ~detail::msb_32bit; itsSharedPointerMap.insert( {stripped_id, ptr} ); @@ -520,7 +558,7 @@ namespace cereal @param id The unique id that was serialized for the polymorphic type @return The string identifier for the tyep */ - std::string getPolymorphicName(std::uint32_t const id) + inline std::string getPolymorphicName(std::uint32_t const id) { auto name = itsPolymorphicTypeMap.find( id ); if(name == itsPolymorphicTypeMap.end()) @@ -536,7 +574,7 @@ namespace cereal @param id The unique identifier for the polymorphic type @param name The name associated with the tyep */ - void registerPolymorphicName(std::uint32_t const id, std::string const & name) + inline void registerPolymorphicName(std::uint32_t const id, std::string const & name) { std::uint32_t const stripped_id = id & ~detail::msb_32bit; itsPolymorphicTypeMap.insert( {stripped_id, name} ); diff --git a/sandbox.cpp b/sandbox.cpp index 4fab041b..58cae90e 100644 --- a/sandbox.cpp +++ b/sandbox.cpp @@ -380,26 +380,66 @@ void test_unordered_loads() } } -struct BoostTransitionMS +class BoostTransitionMS { - BoostTransitionMS( int xx ) : x(xx) {} + public: + BoostTransitionMS( int xx ) : x(xx) {} - int x; + private: + friend class cereal::access; + int x; - //template - //void serialize( Archive & ar ) - //{ - // ar( x ); - //} - - template - void serialize( Archive & ar, const std::uint32_t version ) - { - std::cout << "BoostTransitionMS " << version << std::endl; - ar( x ); - } + template + void serialize( Archive & ar, const std::uint32_t version ) + { ar( x ); } }; +class BoostTransitionSplit +{ + public: + BoostTransitionSplit( int xx ) : x(xx) {} + + private: + friend class cereal::access; + int x; + + template + void save( Archive & ar, const std::uint32_t version ) const + { ar( x ); } + + template + void load( Archive & ar, const std::uint32_t version ) + { ar( x ); } +}; + +class BoostTransitionNMS +{ + public: + BoostTransitionNMS( int xx ) : x(xx) {} + + int x; +}; + +template +void serialize( Archive & ar, BoostTransitionNMS & bnms, const std::uint32_t version ) +{ ar( bnms.x ); } + +struct BoostTransitionNMSplit +{ + public: + BoostTransitionNMSplit( int xx ) : x(xx) {} + + int x; +}; + +template +void save( Archive & ar, BoostTransitionNMSplit const & bnsplit, const std::uint32_t version ) +{ ar( bnsplit.x ); } + +template +void load( Archive & ar, BoostTransitionNMSplit & bnsplit, const std::uint32_t version ) +{ ar( bnsplit.x ); } + // ###################################################################### int main() { @@ -681,17 +721,30 @@ int main() cereal::traits::has_member_serialize::value && cereal::traits::is_output_versioned::value )<< std::endl; + std::cout << "---------snarf" << std::endl; + std::cout << cereal::traits::has_non_member_serialize() << std::endl; + { // Boost transition layer stuff cereal::XMLOutputArchive ar(std::cout); BoostTransitionMS b(3); + ar( b, b ); - ar( b ); - ar( b ); + BoostTransitionSplit c(4); + ar( c, c ); + + BoostTransitionNMS d(5); + ar( d, d ); + + BoostTransitionNMSplit e(32); + ar( e, e ); } return 0; } CEREAL_CLASS_VERSION(BoostTransitionMS, 1); +CEREAL_CLASS_VERSION(BoostTransitionSplit, 2); +CEREAL_CLASS_VERSION(BoostTransitionNMS, 3); +// keep the other at default version (0)