index_base.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Copyright 2003-2020 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_BASE_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_INDEX_BASE_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/core/addressof.hpp>
  15. #include <boost/core/no_exceptions_support.hpp>
  16. #include <boost/detail/workaround.hpp>
  17. #include <boost/move/utility_core.hpp>
  18. #include <boost/mpl/vector.hpp>
  19. #include <boost/multi_index/detail/allocator_traits.hpp>
  20. #include <boost/multi_index/detail/copy_map.hpp>
  21. #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
  22. #include <boost/multi_index/detail/node_handle.hpp>
  23. #include <boost/multi_index/detail/node_type.hpp>
  24. #include <boost/multi_index/detail/vartempl_support.hpp>
  25. #include <boost/multi_index_container_fwd.hpp>
  26. #include <boost/tuple/tuple.hpp>
  27. #include <utility>
  28. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  29. #include <boost/multi_index/detail/index_loader.hpp>
  30. #include <boost/multi_index/detail/index_saver.hpp>
  31. #endif
  32. namespace boost{
  33. namespace multi_index{
  34. namespace detail{
  35. /* The role of this class is threefold:
  36. * - tops the linear hierarchy of indices.
  37. * - terminates some cascading backbone function calls (insert_, etc.),
  38. * - grants access to the backbone functions of the final
  39. * multi_index_container class (for access restriction reasons, these
  40. * cannot be called directly from the index classes.)
  41. */
  42. struct lvalue_tag{};
  43. struct rvalue_tag{};
  44. struct emplaced_tag{};
  45. template<typename Value,typename IndexSpecifierList,typename Allocator>
  46. class index_base
  47. {
  48. protected:
  49. typedef index_node_base<Value,Allocator> index_node_type;
  50. typedef typename multi_index_node_type<
  51. Value,IndexSpecifierList,Allocator>::type final_node_type;
  52. typedef multi_index_container<
  53. Value,IndexSpecifierList,Allocator> final_type;
  54. typedef tuples::null_type ctor_args_list;
  55. typedef typename rebind_alloc_for<
  56. Allocator,typename Allocator::value_type
  57. >::type final_allocator_type;
  58. typedef node_handle<
  59. final_node_type,final_allocator_type> final_node_handle_type;
  60. typedef mpl::vector0<> index_type_list;
  61. typedef mpl::vector0<> iterator_type_list;
  62. typedef mpl::vector0<> const_iterator_type_list;
  63. typedef copy_map<
  64. final_node_type,
  65. final_allocator_type> copy_map_type;
  66. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  67. typedef index_saver<
  68. index_node_type,
  69. final_allocator_type> index_saver_type;
  70. typedef index_loader<
  71. index_node_type,
  72. final_node_type,
  73. final_allocator_type> index_loader_type;
  74. #endif
  75. private:
  76. typedef Value value_type;
  77. typedef allocator_traits<Allocator> alloc_traits;
  78. typedef typename alloc_traits::size_type size_type;
  79. protected:
  80. explicit index_base(const ctor_args_list&,const Allocator&){}
  81. index_base(
  82. const index_base<Value,IndexSpecifierList,Allocator>&,
  83. do_not_copy_elements_tag)
  84. {}
  85. void copy_(
  86. const index_base<Value,IndexSpecifierList,Allocator>&,const copy_map_type&)
  87. {}
  88. final_node_type* insert_(const value_type& v,final_node_type*& x,lvalue_tag)
  89. {
  90. x=final().allocate_node();
  91. BOOST_TRY{
  92. final().construct_value(x,v);
  93. }
  94. BOOST_CATCH(...){
  95. final().deallocate_node(x);
  96. BOOST_RETHROW;
  97. }
  98. BOOST_CATCH_END
  99. return x;
  100. }
  101. final_node_type* insert_(const value_type& v,final_node_type*& x,rvalue_tag)
  102. {
  103. x=final().allocate_node();
  104. BOOST_TRY{
  105. final().construct_value(x,boost::move(const_cast<value_type&>(v)));
  106. }
  107. BOOST_CATCH(...){
  108. final().deallocate_node(x);
  109. BOOST_RETHROW;
  110. }
  111. BOOST_CATCH_END
  112. return x;
  113. }
  114. final_node_type* insert_(const value_type&,final_node_type*& x,emplaced_tag)
  115. {
  116. return x;
  117. }
  118. final_node_type* insert_(
  119. const value_type& v,index_node_type*,final_node_type*& x,lvalue_tag)
  120. {
  121. return insert_(v,x,lvalue_tag());
  122. }
  123. final_node_type* insert_(
  124. const value_type& v,index_node_type*,final_node_type*& x,rvalue_tag)
  125. {
  126. return insert_(v,x,rvalue_tag());
  127. }
  128. final_node_type* insert_(
  129. const value_type&,index_node_type*,final_node_type*& x,emplaced_tag)
  130. {
  131. return x;
  132. }
  133. void extract_(index_node_type*){}
  134. void clear_(){}
  135. template<typename BoolConstant>
  136. void swap_(
  137. index_base<Value,IndexSpecifierList,Allocator>&,
  138. BoolConstant /* swap_allocators */)
  139. {}
  140. void swap_elements_(index_base<Value,IndexSpecifierList,Allocator>&){}
  141. bool replace_(const value_type& v,index_node_type* x,lvalue_tag)
  142. {
  143. x->value()=v;
  144. return true;
  145. }
  146. bool replace_(const value_type& v,index_node_type* x,rvalue_tag)
  147. {
  148. x->value()=boost::move(const_cast<value_type&>(v));
  149. return true;
  150. }
  151. bool modify_(index_node_type*){return true;}
  152. bool modify_rollback_(index_node_type*){return true;}
  153. bool check_rollback_(index_node_type*)const{return true;}
  154. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  155. /* serialization */
  156. template<typename Archive>
  157. void save_(Archive&,const unsigned int,const index_saver_type&)const{}
  158. template<typename Archive>
  159. void load_(Archive&,const unsigned int,const index_loader_type&){}
  160. #endif
  161. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  162. /* invariant stuff */
  163. bool invariant_()const{return true;}
  164. #endif
  165. /* access to backbone memfuns of Final class */
  166. final_type& final(){return *static_cast<final_type*>(this);}
  167. const final_type& final()const{return *static_cast<const final_type*>(this);}
  168. final_node_type* final_header()const{return final().header();}
  169. bool final_empty_()const{return final().empty_();}
  170. size_type final_size_()const{return final().size_();}
  171. size_type final_max_size_()const{return final().max_size_();}
  172. std::pair<final_node_type*,bool> final_insert_(const value_type& x)
  173. {return final().insert_(x);}
  174. std::pair<final_node_type*,bool> final_insert_rv_(const value_type& x)
  175. {return final().insert_rv_(x);}
  176. template<typename T>
  177. std::pair<final_node_type*,bool> final_insert_ref_(const T& t)
  178. {return final().insert_ref_(t);}
  179. template<typename T>
  180. std::pair<final_node_type*,bool> final_insert_ref_(T& t)
  181. {return final().insert_ref_(t);}
  182. std::pair<final_node_type*,bool> final_insert_nh_(final_node_handle_type& nh)
  183. {return final().insert_nh_(nh);}
  184. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  185. std::pair<final_node_type*,bool> final_emplace_(
  186. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  187. {
  188. return final().emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  189. }
  190. std::pair<final_node_type*,bool> final_insert_(
  191. const value_type& x,final_node_type* position)
  192. {return final().insert_(x,position);}
  193. std::pair<final_node_type*,bool> final_insert_rv_(
  194. const value_type& x,final_node_type* position)
  195. {return final().insert_rv_(x,position);}
  196. template<typename T>
  197. std::pair<final_node_type*,bool> final_insert_ref_(
  198. const T& t,final_node_type* position)
  199. {return final().insert_ref_(t,position);}
  200. template<typename T>
  201. std::pair<final_node_type*,bool> final_insert_ref_(
  202. T& t,final_node_type* position)
  203. {return final().insert_ref_(t,position);}
  204. std::pair<final_node_type*,bool> final_insert_nh_(
  205. final_node_handle_type& nh,final_node_type* position)
  206. {return final().insert_nh_(nh,position);}
  207. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  208. std::pair<final_node_type*,bool> final_emplace_hint_(
  209. final_node_type* position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  210. {
  211. return final().emplace_hint_(
  212. position,BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  213. }
  214. final_node_handle_type final_extract_(final_node_type* x)
  215. {
  216. return final().extract_(x);
  217. }
  218. void final_erase_(final_node_type* x){final().erase_(x);}
  219. void final_delete_node_(final_node_type* x){final().delete_node_(x);}
  220. void final_delete_all_nodes_(){final().delete_all_nodes_();}
  221. void final_clear_(){final().clear_();}
  222. void final_swap_(final_type& x){final().swap_(x);}
  223. bool final_replace_(
  224. const value_type& k,final_node_type* x)
  225. {return final().replace_(k,x);}
  226. bool final_replace_rv_(
  227. const value_type& k,final_node_type* x)
  228. {return final().replace_rv_(k,x);}
  229. template<typename Modifier>
  230. bool final_modify_(Modifier& mod,final_node_type* x)
  231. {return final().modify_(mod,x);}
  232. template<typename Modifier,typename Rollback>
  233. bool final_modify_(Modifier& mod,Rollback& back,final_node_type* x)
  234. {return final().modify_(mod,back,x);}
  235. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  236. void final_check_invariant_()const{final().check_invariant_();}
  237. #endif
  238. };
  239. } /* namespace multi_index::detail */
  240. } /* namespace multi_index */
  241. } /* namespace boost */
  242. #endif