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.
This commit is contained in:
Shane Grant
2013-12-04 23:58:58 -08:00
parent e64ad60b4a
commit d30c21e5f8
2 changed files with 50 additions and 1 deletions

View File

@@ -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<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
/*! \sa virtual_base_class */
template <class T> inline
@@ -307,6 +328,18 @@ namespace cereal
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
template <class T> inline
typename std::enable_if<traits::is_specialized_non_member_serialize<T, ArchiveType>::value ||

View File

@@ -674,8 +674,24 @@ int main()
std::cerr << "-------------------------" << std::endl;
std::cout << cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
std::cout << cereal::traits::has_member_versioned_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
std::cout << cereal::traits::is_output_serializable<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
std::cout << cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
std::cout << (cereal::traits::is_output_serializable<BoostTransitionMS, cereal::BinaryOutputArchive>::value &&
cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>::value &&
cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>::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<int>, 1);
CEREAL_CLASS_VERSION(BoostTransitionMS, 1);