index_base.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /* Copyright 2003-2017 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/allocator_utilities.hpp>
  16. #include <boost/detail/no_exceptions_support.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. #include <boost/move/core.hpp>
  19. #include <boost/move/utility.hpp>
  20. #include <boost/mpl/vector.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
  56. boost::detail::allocator::rebind_to<
  57. Allocator,
  58. typename Allocator::value_type
  59. >::type final_allocator_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. node_type,
  69. final_allocator_type> index_saver_type;
  70. typedef index_loader<
  71. node_type,
  72. final_node_type,
  73. final_allocator_type> index_loader_type;
  74. #endif
  75. private:
  76. typedef Value value_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. boost::detail::allocator::construct(boost::addressof(x->value()),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. /* This shoud have used a modified, T&&-compatible version of
  104. * boost::detail::allocator::construct, but
  105. * <boost/detail/allocator_utilities.hpp> is too old and venerable to
  106. * mess with; besides, it is a general internal utility and the imperfect
  107. * perfect forwarding emulation of Boost.Move might break other libs.
  108. */
  109. new (boost::addressof(x->value()))
  110. value_type(boost::move(const_cast<value_type&>(v)));
  111. }
  112. BOOST_CATCH(...){
  113. final().deallocate_node(x);
  114. BOOST_RETHROW;
  115. }
  116. BOOST_CATCH_END
  117. return x;
  118. }
  119. final_node_type* insert_(const value_type&,final_node_type*& x,emplaced_tag)
  120. {
  121. return x;
  122. }
  123. final_node_type* insert_(
  124. const value_type& v,node_type*,final_node_type*& x,lvalue_tag)
  125. {
  126. return insert_(v,x,lvalue_tag());
  127. }
  128. final_node_type* insert_(
  129. const value_type& v,node_type*,final_node_type*& x,rvalue_tag)
  130. {
  131. return insert_(v,x,rvalue_tag());
  132. }
  133. final_node_type* insert_(
  134. const value_type&,node_type*,final_node_type*& x,emplaced_tag)
  135. {
  136. return x;
  137. }
  138. void erase_(node_type* x)
  139. {
  140. boost::detail::allocator::destroy(boost::addressof(x->value()));
  141. }
  142. void delete_node_(node_type* x)
  143. {
  144. boost::detail::allocator::destroy(boost::addressof(x->value()));
  145. }
  146. void clear_(){}
  147. void swap_(index_base<Value,IndexSpecifierList,Allocator>&){}
  148. void swap_elements_(index_base<Value,IndexSpecifierList,Allocator>&){}
  149. bool replace_(const value_type& v,node_type* x,lvalue_tag)
  150. {
  151. x->value()=v;
  152. return true;
  153. }
  154. bool replace_(const value_type& v,node_type* x,rvalue_tag)
  155. {
  156. x->value()=boost::move(const_cast<value_type&>(v));
  157. return true;
  158. }
  159. bool modify_(node_type*){return true;}
  160. bool modify_rollback_(node_type*){return true;}
  161. bool check_rollback_(node_type*)const{return true;}
  162. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  163. /* serialization */
  164. template<typename Archive>
  165. void save_(Archive&,const unsigned int,const index_saver_type&)const{}
  166. template<typename Archive>
  167. void load_(Archive&,const unsigned int,const index_loader_type&){}
  168. #endif
  169. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  170. /* invariant stuff */
  171. bool invariant_()const{return true;}
  172. #endif
  173. /* access to backbone memfuns of Final class */
  174. final_type& final(){return *static_cast<final_type*>(this);}
  175. const final_type& final()const{return *static_cast<const final_type*>(this);}
  176. final_node_type* final_header()const{return final().header();}
  177. bool final_empty_()const{return final().empty_();}
  178. std::size_t final_size_()const{return final().size_();}
  179. std::size_t final_max_size_()const{return final().max_size_();}
  180. std::pair<final_node_type*,bool> final_insert_(const value_type& x)
  181. {return final().insert_(x);}
  182. std::pair<final_node_type*,bool> final_insert_rv_(const value_type& x)
  183. {return final().insert_rv_(x);}
  184. template<typename T>
  185. std::pair<final_node_type*,bool> final_insert_ref_(const T& t)
  186. {return final().insert_ref_(t);}
  187. template<typename T>
  188. std::pair<final_node_type*,bool> final_insert_ref_(T& t)
  189. {return final().insert_ref_(t);}
  190. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  191. std::pair<final_node_type*,bool> final_emplace_(
  192. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  193. {
  194. return final().emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  195. }
  196. std::pair<final_node_type*,bool> final_insert_(
  197. const value_type& x,final_node_type* position)
  198. {return final().insert_(x,position);}
  199. std::pair<final_node_type*,bool> final_insert_rv_(
  200. const value_type& x,final_node_type* position)
  201. {return final().insert_rv_(x,position);}
  202. template<typename T>
  203. std::pair<final_node_type*,bool> final_insert_ref_(
  204. const T& t,final_node_type* position)
  205. {return final().insert_ref_(t,position);}
  206. template<typename T>
  207. std::pair<final_node_type*,bool> final_insert_ref_(
  208. T& t,final_node_type* position)
  209. {return final().insert_ref_(t,position);}
  210. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  211. std::pair<final_node_type*,bool> final_emplace_hint_(
  212. final_node_type* position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  213. {
  214. return final().emplace_hint_(
  215. position,BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  216. }
  217. void final_erase_(final_node_type* x){final().erase_(x);}
  218. void final_delete_node_(final_node_type* x){final().delete_node_(x);}
  219. void final_delete_all_nodes_(){final().delete_all_nodes_();}
  220. void final_clear_(){final().clear_();}
  221. void final_swap_(final_type& x){final().swap_(x);}
  222. bool final_replace_(
  223. const value_type& k,final_node_type* x)
  224. {return final().replace_(k,x);}
  225. bool final_replace_rv_(
  226. const value_type& k,final_node_type* x)
  227. {return final().replace_rv_(k,x);}
  228. template<typename Modifier>
  229. bool final_modify_(Modifier& mod,final_node_type* x)
  230. {return final().modify_(mod,x);}
  231. template<typename Modifier,typename Rollback>
  232. bool final_modify_(Modifier& mod,Rollback& back,final_node_type* x)
  233. {return final().modify_(mod,back,x);}
  234. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  235. void final_check_invariant_()const{final().check_invariant_();}
  236. #endif
  237. };
  238. } /* namespace multi_index::detail */
  239. } /* namespace multi_index */
  240. } /* namespace boost */
  241. #endif