iset_index.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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::char_type char_type;
  46. typedef typename MapConfig::template
  47. intrusive_value_type<derivation_hook>::type value_type;
  48. typedef typename MapConfig::compare_key_type compare_key_type;
  49. struct less_function
  50. {
  51. bool operator()(const compare_key_type&i, const value_type &b) const
  52. {
  53. std::size_t blen = b.name_length();
  54. return (i.m_len < blen) ||
  55. (i.m_len == blen &&
  56. std::char_traits<char_type>::compare
  57. (i.mp_str, b.name(), i.m_len) < 0);
  58. }
  59. bool operator()(const value_type &b, const compare_key_type&i) const
  60. {
  61. std::size_t blen = b.name_length();
  62. return (blen < i.m_len) ||
  63. (blen == i.m_len &&
  64. std::char_traits<char_type>::compare
  65. (b.name(), i.mp_str, i.m_len) < 0);
  66. }
  67. bool operator()(const value_type& a, const value_type& b) const
  68. {
  69. std::size_t alen = a.name_length();
  70. std::size_t blen = b.name_length();
  71. return (alen < blen) ||
  72. (alen == blen &&
  73. std::char_traits<char_type>::compare
  74. (a.name(), b.name(), alen) < 0);
  75. }
  76. };
  77. typedef std::less<value_type> value_compare;
  78. typedef typename bi::make_set
  79. < value_type
  80. , bi::base_hook<derivation_hook>
  81. , bi::compare<less_function>
  82. >::type index_t;
  83. };
  84. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  85. //!Index type based in boost::intrusive::set.
  86. //!Just derives from boost::intrusive::set
  87. //!and defines the interface needed by managed memory segments*/
  88. template <class MapConfig>
  89. class iset_index
  90. //Derive class from map specialization
  91. : private iset_index_aux<MapConfig>::index_t
  92. {
  93. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  94. typedef iset_index_aux<MapConfig> index_aux;
  95. typedef typename index_aux::index_t index_type;
  96. typedef typename MapConfig::char_type char_type;
  97. typedef typename index_aux::less_function less_function;
  98. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  99. public:
  100. typedef typename index_type::iterator iterator;
  101. typedef typename index_type::const_iterator const_iterator;
  102. typedef typename index_type::insert_commit_data insert_commit_data;
  103. typedef typename index_type::value_type value_type;
  104. typedef typename MapConfig::compare_key_type compare_key_type;
  105. typedef value_type index_data_t;
  106. public:
  107. using index_type::begin;
  108. using index_type::end;
  109. using index_type::size;
  110. using index_type::erase;
  111. //!Constructor. Takes a pointer to the
  112. //!segment manager. Can throw
  113. iset_index(typename MapConfig::segment_manager_base *)
  114. : index_type(/*typename index_aux::value_compare()*/)
  115. {}
  116. //!This reserves memory to optimize the insertion of n
  117. //!elements in the index
  118. void reserve(typename MapConfig::segment_manager_base::size_type)
  119. { /*Does nothing, map has not reserve or rehash*/ }
  120. //!This frees all unnecessary memory
  121. void shrink_to_fit()
  122. { /*Does nothing, this intrusive index does not allocate memory;*/ }
  123. iterator find(const compare_key_type&key)
  124. { return index_type::find(key, less_function()); }
  125. const_iterator find(const compare_key_type&key) const
  126. { return index_type::find(key, less_function()); }
  127. std::pair<iterator, bool>insert_check
  128. (const compare_key_type &key, insert_commit_data &commit_data)
  129. { return index_type::insert_check(key, less_function(), commit_data); }
  130. iterator insert_commit
  131. (const compare_key_type &, void*, index_data_t&v, insert_commit_data& commit_data)
  132. { return index_type::insert_commit(v, commit_data); }
  133. };
  134. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  135. //!Trait class to detect if an index is an intrusive
  136. //!index.
  137. template<class MapConfig>
  138. struct is_intrusive_index
  139. <boost::interprocess::iset_index<MapConfig> >
  140. {
  141. static const bool value = true;
  142. };
  143. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  144. } //namespace interprocess {
  145. } //namespace boost
  146. #include <boost/interprocess/detail/config_end.hpp>
  147. #endif //#ifndef BOOST_INTERPROCESS_ISET_INDEX_HPP