OutputBinding initial implementation

This commit is contained in:
Randolph Voorhies
2013-06-28 14:53:47 -07:00
parent 35f538ecae
commit d72cfd7297
4 changed files with 43 additions and 14 deletions

View File

@@ -183,7 +183,7 @@ namespace cereal
} }
//! Registers a pointer with the archive //! Registers a pointer with the archive
uint32_t registerSharedPointer( void * addr ) uint32_t registerSharedPointer( void const * addr )
{ {
// Handle null pointers by just returning 0 // Handle null pointers by just returning 0
if(addr == 0) return 0; if(addr == 0) return 0;
@@ -307,7 +307,7 @@ namespace cereal
std::unordered_set<traits::detail::base_class_id, traits::detail::base_class_id_hash> itsBaseClassSet; std::unordered_set<traits::detail::base_class_id, traits::detail::base_class_id_hash> itsBaseClassSet;
//! Maps from addresses to pointer ids //! Maps from addresses to pointer ids
std::unordered_map<void *, std::size_t> itsSharedPointerMap; std::unordered_map<void const *, std::size_t> itsSharedPointerMap;
//! The id to be given to the next pointer //! The id to be given to the next pointer
std::size_t itsCurrentPointerId; std::size_t itsCurrentPointerId;

View File

@@ -42,6 +42,7 @@
#define CEREAL_DETAILS_POLYMORPHIC_IMPL_HPP_ #define CEREAL_DETAILS_POLYMORPHIC_IMPL_HPP_
#include <cereal/details/static_object.hpp> #include <cereal/details/static_object.hpp>
#include <cereal/types/memory.hpp>
#include <typeindex> #include <typeindex>
#include <map> #include <map>
@@ -50,12 +51,25 @@ namespace cereal
namespace detail namespace detail
{ {
template <class Archive> template <class Archive>
struct BindingMap struct OutputBindingMap
{ {
std::map<std::type_index, std::function<void(void*, void*)>> map; //! A serializer function
typedef std::function<void(void*, void const *)> 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
}; };
// Forward decls //! A map of serializers for pointers of all registered types
std::map<std::type_index, Serializers> map;
};
//! An empty noop deleter
template<class T> struct EmptyDeleter { void operator()(T *) const {} };
struct InputArchiveBase; struct InputArchiveBase;
struct OutputArchiveBase; struct OutputArchiveBase;
@@ -65,8 +79,27 @@ namespace cereal
{ {
OutputBinding( ) OutputBinding( )
{ {
StaticObject<BindingMap<Archive>>::getInstance().map.insert( {std::type_index(typeid(T)), [](void*, void*){ std::cout << "hello" << std::endl; }} ); typename OutputBindingMap<Archive>::Serializers serializers;
//BindingMap<Archive>::map.insert( {std::type_index(typeid(T)), [](void*, void*){ std::cout << "hello" << std::endl; }} );
serializers.shared_ptr =
[](void * arptr, void const * dptr)
{
Archive & ar = *static_cast<Archive*>(arptr);
std::shared_ptr<T const> const ptr(static_cast<T const *>(dptr), EmptyDeleter<T const>());
ar( detail::make_ptr_wrapper(ptr) );
};
serializers.unique_ptr =
[](void * arptr, void const * dptr)
{
Archive & ar = *static_cast<Archive*>(arptr);
std::unique_ptr<T const, EmptyDeleter<T const>> const ptr(static_cast<T const *>(dptr));
ar( detail::make_ptr_wrapper(ptr) );
};
StaticObject<OutputBindingMap<Archive>>::getInstance().map.insert( {std::type_index(typeid(T)), serializers} );
} }
}; };

View File

@@ -28,7 +28,7 @@
#define CEREAL_TYPES_POLYMORPHIC_HPP_ #define CEREAL_TYPES_POLYMORPHIC_HPP_
#include <cereal/cereal.hpp> #include <cereal/cereal.hpp>
#include <memory> #include <cereal/types/memory.hpp>
#include <cereal/details/polymorphic_impl.hpp> #include <cereal/details/polymorphic_impl.hpp>
//! Binds a polymorhic type to all registered archives //! Binds a polymorhic type to all registered archives

View File

@@ -33,14 +33,10 @@ struct MyType {};
CEREAL_BIND_TO_ARCHIVES(MyType); CEREAL_BIND_TO_ARCHIVES(MyType);
template <class T> template <class T> void nop(T&&t) {}
void nop(T&&t) {}
int main() int main()
{ {
cereal::detail::StaticObject<
cereal::detail::BindingMap<cereal::BinaryOutputArchive>
>::getInstance().map[std::type_index(typeid(MyType))](0, 0);
} }