1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. 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 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
26 #include <boost/intrusive/detail/minimal_pair_header.hpp>
27 #include <boost/unordered_map.hpp>
28 #include <boost/interprocess/detail/utilities.hpp>
29 #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
31 #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
32 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
35 //!Describes index adaptor of boost::unordered_map container, to use it
36 //!as name/shared memory index
39 namespace interprocess {
41 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
43 //!Helper class to define typedefs from
45 template <class MapConfig>
46 struct unordered_map_index_aux
48 typedef typename MapConfig::key_type key_type;
49 typedef typename MapConfig::mapped_type mapped_type;
50 typedef std::equal_to<key_type> key_equal;
51 typedef std::pair<const key_type, mapped_type> value_type;
52 typedef private_adaptive_pool
55 segment_manager_base> allocator_type;
57 : std::unary_function<key_type, std::size_t>
59 std::size_t operator()(const key_type &val) const
61 typedef typename key_type::char_type char_type;
62 const char_type *beg = ipcdetail::to_raw_pointer(val.mp_str),
63 *end = beg + val.m_len;
64 return boost::hash_range(beg, end);
67 typedef unordered_map<key_type, mapped_type, hasher,
68 key_equal, allocator_type> index_t;
71 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
73 //!Index type based in unordered_map. Just derives from unordered_map and
74 //!defines the interface needed by managed memory segments
75 template <class MapConfig>
76 class unordered_map_index
77 //Derive class from unordered_map specialization
78 : public unordered_map_index_aux<MapConfig>::index_t
80 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
81 typedef unordered_map_index_aux<MapConfig> index_aux;
82 typedef typename index_aux::index_t base_type;
84 MapConfig::segment_manager_base segment_manager_base;
85 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
88 //!Constructor. Takes a pointer to the
89 //!segment manager. Can throw
90 unordered_map_index(segment_manager_base *segment_mngr)
92 typename index_aux::hasher(),
93 typename index_aux::key_equal(),
96 //!This reserves memory to optimize the insertion of n
97 //!elements in the index
98 void reserve(typename segment_manager_base::size_type n)
99 { base_type::rehash(n); }
101 //!This tries to free previously allocate
104 { base_type::rehash(base_type::size()); }
107 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
109 //!Trait class to detect if an index is a node
110 //!index. This allows more efficient operations
111 //!when deallocating named objects.
112 template<class MapConfig>
114 <boost::interprocess::unordered_map_index<MapConfig> >
116 static const bool value = true;
118 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
120 }} //namespace boost { namespace interprocess {
122 #include <boost/interprocess/detail/config_end.hpp>
124 #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP