]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/interprocess/indexes/unordered_map_index.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / interprocess / indexes / unordered_map_index.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
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 #include <boost/interprocess/detail/config_begin.hpp>
15 #include <boost/interprocess/detail/workaround.hpp>
16
17 #include <functional>
18 #include <utility>
19 #include <boost/unordered_map.hpp>
20 #include <boost/interprocess/detail/utilities.hpp>
21 #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
22
23 //!\file
24 //!Describes index adaptor of boost::unordered_map container, to use it
25 //!as name/shared memory index
26
27 namespace boost {
28 namespace interprocess {
29
30 ///@cond
31
32 //!Helper class to define typedefs from
33 //!IndexTraits
34 template <class MapConfig>
35 struct unordered_map_index_aux
36 {
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
42             <value_type,
43                typename MapConfig::
44                   segment_manager_base>      allocator_type;
45     struct hasher
46       : std::unary_function<key_type, std::size_t>
47     {
48         std::size_t operator()(const key_type &val) const
49         {
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);
54         }
55     };
56    typedef unordered_map<key_type,  mapped_type, hasher,
57                          key_equal, allocator_type>      index_t;
58 };
59
60 ///@endcond
61
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
68 {
69    /// @cond
70    typedef unordered_map_index_aux<MapConfig>   index_aux;
71    typedef typename index_aux::index_t          base_type;
72    typedef typename 
73       MapConfig::segment_manager_base     segment_manager_base;
74    /// @endcond
75
76    public:
77    //!Constructor. Takes a pointer to the
78    //!segment manager. Can throw
79    unordered_map_index(segment_manager_base *segment_mngr)
80       : base_type(0,
81                   typename index_aux::hasher(),
82                   typename index_aux::key_equal(),
83                   segment_mngr){}
84
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);  }
89
90    //!This tries to free previously allocate
91    //!unused memory.
92    void shrink_to_fit()
93    {  base_type::rehash(base_type::size()); }
94 };
95
96 /// @cond
97
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>
102 struct is_node_index
103    <boost::interprocess::unordered_map_index<MapConfig> >
104 {
105    enum {   value = true };
106 };
107 /// @endcond
108
109 }}   //namespace boost { namespace interprocess {
110
111 #include <boost/interprocess/detail/config_end.hpp>
112
113 #endif   //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP