iset_index.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_ISET_INDEX_HPP
  11. #define BOOST_INTERPROCESS_ISET_INDEX_HPP
  12. #include <boost/interprocess/detail/config_begin.hpp>
  13. #include <boost/interprocess/detail/workaround.hpp>
  14. #include <string>
  15. #include <functional>
  16. #include <utility>
  17. #include <boost/interprocess/detail/utilities.hpp>
  18. #include <boost/intrusive/set.hpp>
  19. //!\file
  20. //!Describes index adaptor of boost::intrusive::set container, to use it
  21. //!as name/shared memory index
  22. namespace boost {
  23. namespace interprocess {
  24. /// @cond
  25. //!Helper class to define typedefs from IndexTraits
  26. template <class MapConfig>
  27. struct iset_index_aux
  28. {
  29. typedef typename
  30. MapConfig::segment_manager_base segment_manager_base;
  31. typedef typename
  32. segment_manager_base::void_pointer void_pointer;
  33. typedef typename bi::make_set_base_hook
  34. < bi::void_pointer<void_pointer>
  35. , bi::optimize_size<true>
  36. >::type derivation_hook;
  37. typedef typename MapConfig::template
  38. intrusive_value_type<derivation_hook>::type value_type;
  39. typedef std::less<value_type> value_compare;
  40. typedef typename bi::make_set
  41. < value_type
  42. , bi::base_hook<derivation_hook>
  43. >::type index_t;
  44. };
  45. /// @endcond
  46. //!Index type based in boost::intrusive::set.
  47. //!Just derives from boost::intrusive::set
  48. //!and defines the interface needed by managed memory segments*/
  49. template <class MapConfig>
  50. class iset_index
  51. //Derive class from map specialization
  52. : public iset_index_aux<MapConfig>::index_t
  53. {
  54. /// @cond
  55. typedef iset_index_aux<MapConfig> index_aux;
  56. typedef typename index_aux::index_t index_type;
  57. typedef typename MapConfig::
  58. intrusive_compare_key_type intrusive_compare_key_type;
  59. typedef typename MapConfig::char_type char_type;
  60. /// @endcond
  61. public:
  62. typedef typename index_type::iterator iterator;
  63. typedef typename index_type::const_iterator const_iterator;
  64. typedef typename index_type::insert_commit_data insert_commit_data;
  65. typedef typename index_type::value_type value_type;
  66. /// @cond
  67. private:
  68. struct intrusive_key_value_less
  69. {
  70. bool operator()(const intrusive_compare_key_type &i, const value_type &b) const
  71. {
  72. std::size_t blen = b.name_length();
  73. return (i.m_len < blen) ||
  74. (i.m_len == blen &&
  75. std::char_traits<char_type>::compare
  76. (i.mp_str, b.name(), i.m_len) < 0);
  77. }
  78. bool operator()(const value_type &b, const intrusive_compare_key_type &i) const
  79. {
  80. std::size_t blen = b.name_length();
  81. return (blen < i.m_len) ||
  82. (blen == i.m_len &&
  83. std::char_traits<char_type>::compare
  84. (b.name(), i.mp_str, i.m_len) < 0);
  85. }
  86. };
  87. /// @endcond
  88. public:
  89. //!Constructor. Takes a pointer to the
  90. //!segment manager. Can throw
  91. iset_index(typename MapConfig::segment_manager_base *)
  92. : index_type(/*typename index_aux::value_compare()*/)
  93. {}
  94. //!This reserves memory to optimize the insertion of n
  95. //!elements in the index
  96. void reserve(typename MapConfig::segment_manager_base::size_type)
  97. { /*Does nothing, map has not reserve or rehash*/ }
  98. //!This frees all unnecessary memory
  99. void shrink_to_fit()
  100. { /*Does nothing, this intrusive index does not allocate memory;*/ }
  101. iterator find(const intrusive_compare_key_type &key)
  102. { return index_type::find(key, intrusive_key_value_less()); }
  103. const_iterator find(const intrusive_compare_key_type &key) const
  104. { return index_type::find(key, intrusive_key_value_less()); }
  105. std::pair<iterator, bool>insert_check
  106. (const intrusive_compare_key_type &key, insert_commit_data &commit_data)
  107. { return index_type::insert_check(key, intrusive_key_value_less(), commit_data); }
  108. };
  109. /// @cond
  110. //!Trait class to detect if an index is an intrusive
  111. //!index.
  112. template<class MapConfig>
  113. struct is_intrusive_index
  114. <boost::interprocess::iset_index<MapConfig> >
  115. {
  116. static const bool value = true;
  117. };
  118. /// @endcond
  119. } //namespace interprocess {
  120. } //namespace boost
  121. #include <boost/interprocess/detail/config_end.hpp>
  122. #endif //#ifndef BOOST_INTERPROCESS_ISET_INDEX_HPP