Basic polymorphism serialization works!

This commit is contained in:
Randolph Voorhies
2013-06-28 15:29:40 -07:00
parent 1cb49610b6
commit f1b42507d7
2 changed files with 14 additions and 10 deletions

View File

@@ -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

View File

@@ -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);
} }