index_base.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Copyright 2003-2018 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/detail/no_exceptions_support.hpp>
  16. #include <boost/detail/workaround.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/mpl/vector.hpp>
  20. #include <boost/multi_index/detail/allocator_traits.hpp>
  21. #include <boost/multi_index/detail/copy_map.hpp>
  22. #include <boost/multi_index/detail/do_not_copy_elements_tag.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> 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 mpl::vector0<> index_type_list;
  59. typedef mpl::vector0<> iterator_type_list;
  60. typedef mpl::vector0<> const_iterator_type_list;
  61. typedef copy_map<
  62. final_node_type,
  63. final_allocator_type> copy_map_type;
  64. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  65. typedef index_saver<
  66. node_type,
  67. final_allocator_type> index_saver_type;
  68. typedef index_loader<
  69. node_type,
  70. final_node_type,
  71. final_allocator_type> index_loader_type;
  72. #endif
  73. private:
  74. typedef Value value_type;
  75. typedef allocator_traits<Allocator> alloc_traits;
  76. typedef typename alloc_traits::size_type size_type;
  77. protected:
  78. explicit index_base(const ctor_args_list&,const Allocator&){}
  79. index_base(
  80. const index_base<Value,IndexSpecifierList,Allocator>&,
  81. do_not_copy_elements_tag)
  82. {}
  83. void copy_(
  84. const index_base<Value,IndexSpecifierList,Allocator>&,const copy_map_type&)
  85. {}
  86. final_node_type* insert_(const value_type& v,final_node_type*& x,lvalue_tag)
  87. {
  88. x=final().allocate_node();
  89. BOOST_TRY{
  90. final().construct_value(x,v);
  91. }
  92. BOOST_CATCH(...){
  93. final().deallocate_node(x);
  94. BOOST_RETHROW;
  95. }
  96. BOOST_CATCH_END
  97. return x;
  98. }
  99. final_node_type* insert_(const value_type& v,final_node_type*& x,rvalue_tag)
  100. {
  101. x=final().allocate_node();
  102. BOOST_TRY{
  103. final().construct_value(x,boost::move(const_cast<value_type&>(v)));
  104. }
  105. BOOST_CATCH(...){
  106. final().deallocate_node(x);
  107. BOOST_RETHROW;
  108. }
  109. BOOST_CATCH_END
  110. return x;
  111. }
  112. final_node_type* insert_(const value_type&,final_node_type*& x,emplaced_tag)
  113. {
  114. return x;
  115. }
  116. final_node_type* insert_(
  117. const value_type& v,node_type*,final_node_type*& x,lvalue_tag)
  118. {
  119. return insert_(v,x,lvalue_tag());
  120. }
  121. final_node_type* insert_(
  122. const value_type& v,node_type*,final_node_type*& x,rvalue_tag)
  123. {
  124. return insert_(v,x,rvalue_tag());
  125. }
  126. final_node_type* insert_(
  127. const value_type&,node_type*,final_node_type*& x,emplaced_tag)
  128. {
  129. return x;
  130. }
  131. void erase_(node_type* x)
  132. {
  133. final().destroy_value(static_cast<final_node_type*>(x));
  134. }
  135. void delete_node_(node_type* x)
  136. {
  137. final().destroy_value(static_cast<final_node_type*>(x));
  138. }
  139. void clear_(){}
  140. void swap_(index_base<Value,IndexSpecifierList,Allocator>&){}
  141. void swap_elements_(index_base<Value,IndexSpecifierList,Allocator>&){}
  142. bool replace_(const value_type& v,node_type* x,lvalue_tag)
  143. {
  144. x->value()=v;
  145. return true;
  146. }
  147. bool replace_(const value_type& v,node_type* x,rvalue_tag)
  148. {
  149. x->value()=boost::move(const_cast<value_type&>(v));
  150. return true;
  151. }
  152. bool modify_(node_type*){return true;}
  153. bool modify_rollback_(node_type*){return true;}
  154. bool check_rollback_(node_type*)const{return true;}
  155. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  156. /* serialization */
  157. template<typename Archive>
  158. void save_(Archive&,const unsigned int,const index_saver_type&)const{}
  159. template<typename Archive>
  160. void load_(Archive&,const unsigned int,const index_loader_type&){}
  161. #endif
  162. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  163. /* invariant stuff */
  164. bool invariant_()const{return true;}
  165. #endif
  166. /* access to backbone memfuns of Final class */
  167. final_type& final(){return *static_cast<final_type*>(this);}
  168. const final_type& final()const{return *static_cast<const final_type*>(this);}
  169. final_node_type* final_header()const{return final().header();}
  170. bool final_empty_()const{return final().empty_();}
  171. size_type final_size_()const{return final().size_();}
  172. size_type final_max_size_()const{return final().max_size_();}
  173. std::pair<final_node_type*,bool> final_insert_(const value_type& x)
  174. {return final().insert_(x);}
  175. std::pair<final_node_type*,bool> final_insert_rv_(const value_type& x)
  176. {return final().insert_rv_(x);}
  177. template<typename T>
  178. std::pair<final_node_type*,bool> final_insert_ref_(const T& t)
  179. {return final().insert_ref_(t);}
  180. template<typename T>
  181. std::pair<final_node_type*,bool> final_insert_ref_(T& t)
  182. {return final().insert_ref_(t);}
  183. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  184. std::pair<final_node_type*,bool> final_emplace_(
  185. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  186. {
  187. return final().emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  188. }
  189. std::pair<final_node_type*,bool> final_insert_(
  190. const value_type& x,final_node_type* position)
  191. {return final().insert_(x,position);}
  192. std::pair<final_node_type*,bool> final_insert_rv_(
  193. const value_type& x,final_node_type* position)
  194. {return final().insert_rv_(x,position);}
  195. template<typename T>
  196. std::pair<final_node_type*,bool> final_insert_ref_(
  197. const T& t,final_node_type* position)
  198. {return final().insert_ref_(t,position);}
  199. template<typename T>
  200. std::pair<final_node_type*,bool> final_insert_ref_(
  201. T& t,final_node_type* position)
  202. {return final().insert_ref_(t,position);}
  203. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  204. std::pair<final_node_type*,bool> final_emplace_hint_(
  205. final_node_type* position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  206. {
  207. return final().emplace_hint_(
  208. position,BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  209. }
  210. void final_erase_(final_node_type* x){final().erase_(x);}
  211. void final_delete_node_(final_node_type* x){final().delete_node_(x);}
  212. void final_delete_all_nodes_(){final().delete_all_nodes_();}
  213. void final_clear_(){final().clear_();}
  214. void final_swap_(final_type& x){final().swap_(x);}
  215. bool final_replace_(
  216. const value_type& k,final_node_type* x)
  217. {return final().replace_(k,x);}
  218. bool final_replace_rv_(
  219. const value_type& k,final_node_type* x)
  220. {return final().replace_rv_(k,x);}
  221. template<typename Modifier>
  222. bool final_modify_(Modifier& mod,final_node_type* x)
  223. {return final().modify_(mod,x);}
  224. template<typename Modifier,typename Rollback>
  225. bool final_modify_(Modifier& mod,Rollback& back,final_node_type* x)
  226. {return final().modify_(mod,back,x);}
  227. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  228. void final_check_invariant_()const{final().check_invariant_();}
  229. #endif
  230. };
  231. } /* namespace multi_index::detail */
  232. } /* namespace multi_index */
  233. } /* namespace boost */
  234. #endif