1 /* Copyright 2016-2018 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>
19 #include <type_traits>
21 #include <unordered_map>
26 namespace poly_collection{
30 /* To cope with dynamic modules/libs, the standard allows for different
31 * std::type_info instances to describe the same type, which implies that
32 * std::type_info::operator== and std::type_info::hash_code are costly
33 * operations typically relying on the stored type name.
34 * type_info_ptr_hash<T> behaves roughly as a
35 * std::unordered_map<std::type_index,T> but maintains an internal cache of
36 * passed std::type_info instances so that lookup is performed (when there's a
37 * cache hit) without invoking std::type_info equality and hashing ops.
40 struct type_info_ptr_hash
42 std::size_t operator()(const std::type_info* p)const noexcept
43 {return p->hash_code();}
46 struct type_info_ptr_equal_to
49 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 type_info_map()=default;
72 type_info_map(const type_info_map& x):
74 cache{make<cache_type>(std::allocator_traits<cache_allocator_type>::
75 select_on_container_copy_construction(x.cache.get_allocator()))}
76 {build_cache(x.cache);}
77 type_info_map(type_info_map&& x)=default;
78 type_info_map(const allocator_type& al):
79 map{make<map_type>(al)},cache{make<cache_type>(al)}{}
80 type_info_map(const type_info_map& x,const allocator_type& al):
81 map{make(x.map,al)},cache{make<cache_type>(al)}
82 {build_cache(x.cache);}
83 type_info_map(type_info_map&& x,const allocator_type& al):
84 map{make(std::move(x.map),al)},
86 al==allocator_type{x.map.get_allocator()}&&x.map.empty()?
87 make(std::move(x.cache),al):
91 if(!(al==allocator_type{x.map.get_allocator()}&&x.map.empty())){
98 type_info_map& operator=(const type_info_map& x)
102 cache=make<cache_type>(map.get_allocator());
103 build_cache(x.cache);
113 type_info_map& operator=(type_info_map&& x)
116 map=std::move(x.map);
117 if(map.get_allocator()==x.map.get_allocator()){
118 cache=std::move(x.cache);
121 cache=make<cache_type>(map.get_allocator());
122 build_cache(x.cache);
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 c=map.bucket_count();
165 auto p=map.emplace(&key,std::forward<P>(x));
166 if(map.bucket_count()!=c)rebuild_cache();
167 cache.insert({&key,p.first});
171 void swap(type_info_map& x){map.swap(x.map);cache.swap(x.cache);}
174 using cache_type=std::unordered_map<
175 const std::type_info*,iterator,
176 std::hash<const std::type_info*>,std::equal_to<const std::type_info*>,
177 typename std::allocator_traits<Allocator>::template
178 rebind_alloc<std::pair<const std::type_info* const,iterator>>
180 using cache_allocator_type=typename cache_type::allocator_type;
182 #if BOOST_WORKAROUND(BOOST_LIBSTDCXX_VERSION,<40900)
183 /* std::unordered_map(const allocator_type&),
184 * std::unordered_map(const unordered_map&,const allocator_type&) and
185 * std::unordered_map(unordered_map&&,const allocator_type&) not available.
186 * We make move construction decay to copy construction.
189 template<typename UnorderedMap>
190 static UnorderedMap make(const typename UnorderedMap::allocator_type& al)
193 10,typename UnorderedMap::hasher{},typename UnorderedMap::key_equal{},al
197 template<typename UnorderedMap>
198 static typename std::decay<UnorderedMap>::type make(
200 const typename std::decay<UnorderedMap>::type::allocator_type& al)
202 using RawUnorderedMap=typename std::decay<UnorderedMap>::type;
204 return RawUnorderedMap{
205 x.begin(),x.end(),0,typename RawUnorderedMap::hasher{},
206 typename RawUnorderedMap::key_equal{},al
210 template<typename UnorderedMap>
211 static UnorderedMap make(const typename UnorderedMap::allocator_type& al)
213 return UnorderedMap{al};
216 template<typename UnorderedMap>
217 static typename std::decay<UnorderedMap>::type make(
219 const typename std::decay<UnorderedMap>::type::allocator_type& al)
221 return {std::forward<UnorderedMap>(x),al};
225 void build_cache(const cache_type& x)
227 for(const auto& p:x)cache.insert({p.first,map.find(p.first)});
232 for(auto& p:cache)p.second=map.find(p.first);
239 template<typename T,typename Allocator>
240 void swap(type_info_map<T,Allocator>& x,type_info_map<T,Allocator>& y)
245 } /* namespace poly_collection::detail */
247 } /* namespace poly_collection */
249 } /* namespace boost */