iset_index.hpp 5.5 KB

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