]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
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) | |
6 | // | |
7 | // See http://www.boost.org/libs/interprocess for documentation. | |
8 | // | |
9 | ////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP | |
12 | #define BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP | |
13 | ||
14 | #ifndef BOOST_CONFIG_HPP | |
15 | # include <boost/config.hpp> | |
16 | #endif | |
17 | # | |
18 | #if defined(BOOST_HAS_PRAGMA_ONCE) | |
19 | # pragma once | |
20 | #endif | |
21 | ||
22 | #include <boost/interprocess/detail/config_begin.hpp> | |
23 | #include <boost/interprocess/detail/workaround.hpp> | |
24 | ||
25 | #include <functional> | |
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> | |
30 | ||
31 | #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair | |
32 | #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less | |
33 | ||
34 | //!\file | |
35 | //!Describes index adaptor of boost::unordered_map container, to use it | |
36 | //!as name/shared memory index | |
37 | ||
38 | namespace boost { | |
39 | namespace interprocess { | |
40 | ||
41 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
42 | ||
43 | //!Helper class to define typedefs from | |
44 | //!IndexTraits | |
45 | template <class MapConfig> | |
46 | struct unordered_map_index_aux | |
47 | { | |
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 | |
53 | <value_type, | |
54 | typename MapConfig:: | |
55 | segment_manager_base> allocator_type; | |
56 | struct hasher | |
57 | : std::unary_function<key_type, std::size_t> | |
58 | { | |
59 | std::size_t operator()(const key_type &val) const | |
60 | { | |
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); | |
65 | } | |
66 | }; | |
67 | typedef unordered_map<key_type, mapped_type, hasher, | |
68 | key_equal, allocator_type> index_t; | |
69 | }; | |
70 | ||
71 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
72 | ||
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 | |
79 | { | |
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; | |
83 | typedef typename | |
84 | MapConfig::segment_manager_base segment_manager_base; | |
85 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
86 | ||
87 | public: | |
88 | //!Constructor. Takes a pointer to the | |
89 | //!segment manager. Can throw | |
90 | unordered_map_index(segment_manager_base *segment_mngr) | |
91 | : base_type(0, | |
92 | typename index_aux::hasher(), | |
93 | typename index_aux::key_equal(), | |
94 | segment_mngr){} | |
95 | ||
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); } | |
100 | ||
101 | //!This tries to free previously allocate | |
102 | //!unused memory. | |
103 | void shrink_to_fit() | |
104 | { base_type::rehash(base_type::size()); } | |
105 | }; | |
106 | ||
107 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) | |
108 | ||
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> | |
113 | struct is_node_index | |
114 | <boost::interprocess::unordered_map_index<MapConfig> > | |
115 | { | |
116 | static const bool value = true; | |
117 | }; | |
118 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED | |
119 | ||
120 | }} //namespace boost { namespace interprocess { | |
121 | ||
122 | #include <boost/interprocess/detail/config_end.hpp> | |
123 | ||
124 | #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP |