map_index.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_MAP_INDEX_HPP
  11. #define BOOST_INTERPROCESS_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/container/map.hpp>
  23. #include <boost/interprocess/allocators/private_adaptive_pool.hpp>
  24. #include <boost/intrusive/detail/minimal_pair_header.hpp> //std::pair
  25. #include <boost/intrusive/detail/minimal_less_equal_header.hpp> //std::less
  26. //!\file
  27. //!Describes index adaptor of boost::map container, to use it
  28. //!as name/shared memory index
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail{
  32. //!Helper class to define typedefs from IndexTraits
  33. template <class MapConfig>
  34. struct map_index_aux
  35. {
  36. typedef typename MapConfig::key_type key_type;
  37. typedef typename MapConfig::mapped_type mapped_type;
  38. typedef std::less<key_type> key_less;
  39. typedef std::pair<const key_type, mapped_type> value_type;
  40. typedef private_adaptive_pool
  41. <value_type,
  42. typename MapConfig::
  43. segment_manager_base> allocator_type;
  44. typedef boost::container::map
  45. <key_type, mapped_type,
  46. key_less, allocator_type> index_t;
  47. };
  48. } //namespace ipcdetail {
  49. //!Index type based in boost::interprocess::map. Just derives from boost::interprocess::map
  50. //!and defines the interface needed by managed memory segments
  51. template <class MapConfig>
  52. class map_index
  53. //Derive class from map specialization
  54. : private ipcdetail::map_index_aux<MapConfig>::index_t
  55. {
  56. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  57. typedef ipcdetail::map_index_aux<MapConfig> index_aux;
  58. typedef typename index_aux::index_t base_type;
  59. typedef typename MapConfig::
  60. segment_manager_base segment_manager_base;
  61. typedef typename base_type::key_type key_type;
  62. typedef typename base_type::mapped_type mapped_type;
  63. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  64. public:
  65. using base_type::begin;
  66. using base_type::end;
  67. using base_type::size;
  68. using base_type::erase;
  69. typedef typename base_type::iterator iterator;
  70. typedef typename base_type::const_iterator const_iterator;
  71. typedef typename base_type::value_type value_type;
  72. typedef typename MapConfig::compare_key_type compare_key_type;
  73. typedef iterator insert_commit_data;
  74. typedef iterator index_data_t;
  75. //!Constructor. Takes a pointer to the
  76. //!segment manager. Can throw
  77. map_index(segment_manager_base *segment_mngr)
  78. : base_type(typename index_aux::key_less(),
  79. segment_mngr){}
  80. //!This reserves memory to optimize the insertion of n
  81. //!elements in the index
  82. void reserve(typename segment_manager_base::size_type)
  83. { /*Does nothing, map has not reserve or rehash*/ }
  84. //!This tries to free previously allocate
  85. //!unused memory.
  86. void shrink_to_fit()
  87. { base_type::get_stored_allocator().deallocate_free_blocks(); }
  88. std::pair<iterator, bool> insert_check
  89. (const compare_key_type& key, insert_commit_data& )
  90. {
  91. std::pair<iterator, bool> r;
  92. r.first = this->base_type::find(key_type(key.str(), key.len()));
  93. r.second = r.first == this->base_type::end();
  94. return r;
  95. }
  96. iterator insert_commit
  97. (const compare_key_type &k, void *context, index_data_t &index_data, insert_commit_data& )
  98. {
  99. //Now commit the insertion using previous context data
  100. iterator it = this->base_type::insert(value_type(key_type(k.str(), k.len()), mapped_type(context))).first;
  101. return (index_data = it);
  102. }
  103. iterator find(const compare_key_type& k)
  104. { return this->base_type::find(key_type(k.str(), k.len())); }
  105. };
  106. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  107. //!Trait class to detect if an index is a node
  108. //!index. This allows more efficient operations
  109. //!when deallocating named objects.
  110. template<class MapConfig>
  111. struct is_node_index
  112. <boost::interprocess::map_index<MapConfig> >
  113. {
  114. static const bool value = true;
  115. };
  116. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  117. }} //namespace boost { namespace interprocess {
  118. #include <boost/interprocess/detail/config_end.hpp>
  119. #endif //#ifndef BOOST_INTERPROCESS_MAP_INDEX_HPP