flat_map_index.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_FLAT_MAP_INDEX_HPP
  11. #define BOOST_INTERPROCESS_FLAT_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. // interprocess
  22. #include <boost/container/flat_map.hpp>
  23. #include <boost/interprocess/allocators/allocator.hpp>
  24. // intrusive/detail
  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::map container, to use it
  29. //!as name/shared memory index
  30. //[flat_map_index
  31. namespace boost { namespace interprocess {
  32. #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  33. //!Helper class to define typedefs from IndexTraits
  34. template <class MapConfig>
  35. struct flat_map_index_aux
  36. {
  37. typedef typename MapConfig::key_type key_type;
  38. typedef typename MapConfig::mapped_type mapped_type;
  39. typedef typename MapConfig::
  40. segment_manager_base segment_manager_base;
  41. typedef std::less<key_type> key_less;
  42. typedef std::pair<key_type, mapped_type> value_type;
  43. typedef allocator<value_type
  44. ,segment_manager_base> allocator_type;
  45. typedef boost::container::flat_map<key_type, mapped_type,
  46. key_less, allocator_type> index_t;
  47. };
  48. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  49. //!Index type based in flat_map. Just derives from flat_map and
  50. //!defines the interface needed by managed memory segments.
  51. template <class MapConfig>
  52. class flat_map_index
  53. //Derive class from flat_map specialization
  54. : private flat_map_index_aux<MapConfig>::index_t
  55. {
  56. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  57. typedef flat_map_index_aux<MapConfig> index_aux;
  58. typedef typename index_aux::index_t base_type;
  59. typedef typename index_aux::
  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. using base_type::shrink_to_fit;
  70. using base_type::reserve;
  71. typedef typename base_type::iterator iterator;
  72. typedef typename base_type::const_iterator const_iterator;
  73. typedef typename base_type::value_type value_type;
  74. typedef typename MapConfig::compare_key_type compare_key_type;
  75. typedef iterator insert_commit_data;
  76. typedef iterator index_data_t;
  77. //!Constructor. Takes a pointer to the segment manager. Can throw
  78. flat_map_index(segment_manager_base *segment_mngr)
  79. : base_type(typename index_aux::key_less(),
  80. typename index_aux::allocator_type(segment_mngr))
  81. {}
  82. std::pair<iterator, bool> insert_check
  83. (const compare_key_type& key, insert_commit_data&)
  84. {
  85. std::pair<iterator, bool> r;
  86. r.first = this->base_type::find(key_type(key.str(), key.len()));
  87. r.second = r.first == this->base_type::end();
  88. return r;
  89. }
  90. iterator insert_commit
  91. (const compare_key_type &k, void *context, index_data_t&, insert_commit_data& )
  92. {
  93. //Now commit the insertion using previous context data
  94. return this->base_type::insert(value_type(key_type(k.str(), k.len()), mapped_type(context))).first;
  95. }
  96. iterator find(const compare_key_type& k)
  97. { return this->base_type::find(key_type(k.str(), k.len())); }
  98. };
  99. }} //namespace boost { namespace interprocess
  100. //]
  101. #include <boost/interprocess/detail/config_end.hpp>
  102. #endif //#ifndef BOOST_INTERPROCESS_FLAT_MAP_INDEX_HPP