Boost transition layer saving working

See issue #8
This commit is contained in:
Shane Grant
2013-12-08 13:28:39 -08:00
parent d30c21e5f8
commit 9a3b1a339c
2 changed files with 144 additions and 53 deletions

View File

@@ -220,7 +220,7 @@ namespace cereal
@internal @internal
@param addr The address (see shared_ptr get()) pointed to by the shared pointer @param addr The address (see shared_ptr get()) pointed to by the shared pointer
@return A key that uniquely identifies the 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 // Handle null pointers by just returning 0
if(addr == 0) return 0; if(addr == 0) return 0;
@@ -244,7 +244,7 @@ namespace cereal
@internal @internal
@param name The name to associate with a polymorphic type @param name The name to associate with a polymorphic type
@return A key that uniquely identifies the polymorphic type name */ @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 ); auto id = itsPolymorphicTypeMap.find( name );
if( id == itsPolymorphicTypeMap.end() ) if( id == itsPolymorphicTypeMap.end() )
@@ -275,24 +275,6 @@ namespace cereal
self->process( std::forward<Other>( tail )... ); self->process( std::forward<Other>( 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 <class T> 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<ArchiveType>("cereal_class_version", version) );
}
//! Serialization of a virtual_base_class wrapper //! Serialization of a virtual_base_class wrapper
/*! \sa virtual_base_class */ /*! \sa virtual_base_class */
@@ -328,18 +310,6 @@ namespace cereal
return *self; return *self;
} }
//! Member serialization
/*! Boost Transition Layer version */
template <class T> inline
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_member_versioned_serialize<T, ArchiveType>::value,
ArchiveType &>::type
processImpl(T const & t)
{
registerClassVersion<T>( detail::Version<T>::version );
access::member_serialize(*self, const_cast<T &>(t), detail::Version<T>::version);
return *self;
}
//! Non member serialization //! Non member serialization
template <class T> inline template <class T> inline
typename std::enable_if<traits::is_specialized_non_member_serialize<T, ArchiveType>::value || typename std::enable_if<traits::is_specialized_non_member_serialize<T, ArchiveType>::value ||
@@ -401,6 +371,74 @@ namespace cereal
return *self; 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 <class T> 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<ArchiveType>("cereal_class_version", version) );
}
//! Member serialization
/*! Boost Transition Layer version */
template <class T> inline
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_member_versioned_serialize<T, ArchiveType>::value,
ArchiveType &>::type
processImpl(T const & t)
{
registerClassVersion<T>( detail::Version<T>::version );
access::member_serialize(*self, const_cast<T &>(t), detail::Version<T>::version);
return *self;
}
//! Non member serialization
/*! Boost Transition Layer version */
template <class T> inline
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_non_member_versioned_serialize<T, ArchiveType>::value,
ArchiveType &>::type
processImpl(T const & t)
{
registerClassVersion<T>( detail::Version<T>::version );
serialize(*self, const_cast<T &>(t), detail::Version<T>::version);
return *self;
}
//! Member split (save)
/*! Boost Transition Layer version */
template <class T> inline
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_member_versioned_save<T, ArchiveType>::value,
ArchiveType &>::type
processImpl(T const & t)
{
registerClassVersion<T>( detail::Version<T>::version );
access::member_save(*self, t, detail::Version<T>::version);
return *self;
}
//! Non member split (save)
/*! Boost Transition Layer version */
template <class T> inline
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_non_member_versioned_save<T, ArchiveType>::value,
ArchiveType &>::type
processImpl(T const & t)
{
registerClassVersion<T>( detail::Version<T>::version );
save(*self, t, detail::Version<T>::version);
return *self;
}
//! @}
private: private:
ArchiveType * const self; ArchiveType * const self;
@@ -491,7 +529,7 @@ namespace cereal
@param id The unique id that was serialized for the pointer @param id The unique id that was serialized for the pointer
@return A shared pointer to the data */ @return A shared pointer to the data */
std::shared_ptr<void> getSharedPointer(std::uint32_t const id) inline std::shared_ptr<void> getSharedPointer(std::uint32_t const id)
{ {
if(id == 0) return std::shared_ptr<void>(nullptr); if(id == 0) return std::shared_ptr<void>(nullptr);
@@ -508,7 +546,7 @@ namespace cereal
@param id The unique identifier for the shared pointer @param id The unique identifier for the shared pointer
@param ptr The actual shared pointer */ @param ptr The actual shared pointer */
void registerSharedPointer(std::uint32_t const id, std::shared_ptr<void> ptr) inline void registerSharedPointer(std::uint32_t const id, std::shared_ptr<void> ptr)
{ {
std::uint32_t const stripped_id = id & ~detail::msb_32bit; std::uint32_t const stripped_id = id & ~detail::msb_32bit;
itsSharedPointerMap.insert( {stripped_id, ptr} ); itsSharedPointerMap.insert( {stripped_id, ptr} );
@@ -520,7 +558,7 @@ namespace cereal
@param id The unique id that was serialized for the polymorphic type @param id The unique id that was serialized for the polymorphic type
@return The string identifier for the tyep */ @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 ); auto name = itsPolymorphicTypeMap.find( id );
if(name == itsPolymorphicTypeMap.end()) if(name == itsPolymorphicTypeMap.end())
@@ -536,7 +574,7 @@ namespace cereal
@param id The unique identifier for the polymorphic type @param id The unique identifier for the polymorphic type
@param name The name associated with the tyep */ @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; std::uint32_t const stripped_id = id & ~detail::msb_32bit;
itsPolymorphicTypeMap.insert( {stripped_id, name} ); itsPolymorphicTypeMap.insert( {stripped_id, name} );

View File

@@ -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 <class Archive> template <class Archive>
//void serialize( Archive & ar ) void serialize( Archive & ar, const std::uint32_t version )
//{ { ar( x ); }
// ar( x );
//}
template <class Archive>
void serialize( Archive & ar, const std::uint32_t version )
{
std::cout << "BoostTransitionMS " << version << std::endl;
ar( x );
}
}; };
class BoostTransitionSplit
{
public:
BoostTransitionSplit( int xx ) : x(xx) {}
private:
friend class cereal::access;
int x;
template <class Archive>
void save( Archive & ar, const std::uint32_t version ) const
{ ar( x ); }
template <class Archive>
void load( Archive & ar, const std::uint32_t version )
{ ar( x ); }
};
class BoostTransitionNMS
{
public:
BoostTransitionNMS( int xx ) : x(xx) {}
int x;
};
template <class Archive>
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 <class Archive>
void save( Archive & ar, BoostTransitionNMSplit const & bnsplit, const std::uint32_t version )
{ ar( bnsplit.x ); }
template <class Archive>
void load( Archive & ar, BoostTransitionNMSplit & bnsplit, const std::uint32_t version )
{ ar( bnsplit.x ); }
// ###################################################################### // ######################################################################
int main() int main()
{ {
@@ -681,17 +721,30 @@ int main()
cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>::value && cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>::value &&
cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>::value )<< std::endl; cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>::value )<< std::endl;
std::cout << "---------snarf" << std::endl;
std::cout << cereal::traits::has_non_member_serialize<BoostTransitionNMS, cereal::BinaryOutputArchive>() << std::endl;
{ {
// Boost transition layer stuff // Boost transition layer stuff
cereal::XMLOutputArchive ar(std::cout); cereal::XMLOutputArchive ar(std::cout);
BoostTransitionMS b(3); BoostTransitionMS b(3);
ar( b, b );
ar( b ); BoostTransitionSplit c(4);
ar( b ); ar( c, c );
BoostTransitionNMS d(5);
ar( d, d );
BoostTransitionNMSplit e(32);
ar( e, e );
} }
return 0; return 0;
} }
CEREAL_CLASS_VERSION(BoostTransitionMS, 1); CEREAL_CLASS_VERSION(BoostTransitionMS, 1);
CEREAL_CLASS_VERSION(BoostTransitionSplit, 2);
CEREAL_CLASS_VERSION(BoostTransitionNMS, 3);
// keep the other at default version (0)