1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
12 #define BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
14 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
19 #include <boost/unordered_map.hpp>
20 #include <boost/interprocess/detail/utilities.hpp>
21 #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
24 //!Describes index adaptor of boost::unordered_map container, to use it
25 //!as name/shared memory index
28 namespace interprocess {
32 //!Helper class to define typedefs from
34 template <class MapConfig>
35 struct unordered_map_index_aux
37 typedef typename MapConfig::key_type key_type;
38 typedef typename MapConfig::mapped_type mapped_type;
39 typedef std::equal_to<key_type> key_equal;
40 typedef std::pair<const key_type, mapped_type> value_type;
41 typedef private_adaptive_pool
44 segment_manager_base> allocator_type;
46 : std::unary_function<key_type, std::size_t>
48 std::size_t operator()(const key_type &val) const
50 typedef typename key_type::char_type char_type;
51 const char_type *beg = ipcdetail::get_pointer(val.mp_str),
52 *end = beg + val.m_len;
53 return boost::hash_range(beg, end);
56 typedef unordered_map<key_type, mapped_type, hasher,
57 key_equal, allocator_type> index_t;
62 //!Index type based in unordered_map. Just derives from unordered_map and
63 //!defines the interface needed by managed memory segments
64 template <class MapConfig>
65 class unordered_map_index
66 //Derive class from unordered_map specialization
67 : public unordered_map_index_aux<MapConfig>::index_t
70 typedef unordered_map_index_aux<MapConfig> index_aux;
71 typedef typename index_aux::index_t base_type;
73 MapConfig::segment_manager_base segment_manager_base;
77 //!Constructor. Takes a pointer to the
78 //!segment manager. Can throw
79 unordered_map_index(segment_manager_base *segment_mngr)
81 typename index_aux::hasher(),
82 typename index_aux::key_equal(),
85 //!This reserves memory to optimize the insertion of n
86 //!elements in the index
87 void reserve(typename segment_manager_base::size_type n)
88 { base_type::rehash(n); }
90 //!This tries to free previously allocate
93 { base_type::rehash(base_type::size()); }
98 //!Trait class to detect if an index is a node
99 //!index. This allows more efficient operations
100 //!when deallocating named objects.
101 template<class MapConfig>
103 <boost::interprocess::unordered_map_index<MapConfig> >
105 enum { value = true };
109 }} //namespace boost { namespace interprocess {
111 #include <boost/interprocess/detail/config_end.hpp>
113 #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP