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
|
typename std::enable_if<std::is_polymorphic<T>::value, void>::type
|
||||||
save( Archive & ar, std::shared_ptr<T> const & ptr )
|
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
|
//! Loading std::shared_ptr, case when user load and allocate for polymorphic types
|
||||||
|
|||||||
@@ -28,6 +28,7 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <cereal/archives/binary.hpp>
|
#include <cereal/archives/binary.hpp>
|
||||||
#include <cereal/types/polymorphic.hpp>
|
#include <cereal/types/polymorphic.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
struct Base
|
struct Base
|
||||||
{
|
{
|
||||||
@@ -36,6 +37,7 @@ struct Base
|
|||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive & ar)
|
void serialize(Archive & ar)
|
||||||
{
|
{
|
||||||
|
std::cout << "Serializing Base" << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -46,6 +48,7 @@ struct MyType : public Base
|
|||||||
template<class Archive>
|
template<class Archive>
|
||||||
void serialize(Archive & ar)
|
void serialize(Archive & ar)
|
||||||
{
|
{
|
||||||
|
std::cout << "Serializing MyType" << std::endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -55,16 +58,10 @@ template <class T> void nop(T&&t) {}
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
cereal::BinaryOutputArchive archive(std::cout);
|
std::stringstream stream;
|
||||||
//auto ptr = std::make_shared<MyType>();
|
cereal::BinaryOutputArchive archive(stream);
|
||||||
|
|
||||||
|
|
||||||
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::shared_ptr<Base> ptr = std::make_shared<MyType>();
|
||||||
|
|
||||||
|
archive(ptr);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user