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 <boost/intrusive/detail/minimal_pair_header.hpp> 26 #include <boost/unordered_map.hpp> 27 #include <boost/interprocess/detail/utilities.hpp> 28 #include <boost/interprocess/allocators/private_adaptive_pool.hpp> 29 30 #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair 31 #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less 32 33 //!\file 34 //!Describes index adaptor of boost::unordered_map container, to use it 35 //!as name/shared memory index 36 37 namespace boost { 38 namespace interprocess { 39 40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 41 42 //!Helper class to define typedefs from 43 //!IndexTraits 44 template <class MapConfig> 45 struct unordered_map_index_aux 46 { 47 typedef typename MapConfig::key_type key_type; 48 typedef typename MapConfig::mapped_type mapped_type; 49 typedef std::equal_to<key_type> key_equal; 50 typedef std::pair<const key_type, mapped_type> value_type; 51 typedef private_adaptive_pool 52 <value_type, 53 typename MapConfig:: 54 segment_manager_base> allocator_type; 55 struct hasher 56 { 57 typedef key_type argument_type; 58 typedef std::size_t result_type; 59 operator ()boost::interprocess::unordered_map_index_aux::hasher60 std::size_t operator()(const key_type &val) const 61 { 62 typedef typename key_type::char_type char_type; 63 const char_type *beg = ipcdetail::to_raw_pointer(val.mp_str), 64 *end = beg + val.m_len; 65 return boost::hash_range(beg, end); 66 } 67 }; 68 typedef unordered_map<key_type, mapped_type, hasher, 69 key_equal, allocator_type> index_t; 70 }; 71 72 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 73 74 //!Index type based in unordered_map. Just derives from unordered_map and 75 //!defines the interface needed by managed memory segments 76 template <class MapConfig> 77 class unordered_map_index 78 //Derive class from unordered_map specialization 79 : public unordered_map_index_aux<MapConfig>::index_t 80 { 81 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 82 typedef unordered_map_index_aux<MapConfig> index_aux; 83 typedef typename index_aux::index_t base_type; 84 typedef typename 85 MapConfig::segment_manager_base segment_manager_base; 86 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 87 88 public: 89 //!Constructor. Takes a pointer to the 90 //!segment manager. Can throw unordered_map_index(segment_manager_base * segment_mngr)91 unordered_map_index(segment_manager_base *segment_mngr) 92 : base_type(0, 93 typename index_aux::hasher(), 94 typename index_aux::key_equal(), 95 segment_mngr){} 96 97 //!This reserves memory to optimize the insertion of n 98 //!elements in the index reserve(typename segment_manager_base::size_type n)99 void reserve(typename segment_manager_base::size_type n) 100 { base_type::rehash(n); } 101 102 //!This tries to free previously allocate 103 //!unused memory. shrink_to_fit()104 void shrink_to_fit() 105 { base_type::rehash(base_type::size()); } 106 }; 107 108 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) 109 110 //!Trait class to detect if an index is a node 111 //!index. This allows more efficient operations 112 //!when deallocating named objects. 113 template<class MapConfig> 114 struct is_node_index 115 <boost::interprocess::unordered_map_index<MapConfig> > 116 { 117 static const bool value = true; 118 }; 119 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED 120 121 }} //namespace boost { namespace interprocess { 122 123 #include <boost/interprocess/detail/config_end.hpp> 124 125 #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP 126