mostly documentation, some playing around with weak_ptr but it doesn't seem to be properly working

This commit is contained in:
Shane Grant
2013-06-29 11:50:51 -07:00
parent 0568675db1
commit 7f791d2af9
3 changed files with 47 additions and 12 deletions

View File

@@ -74,10 +74,18 @@ namespace cereal
struct binding_name {};
//! A structure holding a map from type_indices to output serializer functions
/*! A static object of this map should be created for each registered archive
type, containing entries for every registered type that describe how to
properly cast the type to its real type in polymorphic scenarios for
shared_ptr, weak_ptr, and unique_ptr. */
template <class Archive>
struct OutputBindingMap
{
//! A serializer function
/*! Serializer functions return nothing and take an archive as
their first parameter (will be cast properly inside the function,
and a pointer to actual data (contents of smart_ptr's get() function)
as their second parameter */
typedef std::function<void(void*, void const *)> Serializer;
//! Struct containing the serializer functions for all pointer types
@@ -92,11 +100,21 @@ namespace cereal
};
//! A structure holding a map from type name strings to input serializer functions
/*! A static object of this map should be created for each registered archive
type, containing entries for every registered type that describe how to
properly cast the type to its real type in polymorphic scenarios for
shared_ptr, weak_ptr, and unique_ptr. */
template <class Archive>
struct InputBindingMap
{
//! A serializer function
//! Shared ptr serializer function
/*! Serializer functions return nothing and take an archive as
their first parameter (will be cast properly inside the function,
and a shared_ptr (or unique_ptr for the unique case) of any base
type. Internally it will properly be loaded and cast to the
correct type. */
typedef std::function<void(void*, std::shared_ptr<void> & )> SharedSerializer;
//! Unique ptr serializer function
typedef std::function<void(void*, std::unique_ptr<void> & )> UniqueSerializer;
//! Struct containing the serializer functions for all pointer types
@@ -113,11 +131,18 @@ namespace cereal
//! An empty noop deleter
template<class T> struct EmptyDeleter { void operator()(T *) const {} };
// forward decls for archives from cereal.hpp
struct InputArchiveBase;
struct OutputArchiveBase;
//! Creates a binding (map entry) between an input archive type and a polymorphic type
/*! Bindings are made when types are registered, assuming that at least one
archive has already been registered. When this struct is created,
it will insert (at run time) an entry into a map that properly handles
casting for serializing polymorphic objects */
template <class Archive, class T> struct InputBindingCreator
{
//! Initialize the binding
InputBindingCreator()
{
typename InputBindingMap<Archive>::Serializers serializers;
@@ -148,8 +173,14 @@ namespace cereal
}
};
//! Creates a binding (map entry) between an output archive type and a polymorphic type
/*! Bindings are made when types are registered, assuming that at least one
archive has already been registered. When this struct is created,
it will insert (at run time) an entry into a map that properly handles
casting for serializing polymorphic objects */
template <class Archive, class T> struct OutputBindingCreator
{
//! Writes appropriate metadata to the archive for this polymorphic type
static void writeMetadata(Archive & ar)
{
// Register the polymorphic type name with the archive, and get the id
@@ -167,11 +198,11 @@ namespace cereal
}
}
//! Initialize the binding
OutputBindingCreator()
{
typename OutputBindingMap<Archive>::Serializers serializers;
serializers.shared_ptr =
[](void * arptr, void const * dptr)
{
@@ -243,6 +274,7 @@ namespace cereal
typedef instantiate_function<instantiate> unused;
};
// instantiate implementation
template <class Archive, class T>
void polymorphic_serialization_support<Archive,T>::instantiate()
{

View File

@@ -31,7 +31,7 @@
#include <cereal/types/memory.hpp>
#include <cereal/details/polymorphic_impl.hpp>
#include <cereal/details/util.hpp>
#include <iostream>
//! Registers a polymorphic type with cereal
/*! Polymorphic types must be registered before pointers
to them can be serialized. This also assumes that
@@ -152,13 +152,9 @@ namespace cereal
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
save( Archive & ar, std::weak_ptr<T> const & ptr )
{
if(!ptr)
{
// same behavior as nullptr in memory implementation
ar( std::uint32_t(0) );
return;
}
std::cout << "poly weak" << std::endl;
auto sptr = ptr.lock();
ar( sptr );
}
//! Loading std::weak_ptr for polymorphic types
@@ -166,6 +162,9 @@ namespace cereal
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
load( Archive & ar, std::weak_ptr<T> & ptr )
{
std::shared_ptr<T> sptr;
ar( sptr );
ptr = sptr;
}
//! Saving std::unique_ptr for polymorphic types

View File

@@ -96,10 +96,12 @@ int main()
std::shared_ptr<Base> ptr1 = std::make_shared<MyType>();
std::shared_ptr<Base> ptr2 = std::make_shared<YourType>();
std::unique_ptr<Base> ptr3(new MyType);
std::weak_ptr<Base> ptr4 = ptr2;
oarchive(ptr1);
oarchive(ptr2);
oarchive(ptr3);
oarchive(ptr4);
}
@@ -111,10 +113,12 @@ int main()
std::shared_ptr<Base> ptr1;
std::shared_ptr<Base> ptr2;
std::unique_ptr<Base> ptr3;
std::weak_ptr<Base> ptr4;
iarchive(ptr1);
iarchive(ptr2);
iarchive(ptr3);
iarchive(ptr4);
}
}