index_base.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Copyright 2003-2013 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)&&(_MSC_VER>=1200)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/detail/allocator_utilities.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/move/core.hpp>
  17. #include <boost/move/utility.hpp>
  18. #include <boost/mpl/vector.hpp>
  19. #include <boost/multi_index/detail/copy_map.hpp>
  20. #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp>
  21. #include <boost/multi_index/detail/node_type.hpp>
  22. #include <boost/multi_index/detail/vartempl_support.hpp>
  23. #include <boost/multi_index_container_fwd.hpp>
  24. #include <boost/tuple/tuple.hpp>
  25. #include <utility>
  26. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  27. #include <boost/multi_index/detail/index_loader.hpp>
  28. #include <boost/multi_index/detail/index_saver.hpp>
  29. #endif
  30. namespace boost{
  31. namespace multi_index{
  32. namespace detail{
  33. /* The role of this class is threefold:
  34. * - tops the linear hierarchy of indices.
  35. * - terminates some cascading backbone function calls (insert_, etc.),
  36. * - grants access to the backbone functions of the final
  37. * multi_index_container class (for access restriction reasons, these
  38. * cannot be called directly from the index classes.)
  39. */
  40. struct lvalue_tag{};
  41. struct rvalue_tag{};
  42. struct emplaced_tag{};
  43. template<typename Value,typename IndexSpecifierList,typename Allocator>
  44. class index_base
  45. {
  46. protected:
  47. typedef index_node_base<Value,Allocator> node_type;
  48. typedef typename multi_index_node_type<
  49. Value,IndexSpecifierList,Allocator>::type final_node_type;
  50. typedef multi_index_container<
  51. Value,IndexSpecifierList,Allocator> final_type;
  52. typedef tuples::null_type ctor_args_list;
  53. typedef typename
  54. boost::detail::allocator::rebind_to<
  55. Allocator,
  56. typename Allocator::value_type>::type final_allocator_type;
  57. typedef mpl::vector0<> index_type_list;
  58. typedef mpl::vector0<> iterator_type_list;
  59. typedef mpl::vector0<> const_iterator_type_list;
  60. typedef copy_map<
  61. final_node_type,
  62. final_allocator_type> copy_map_type;
  63. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  64. typedef index_saver<
  65. node_type,
  66. final_allocator_type> index_saver_type;
  67. typedef index_loader<
  68. node_type,
  69. final_node_type,
  70. final_allocator_type> index_loader_type;
  71. #endif
  72. private:
  73. typedef Value value_type;
  74. protected:
  75. explicit index_base(const ctor_args_list&,const Allocator&){}
  76. index_base(
  77. const index_base<Value,IndexSpecifierList,Allocator>&,
  78. do_not_copy_elements_tag)
  79. {}
  80. void copy_(
  81. const index_base<Value,IndexSpecifierList,Allocator>&,const copy_map_type&)
  82. {}
  83. node_type* insert_(const value_type& v,node_type* x,lvalue_tag)
  84. {
  85. boost::detail::allocator::construct(&x->value(),v);
  86. return x;
  87. }
  88. node_type* insert_(const value_type& v,node_type* x,rvalue_tag)
  89. {
  90. /* This shoud have used a modified, T&&-compatible version of
  91. * boost::detail::allocator::construct, but
  92. * <boost/detail/allocator_utilities.hpp> is too old and venerable to mess
  93. * with; besides, it is a general internal utility and the imperfect
  94. * perfect forwarding emulation of Boost.Move might break other libs.
  95. */
  96. new (&x->value()) value_type(boost::move(const_cast<value_type&>(v)));
  97. return x;
  98. }
  99. node_type* insert_(const value_type&,node_type* x,emplaced_tag)
  100. {
  101. return x;
  102. }
  103. node_type* insert_(const value_type& v,node_type*,node_type* x,lvalue_tag)
  104. {
  105. boost::detail::allocator::construct(&x->value(),v);
  106. return x;
  107. }
  108. node_type* insert_(const value_type& v,node_type*,node_type* x,rvalue_tag)
  109. {
  110. new (&x->value()) value_type(boost::move(const_cast<value_type&>(v)));
  111. return x;
  112. }
  113. node_type* insert_(const value_type&,node_type*,node_type* x,emplaced_tag)
  114. {
  115. return x;
  116. }
  117. void erase_(node_type* x)
  118. {
  119. boost::detail::allocator::destroy(&x->value());
  120. }
  121. void delete_node_(node_type* x)
  122. {
  123. boost::detail::allocator::destroy(&x->value());
  124. }
  125. void clear_(){}
  126. void swap_(index_base<Value,IndexSpecifierList,Allocator>&){}
  127. void swap_elements_(index_base<Value,IndexSpecifierList,Allocator>&){}
  128. bool replace_(const value_type& v,node_type* x,lvalue_tag)
  129. {
  130. x->value()=v;
  131. return true;
  132. }
  133. bool replace_(const value_type& v,node_type* x,rvalue_tag)
  134. {
  135. x->value()=boost::move(const_cast<value_type&>(v));
  136. return true;
  137. }
  138. bool modify_(node_type*){return true;}
  139. bool modify_rollback_(node_type*){return true;}
  140. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  141. /* serialization */
  142. template<typename Archive>
  143. void save_(Archive&,const unsigned int,const index_saver_type&)const{}
  144. template<typename Archive>
  145. void load_(Archive&,const unsigned int,const index_loader_type&){}
  146. #endif
  147. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  148. /* invariant stuff */
  149. bool invariant_()const{return true;}
  150. #endif
  151. /* access to backbone memfuns of Final class */
  152. final_type& final(){return *static_cast<final_type*>(this);}
  153. const final_type& final()const{return *static_cast<const final_type*>(this);}
  154. final_node_type* final_header()const{return final().header();}
  155. bool final_empty_()const{return final().empty_();}
  156. std::size_t final_size_()const{return final().size_();}
  157. std::size_t final_max_size_()const{return final().max_size_();}
  158. std::pair<final_node_type*,bool> final_insert_(const value_type& x)
  159. {return final().insert_(x);}
  160. std::pair<final_node_type*,bool> final_insert_rv_(const value_type& x)
  161. {return final().insert_rv_(x);}
  162. template<typename T>
  163. std::pair<final_node_type*,bool> final_insert_ref_(T& t)
  164. {return final().insert_ref_(t);}
  165. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  166. std::pair<final_node_type*,bool> final_emplace_(
  167. BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  168. {
  169. return final().emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  170. }
  171. std::pair<final_node_type*,bool> final_insert_(
  172. const value_type& x,final_node_type* position)
  173. {return final().insert_(x,position);}
  174. std::pair<final_node_type*,bool> final_insert_rv_(
  175. const value_type& x,final_node_type* position)
  176. {return final().insert_rv_(x,position);}
  177. template<typename T>
  178. std::pair<final_node_type*,bool> final_insert_ref_(
  179. T& t,final_node_type* position)
  180. {return final().insert_ref_(t,position);}
  181. template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK>
  182. std::pair<final_node_type*,bool> final_emplace_hint_(
  183. final_node_type* position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)
  184. {
  185. return final().emplace_hint_(
  186. position,BOOST_MULTI_INDEX_FORWARD_PARAM_PACK);
  187. }
  188. void final_erase_(final_node_type* x){final().erase_(x);}
  189. void final_delete_node_(final_node_type* x){final().delete_node_(x);}
  190. void final_delete_all_nodes_(){final().delete_all_nodes_();}
  191. void final_clear_(){final().clear_();}
  192. void final_swap_(final_type& x){final().swap_(x);}
  193. bool final_replace_(
  194. const value_type& k,final_node_type* x)
  195. {return final().replace_(k,x);}
  196. bool final_replace_rv_(
  197. const value_type& k,final_node_type* x)
  198. {return final().replace_rv_(k,x);}
  199. template<typename Modifier>
  200. bool final_modify_(Modifier& mod,final_node_type* x)
  201. {return final().modify_(mod,x);}
  202. template<typename Modifier,typename Rollback>
  203. bool final_modify_(Modifier& mod,Rollback& back,final_node_type* x)
  204. {return final().modify_(mod,back,x);}
  205. #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING)
  206. void final_check_invariant_()const{final().check_invariant_();}
  207. #endif
  208. };
  209. } /* namespace multi_index::detail */
  210. } /* namespace multi_index */
  211. } /* namespace boost */
  212. #endif