diff --git a/include/cereal/types/polymorphic.hpp b/include/cereal/types/polymorphic.hpp index c57b9cc3..c194bf43 100644 --- a/include/cereal/types/polymorphic.hpp +++ b/include/cereal/types/polymorphic.hpp @@ -56,6 +56,13 @@ namespace cereal typename std::enable_if::value, void>::type save( Archive & ar, std::shared_ptr const & ptr ) { + auto & bindingMap = detail::StaticObject>::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 diff --git a/sandbox_rtti.cpp b/sandbox_rtti.cpp index 74b91978..a6242c16 100644 --- a/sandbox_rtti.cpp +++ b/sandbox_rtti.cpp @@ -28,6 +28,7 @@ #include #include #include +#include struct Base { @@ -36,6 +37,7 @@ struct Base template void serialize(Archive & ar) { + std::cout << "Serializing Base" << std::endl; } }; @@ -46,6 +48,7 @@ struct MyType : public Base template void serialize(Archive & ar) { + std::cout << "Serializing MyType" << std::endl; } }; @@ -55,16 +58,10 @@ template void nop(T&&t) {} int main() { - cereal::BinaryOutputArchive archive(std::cout); - //auto ptr = std::make_shared(); - - - std::shared_ptr const ptr(new int); - - archive(cereal::detail::make_ptr_wrapper(ptr)); - - //cereal::detail::PtrWrapper > &> - + std::stringstream stream; + cereal::BinaryOutputArchive archive(stream); + std::shared_ptr ptr = std::make_shared(); + archive(ptr); }