mirror of
https://github.com/USCiLab/cereal.git
synced 2025-10-18 01:45:52 +02:00
Basic polymorphism serialization works!
This commit is contained in:
@@ -56,6 +56,13 @@ namespace cereal
|
||||
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
|
||||
save( Archive & ar, std::shared_ptr<T> const & ptr )
|
||||
{
|
||||
auto & bindingMap = detail::StaticObject<detail::OutputBindingMap<Archive>>::getInstance().map;
|
||||
|
||||
auto binding = bindingMap.find(std::type_index(typeid(*ptr.get())));
|
||||
if(binding == bindingMap.end())
|
||||
throw cereal::Exception("Trying to serialize unregistered polymorphic type");
|
||||
|
||||
binding->second.shared_ptr(&ar, ptr.get());
|
||||
}
|
||||
|
||||
//! Loading std::shared_ptr, case when user load and allocate for polymorphic types
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <type_traits>
|
||||
#include <cereal/archives/binary.hpp>
|
||||
#include <cereal/types/polymorphic.hpp>
|
||||
#include <sstream>
|
||||
|
||||
struct Base
|
||||
{
|
||||
@@ -36,6 +37,7 @@ struct Base
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar)
|
||||
{
|
||||
std::cout << "Serializing Base" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -46,6 +48,7 @@ struct MyType : public Base
|
||||
template<class Archive>
|
||||
void serialize(Archive & ar)
|
||||
{
|
||||
std::cout << "Serializing MyType" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -55,16 +58,10 @@ template <class T> void nop(T&&t) {}
|
||||
|
||||
int main()
|
||||
{
|
||||
cereal::BinaryOutputArchive archive(std::cout);
|
||||
//auto ptr = std::make_shared<MyType>();
|
||||
|
||||
|
||||
std::shared_ptr<int> const ptr(new int);
|
||||
|
||||
archive(cereal::detail::make_ptr_wrapper(ptr));
|
||||
|
||||
//cereal::detail::PtrWrapper<const std::unique_ptr<const MyType, cereal::detail::EmptyDeleter<const MyType> > &>
|
||||
|
||||
std::stringstream stream;
|
||||
cereal::BinaryOutputArchive archive(stream);
|
||||
|
||||
std::shared_ptr<Base> ptr = std::make_shared<MyType>();
|
||||
|
||||
archive(ptr);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user