unordered_map_index.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
  11. #define BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/intrusive/detail/minimal_pair_header.hpp>
  22. #include <boost/unordered_map.hpp>
  23. #include <boost/interprocess/detail/utilities.hpp>
  24. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  25. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  26. #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
  27. //!\file
  28. //!Describes index adaptor of boost::unordered_map container, to use it
  29. //!as name/shared memory index
  30. namespace boost {
  31. namespace interprocess {
  32. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  33. //!Helper class to define typedefs from
  34. //!IndexTraits
  35. template <class MapConfig>
  36. struct unordered_map_index_aux
  37. {
  38. typedef typename MapConfig::key_type key_type;
  39. typedef typename MapConfig::mapped_type mapped_type;
  40. typedef std::equal_to<key_type> key_equal;
  41. typedef std::pair<const key_type, mapped_type> value_type;
  42. typedef private_adaptive_pool
  43. <value_type,
  44. typename MapConfig::
  45. segment_manager_base> allocator_type;
  46. struct hasher
  47. {
  48. typedef key_type argument_type;
  49. typedef std::size_t result_type;
  50. std::size_t operator()(const key_type &val) const
  51. {
  52. typedef typename key_type::char_type char_type;
  53. const char_type *beg = ipcdetail::to_raw_pointer(val.mp_str),
  54. *end = beg + val.m_len;
  55. return boost::hash_range(beg, end);
  56. }
  57. };
  58. typedef unordered_map<key_type, mapped_type, hasher,
  59. key_equal, allocator_type> index_t;
  60. };
  61. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  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. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  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. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  75. public:
  76. //!Constructor. Takes a pointer to the
  77. //!segment manager. Can throw
  78. unordered_map_index(segment_manager_base *segment_mngr)
  79. : base_type(0,
  80. typename index_aux::hasher(),
  81. typename index_aux::key_equal(),
  82. segment_mngr){}
  83. //!This reserves memory to optimize the insertion of n
  84. //!elements in the index
  85. void reserve(typename segment_manager_base::size_type n)
  86. { base_type::rehash(n); }
  87. //!This tries to free previously allocate
  88. //!unused memory.
  89. void shrink_to_fit()
  90. { base_type::rehash(base_type::size()); }
  91. };
  92. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  93. //!Trait class to detect if an index is a node
  94. //!index. This allows more efficient operations
  95. //!when deallocating named objects.
  96. template<class MapConfig>
  97. struct is_node_index
  98. <boost::interprocess::unordered_map_index<MapConfig> >
  99. {
  100. static const bool value = true;
  101. };
  102. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  103. }} //namespace boost { namespace interprocess {
  104. #include <boost/interprocess/detail/config_end.hpp>
  105. #endif //#ifndef BOOST_INTERPROCESS_UNORDERED_MAP_INDEX_HPP