From d72cfd72976b99f58ca6e69bec7c5dfafa7a02b4 Mon Sep 17 00:00:00 2001 From: Randolph Voorhies Date: Fri, 28 Jun 2013 14:53:47 -0700 Subject: [PATCH] OutputBinding initial implementation --- include/cereal/cereal.hpp | 4 +- include/cereal/details/polymorphic_impl.hpp | 43 ++++++++++++++++++--- include/cereal/types/polymorphic.hpp | 2 +- sandbox_rtti.cpp | 8 +--- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/include/cereal/cereal.hpp b/include/cereal/cereal.hpp index b77493be..fcac5baf 100644 --- a/include/cereal/cereal.hpp +++ b/include/cereal/cereal.hpp @@ -183,7 +183,7 @@ namespace cereal } //! Registers a pointer with the archive - uint32_t registerSharedPointer( void * addr ) + uint32_t registerSharedPointer( void const * addr ) { // Handle null pointers by just returning 0 if(addr == 0) return 0; @@ -307,7 +307,7 @@ namespace cereal std::unordered_set itsBaseClassSet; //! Maps from addresses to pointer ids - std::unordered_map itsSharedPointerMap; + std::unordered_map itsSharedPointerMap; //! The id to be given to the next pointer std::size_t itsCurrentPointerId; diff --git a/include/cereal/details/polymorphic_impl.hpp b/include/cereal/details/polymorphic_impl.hpp index a8a9b1c9..7d90fe84 100644 --- a/include/cereal/details/polymorphic_impl.hpp +++ b/include/cereal/details/polymorphic_impl.hpp @@ -42,6 +42,7 @@ #define CEREAL_DETAILS_POLYMORPHIC_IMPL_HPP_ #include +#include #include #include @@ -50,12 +51,25 @@ namespace cereal namespace detail { template - struct BindingMap + struct OutputBindingMap { - std::map> map; + //! A serializer function + typedef std::function Serializer; + + //! Struct containing the serializer functions for all pointer types + struct Serializers + { + Serializer shared_ptr, //!< Serializer function for shared/weak pointers + unique_ptr; //!< Serializer function for unique pointers + }; + + //! A map of serializers for pointers of all registered types + std::map map; }; - // Forward decls + //! An empty noop deleter + template struct EmptyDeleter { void operator()(T *) const {} }; + struct InputArchiveBase; struct OutputArchiveBase; @@ -65,8 +79,27 @@ namespace cereal { OutputBinding( ) { - StaticObject>::getInstance().map.insert( {std::type_index(typeid(T)), [](void*, void*){ std::cout << "hello" << std::endl; }} ); - //BindingMap::map.insert( {std::type_index(typeid(T)), [](void*, void*){ std::cout << "hello" << std::endl; }} ); + typename OutputBindingMap::Serializers serializers; + + serializers.shared_ptr = + [](void * arptr, void const * dptr) + { + Archive & ar = *static_cast(arptr); + std::shared_ptr const ptr(static_cast(dptr), EmptyDeleter()); + + ar( detail::make_ptr_wrapper(ptr) ); + }; + + serializers.unique_ptr = + [](void * arptr, void const * dptr) + { + Archive & ar = *static_cast(arptr); + std::unique_ptr> const ptr(static_cast(dptr)); + + ar( detail::make_ptr_wrapper(ptr) ); + }; + + StaticObject>::getInstance().map.insert( {std::type_index(typeid(T)), serializers} ); } }; diff --git a/include/cereal/types/polymorphic.hpp b/include/cereal/types/polymorphic.hpp index d66f4e55..c57b9cc3 100644 --- a/include/cereal/types/polymorphic.hpp +++ b/include/cereal/types/polymorphic.hpp @@ -28,7 +28,7 @@ #define CEREAL_TYPES_POLYMORPHIC_HPP_ #include -#include +#include #include //! Binds a polymorhic type to all registered archives diff --git a/sandbox_rtti.cpp b/sandbox_rtti.cpp index 5525d8bd..d1623460 100644 --- a/sandbox_rtti.cpp +++ b/sandbox_rtti.cpp @@ -33,14 +33,10 @@ struct MyType {}; CEREAL_BIND_TO_ARCHIVES(MyType); -template -void nop(T&&t) {} +template void nop(T&&t) {} + int main() { - cereal::detail::StaticObject< - cereal::detail::BindingMap - >::getInstance().map[std::type_index(typeid(MyType))](0, 0); - }