Skip to content

Commit d30c21e

Browse files
committed
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.
1 parent e64ad60 commit d30c21e

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

include/cereal/cereal.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ namespace cereal
217217
unnecessary saves from taking place if multiple shared pointers
218218
point to the same data.
219219
220+
@internal
220221
@param addr The address (see shared_ptr get()) pointed to by the shared pointer
221222
@return A key that uniquely identifies the pointer */
222223
std::uint32_t registerSharedPointer( void const * addr )
@@ -240,6 +241,7 @@ namespace cereal
240241
unnecessary saves of identifying strings used by the polymorphic
241242
support functionality.
242243
244+
@internal
243245
@param name The name to associate with a polymorphic type
244246
@return A key that uniquely identifies the polymorphic type name */
245247
std::uint32_t registerPolymorphicType( char const * name )
@@ -273,6 +275,25 @@ namespace cereal
273275
self->process( std::forward<Other>( tail )... );
274276
}
275277

278+
/*! @name Boost Transition Layer (private)
279+
Specific private functionality and overrides for enabling the Boost Transition Layer */
280+
//! @{
281+
282+
//! Registers a class version with the archive and serializes it if necessary
283+
/*! If this is the first time this class has been serialized, we will record its
284+
version number and serialize that.
285+
286+
@tparam T The type of the class being serialized
287+
@param version The version number associated with it */
288+
template <class T> inline
289+
void registerClassVersion( const std::uint32_t version )
290+
{
291+
const auto insertResult = itsVersionedTypes.insert( std::type_index(typeid(T)).hash_code() );
292+
if( insertResult.second ) // insertion took place, serialize the version number
293+
process( make_nvp<ArchiveType>("cereal_class_version", version) );
294+
}
295+
296+
276297
//! Serialization of a virtual_base_class wrapper
277298
/*! \sa virtual_base_class */
278299
template <class T> inline
@@ -307,6 +328,18 @@ namespace cereal
307328
return *self;
308329
}
309330

331+
//! Member serialization
332+
/*! Boost Transition Layer version */
333+
template <class T> inline
334+
typename std::enable_if<traits::is_output_serializable<T, ArchiveType>::value && traits::has_member_versioned_serialize<T, ArchiveType>::value,
335+
ArchiveType &>::type
336+
processImpl(T const & t)
337+
{
338+
registerClassVersion<T>( detail::Version<T>::version );
339+
access::member_serialize(*self, const_cast<T &>(t), detail::Version<T>::version);
340+
return *self;
341+
}
342+
310343
//! Non member serialization
311344
template <class T> inline
312345
typename std::enable_if<traits::is_specialized_non_member_serialize<T, ArchiveType>::value ||

sandbox.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,8 +674,24 @@ int main()
674674
std::cerr << "-------------------------" << std::endl;
675675
std::cout << cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
676676
std::cout << cereal::traits::has_member_versioned_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
677+
std::cout << cereal::traits::is_output_serializable<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
678+
std::cout << cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>() << std::endl;
679+
680+
std::cout << (cereal::traits::is_output_serializable<BoostTransitionMS, cereal::BinaryOutputArchive>::value &&
681+
cereal::traits::has_member_serialize<BoostTransitionMS, cereal::BinaryOutputArchive>::value &&
682+
cereal::traits::is_output_versioned<BoostTransitionMS, cereal::BinaryOutputArchive>::value )<< std::endl;
683+
684+
{
685+
// Boost transition layer stuff
686+
cereal::XMLOutputArchive ar(std::cout);
687+
688+
BoostTransitionMS b(3);
689+
690+
ar( b );
691+
ar( b );
692+
}
677693

678694
return 0;
679695
}
680696

681-
CEREAL_CLASS_VERSION(std::vector<int>, 1);
697+
CEREAL_CLASS_VERSION(BoostTransitionMS, 1);

0 commit comments

Comments
 (0)