1 /* Copyright 2016-2017 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/poly_collection for library home page.
9 #ifndef BOOST_POLY_COLLECTION_DETAIL_TYPE_INFO_MAP_HPP
10 #define BOOST_POLY_COLLECTION_DETAIL_TYPE_INFO_MAP_HPP
16 #include <boost/detail/workaround.hpp>
17 #include <boost/poly_collection/detail/newdelete_allocator.hpp>
20 #include <unordered_map>
25 namespace poly_collection{
29 /* To cope with dynamic modules/libs, the standard allows for different
30 * std::type_info instances to describe the same type, which implies that
31 * std::type_info::operator== and std::type_info::hash_code are costly
32 * operations typically relying on the stored type name.
33 * type_info_ptr_hash<T> behaves roughly as a
34 * std::unordered_map<std::type_index,T> but maintains an internal cache of
35 * passed std::type_info instances so that lookup is performed (when there's a
36 * cache hit) without invoking std::type_info equality and hashing ops.
39 struct type_info_ptr_hash
41 std::size_t operator()(const std::type_info* p)const noexcept
42 {return p->hash_code();}
45 struct type_info_ptr_equal_to
48 const std::type_info* p,const std::type_info* q)const noexcept
53 template<typename T,typename Allocator>
56 using map_type=std::unordered_map<
57 const std::type_info*,T,
58 type_info_ptr_hash,type_info_ptr_equal_to,
59 typename std::allocator_traits<Allocator>::template
60 rebind_alloc<std::pair<const std::type_info* const,T>>
64 using key_type=std::type_info;
66 using value_type=typename map_type::value_type;
67 using allocator_type=typename map_type::allocator_type;
68 using iterator=typename map_type::iterator;
69 using const_iterator=typename map_type::const_iterator;
71 #if BOOST_WORKAROUND(BOOST_LIBSTDCXX_VERSION,<40900)
72 /* std::unordered_map(const unordered_map&,const allocator_type&),
73 * std::unordered_map(unordered_map&&,const allocator_type&) and
74 * std::unordered_map(const allocator_type&) not available.
77 #define BOOST_POLY_COLLECTION_MAP_CONT_ALLOC_CTOR(x,al) \
79 0,typename map_type::hasher{},typename map_type::key_equal{},al
80 #define BOOST_POLY_COLLECTION_MAP_ALLOC_CTOR(al) \
81 10,typename map_type::hasher{},typename map_type::key_equal{},al
82 #define BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(al) \
83 10,typename cache_type::hasher{},typename cache_type::key_equal{},al
87 #define BOOST_POLY_COLLECTION_MAP_CONT_ALLOC_CTOR(x,al) x,al
88 #define BOOST_POLY_COLLECTION_MAP_ALLOC_CTOR(al) al
89 #define BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(al) al
93 type_info_map()=default;
94 type_info_map(const type_info_map& x):
96 cache{BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(x.cache.get_allocator())}
97 {build_cache(x.cache);}
98 type_info_map(type_info_map&& x)=default;
99 type_info_map(const allocator_type& al):
100 map{BOOST_POLY_COLLECTION_MAP_ALLOC_CTOR(al)},
101 cache{BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(cache_allocator_type{al})}
103 type_info_map(const type_info_map& x,const allocator_type& al):
104 map{BOOST_POLY_COLLECTION_MAP_CONT_ALLOC_CTOR(x.map,al)},
105 cache{BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(cache_allocator_type{al})}
106 {build_cache(x.cache);}
107 type_info_map(type_info_map&& x,const allocator_type& al):
108 map{BOOST_POLY_COLLECTION_MAP_CONT_ALLOC_CTOR(std::move(x.map),al)},
109 cache{BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR(cache_allocator_type{al})}
111 if(al==x.map.get_allocator()&&
112 cache_allocator_type{al}==x.cache.get_allocator()){
113 cache=std::move(x.cache);
116 build_cache(x.cache);
121 #undef BOOST_POLY_COLLECTION_MAP_CONT_ALLOC_CTOR
122 #undef BOOST_POLY_COLLECTION_MAP_ALLOC_CTOR
123 #undef BOOST_POLY_COLLECTION_CACHE_ALLOC_CTOR
125 type_info_map& operator=(const type_info_map& x)
134 type_info_map& operator=(type_info_map&& x)=default;
136 allocator_type get_allocator()const noexcept{return map.get_allocator();}
138 iterator begin()noexcept{return map.begin();}
139 iterator end()noexcept{return map.end();}
140 const_iterator begin()const noexcept{return map.begin();}
141 const_iterator end()const noexcept{return map.end();}
142 const_iterator cbegin()const noexcept{return map.cbegin();}
143 const_iterator cend()const noexcept{return map.cend();}
145 iterator find(const key_type& key)
147 auto cit=cache.find(&key);
148 if(cit!=cache.end())return cit->second;
149 auto mit=map.find(&key);
150 if(mit!=map.end())cache.insert({&key,mit});
154 const_iterator find(const key_type& key)const
156 auto cit=cache.find(&key);
157 if(cit!=cache.end())return cit->second;
158 return map.find(&key);
162 std::pair<iterator,bool> insert(const key_type& key,P&& x)
164 auto p=map.insert({&key,std::forward<P>(x)});
165 cache.insert({&key,p.first});
169 void swap(type_info_map& x){map.swap(x.map);cache.swap(x.cache);}
172 using cache_type=std::unordered_map<
173 const std::type_info*,iterator,
174 std::hash<const std::type_info*>,std::equal_to<const std::type_info*>,
175 newdelete_allocator_adaptor<
176 typename std::allocator_traits<Allocator>::template
177 rebind_alloc<std::pair<const std::type_info* const,iterator>>
180 using cache_allocator_type=typename cache_type::allocator_type;
182 void build_cache(const cache_type& x)
184 for(const auto& p:x)cache.insert({p.first,map.find(p.first)});
191 template<typename T,typename Allocator>
192 void swap(type_info_map<T,Allocator>& x,type_info_map<T,Allocator>& y)
197 } /* namespace poly_collection::detail */
199 } /* namespace poly_collection */
201 } /* namespace boost */