Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove template parameter Archive from InputBindingsMap and OutputBindingsMap #812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions include/cereal/details/polymorphic_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,6 @@ namespace cereal
type, containing entries for every registered type that describe how to
properly cast the type to its real type in polymorphic scenarios for
shared_ptr, weak_ptr, and unique_ptr. */
template <class Archive>
struct OutputBindingMap
{
//! A serializer function
Expand All @@ -483,7 +482,14 @@ namespace cereal
};

//! A map of serializers for pointers of all registered types
std::map<std::type_index, Serializers> map;
using Serializers_map = std::map<std::type_index, Serializers>;
//! A map of archive typeid -> map of serializers for given archive
using Archives_map = std::map<std::type_index, Serializers_map>;
Archives_map archives_map;

//! Obtain serializers map for given archive
template<typename Archive>
Serializers_map& map() { return archives_map[typeid(Archive)]; }
};

//! An empty noop deleter
Expand All @@ -494,7 +500,6 @@ namespace cereal
type, containing entries for every registered type that describe how to
properly cast the type to its real type in polymorphic scenarios for
shared_ptr, weak_ptr, and unique_ptr. */
template <class Archive>
struct InputBindingMap
{
//! Shared ptr serializer function
Expand All @@ -515,7 +520,14 @@ namespace cereal
};

//! A map of serializers for pointers of all registered types
std::map<std::string, Serializers> map;
using Serializers_map = std::map<std::string, Serializers>;
//! A map of archive typeid -> map of serializers for given archive
using Archives_map = std::map<std::type_index, Serializers_map>;
Archives_map archives_map;

//! Obtain serializers map for given archive
template<typename Archive>
Serializers_map& map() { return archives_map[typeid(Archive)]; }
};

// forward decls for archives from cereal.hpp
Expand All @@ -532,15 +544,15 @@ namespace cereal
//! Initialize the binding
InputBindingCreator()
{
auto & map = StaticObject<InputBindingMap<Archive>>::getInstance().map;
auto lock = StaticObject<InputBindingMap<Archive>>::lock();
auto & map = StaticObject<InputBindingMap>::getInstance().map<Archive>();
auto lock = StaticObject<InputBindingMap>::lock();
auto key = std::string(binding_name<T>::name());
auto lb = map.lower_bound(key);

if (lb != map.end() && lb->first == key)
return;

typename InputBindingMap<Archive>::Serializers serializers;
typename InputBindingMap::Serializers serializers;

serializers.shared_ptr =
[](void * arptr, std::shared_ptr<void> & dptr, std::type_info const & baseInfo)
Expand Down Expand Up @@ -652,14 +664,15 @@ namespace cereal
//! Initialize the binding
OutputBindingCreator()
{
auto & map = StaticObject<OutputBindingMap<Archive>>::getInstance().map;
auto & map = StaticObject<OutputBindingMap>::getInstance().map<Archive>();
auto lock = StaticObject<OutputBindingMap>::lock();
auto key = std::type_index(typeid(T));
auto lb = map.lower_bound(key);

if (lb != map.end() && lb->first == key)
return;

typename OutputBindingMap<Archive>::Serializers serializers;
typename OutputBindingMap::Serializers serializers;

serializers.shared_ptr =
[&](void * arptr, void const * dptr, std::type_info const & baseInfo)
Expand Down
14 changes: 7 additions & 7 deletions include/cereal/types/polymorphic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ namespace cereal
//! Get an input binding from the given archive by deserializing the type meta data
/*! @internal */
template<class Archive> inline
typename ::cereal::detail::InputBindingMap<Archive>::Serializers getInputBinding(Archive & ar, std::uint32_t const nameid)
typename ::cereal::detail::InputBindingMap::Serializers getInputBinding(Archive & ar, std::uint32_t const nameid)
{
// If the nameid is zero, we serialized a null pointer
if(nameid == 0)
{
typename ::cereal::detail::InputBindingMap<Archive>::Serializers emptySerializers;
typename ::cereal::detail::InputBindingMap::Serializers emptySerializers;
emptySerializers.shared_ptr = [](void*, std::shared_ptr<void> & ptr, std::type_info const &) { ptr.reset(); };
emptySerializers.unique_ptr = [](void*, std::unique_ptr<void, ::cereal::detail::EmptyDeleter<void>> & ptr, std::type_info const &) { ptr.reset( nullptr ); };
return emptySerializers;
Expand All @@ -214,7 +214,7 @@ namespace cereal
else
name = ar.getPolymorphicName(nameid);

auto const & bindingMap = detail::StaticObject<detail::InputBindingMap<Archive>>::getInstance().map;
auto const & bindingMap = detail::StaticObject<detail::InputBindingMap>::getInstance().map<Archive>();

auto binding = bindingMap.find(name);
if(binding == bindingMap.end())
Expand Down Expand Up @@ -317,7 +317,7 @@ namespace cereal
// of an abstract object
// this implies we need to do the lookup

auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap<Archive>>::getInstance().map;
auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap>::getInstance().map<Archive>();

auto binding = bindingMap.find(std::type_index(ptrinfo));
if(binding == bindingMap.end())
Expand Down Expand Up @@ -352,7 +352,7 @@ namespace cereal
return;
}

auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap<Archive>>::getInstance().map;
auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap>::getInstance().map<Archive>();

auto binding = bindingMap.find(std::type_index(ptrinfo));
if(binding == bindingMap.end())
Expand Down Expand Up @@ -416,7 +416,7 @@ namespace cereal
// of an abstract object
// this implies we need to do the lookup

auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap<Archive>>::getInstance().map;
auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap>::getInstance().map<Archive>();

auto binding = bindingMap.find(std::type_index(ptrinfo));
if(binding == bindingMap.end())
Expand Down Expand Up @@ -451,7 +451,7 @@ namespace cereal
return;
}

auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap<Archive>>::getInstance().map;
auto const & bindingMap = detail::StaticObject<detail::OutputBindingMap>::getInstance().map<Archive>();

auto binding = bindingMap.find(std::type_index(ptrinfo));
if(binding == bindingMap.end())
Expand Down