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 9 #ifndef BOOST_MULTI_INDEX_DETAIL_INDEX_BASE_HPP 10 #define BOOST_MULTI_INDEX_DETAIL_INDEX_BASE_HPP 11 12 #if defined(_MSC_VER) 13 #pragma once 14 #endif 15 16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ 17 #include <boost/core/addressof.hpp> 18 #include <boost/core/no_exceptions_support.hpp> 19 #include <boost/detail/workaround.hpp> 20 #include <boost/move/utility_core.hpp> 21 #include <boost/mpl/vector.hpp> 22 #include <boost/multi_index/detail/allocator_traits.hpp> 23 #include <boost/multi_index/detail/copy_map.hpp> 24 #include <boost/multi_index/detail/do_not_copy_elements_tag.hpp> 25 #include <boost/multi_index/detail/node_handle.hpp> 26 #include <boost/multi_index/detail/node_type.hpp> 27 #include <boost/multi_index/detail/vartempl_support.hpp> 28 #include <boost/multi_index_container_fwd.hpp> 29 #include <boost/tuple/tuple.hpp> 30 #include <utility> 31 32 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION) 33 #include <boost/multi_index/detail/index_loader.hpp> 34 #include <boost/multi_index/detail/index_saver.hpp> 35 #endif 36 37 namespace boost{ 38 39 namespace multi_index{ 40 41 namespace detail{ 42 43 /* The role of this class is threefold: 44 * - tops the linear hierarchy of indices. 45 * - terminates some cascading backbone function calls (insert_, etc.), 46 * - grants access to the backbone functions of the final 47 * multi_index_container class (for access restriction reasons, these 48 * cannot be called directly from the index classes.) 49 */ 50 51 struct lvalue_tag{}; 52 struct rvalue_tag{}; 53 struct emplaced_tag{}; 54 55 template<typename Value,typename IndexSpecifierList,typename Allocator> 56 class index_base 57 { 58 protected: 59 typedef index_node_base<Value,Allocator> index_node_type; 60 typedef typename multi_index_node_type< 61 Value,IndexSpecifierList,Allocator>::type final_node_type; 62 typedef multi_index_container< 63 Value,IndexSpecifierList,Allocator> final_type; 64 typedef tuples::null_type ctor_args_list; 65 typedef typename rebind_alloc_for< 66 Allocator,typename Allocator::value_type 67 >::type final_allocator_type; 68 typedef node_handle< 69 final_node_type,final_allocator_type> final_node_handle_type; 70 typedef mpl::vector0<> index_type_list; 71 typedef mpl::vector0<> iterator_type_list; 72 typedef mpl::vector0<> const_iterator_type_list; 73 typedef copy_map< 74 final_node_type, 75 final_allocator_type> copy_map_type; 76 77 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION) 78 typedef index_saver< 79 index_node_type, 80 final_allocator_type> index_saver_type; 81 typedef index_loader< 82 index_node_type, 83 final_node_type, 84 final_allocator_type> index_loader_type; 85 #endif 86 87 private: 88 typedef Value value_type; 89 typedef allocator_traits<Allocator> alloc_traits; 90 typedef typename alloc_traits::size_type size_type; 91 92 protected: index_base(const ctor_args_list &,const Allocator &)93 explicit index_base(const ctor_args_list&,const Allocator&){} 94 index_base(const index_base<Value,IndexSpecifierList,Allocator> &,do_not_copy_elements_tag)95 index_base( 96 const index_base<Value,IndexSpecifierList,Allocator>&, 97 do_not_copy_elements_tag) 98 {} 99 copy_(const index_base<Value,IndexSpecifierList,Allocator> &,const copy_map_type &)100 void copy_( 101 const index_base<Value,IndexSpecifierList,Allocator>&,const copy_map_type&) 102 {} 103 insert_(const value_type & v,final_node_type * & x,lvalue_tag)104 final_node_type* insert_(const value_type& v,final_node_type*& x,lvalue_tag) 105 { 106 x=final().allocate_node(); 107 BOOST_TRY{ 108 final().construct_value(x,v); 109 } 110 BOOST_CATCH(...){ 111 final().deallocate_node(x); 112 BOOST_RETHROW; 113 } 114 BOOST_CATCH_END 115 return x; 116 } 117 insert_(const value_type & v,final_node_type * & x,rvalue_tag)118 final_node_type* insert_(const value_type& v,final_node_type*& x,rvalue_tag) 119 { 120 x=final().allocate_node(); 121 BOOST_TRY{ 122 final().construct_value(x,boost::move(const_cast<value_type&>(v))); 123 } 124 BOOST_CATCH(...){ 125 final().deallocate_node(x); 126 BOOST_RETHROW; 127 } 128 BOOST_CATCH_END 129 return x; 130 } 131 insert_(const value_type &,final_node_type * & x,emplaced_tag)132 final_node_type* insert_(const value_type&,final_node_type*& x,emplaced_tag) 133 { 134 return x; 135 } 136 insert_(const value_type & v,index_node_type *,final_node_type * & x,lvalue_tag)137 final_node_type* insert_( 138 const value_type& v,index_node_type*,final_node_type*& x,lvalue_tag) 139 { 140 return insert_(v,x,lvalue_tag()); 141 } 142 insert_(const value_type & v,index_node_type *,final_node_type * & x,rvalue_tag)143 final_node_type* insert_( 144 const value_type& v,index_node_type*,final_node_type*& x,rvalue_tag) 145 { 146 return insert_(v,x,rvalue_tag()); 147 } 148 insert_(const value_type &,index_node_type *,final_node_type * & x,emplaced_tag)149 final_node_type* insert_( 150 const value_type&,index_node_type*,final_node_type*& x,emplaced_tag) 151 { 152 return x; 153 } 154 extract_(index_node_type *)155 void extract_(index_node_type*){} 156 clear_()157 void clear_(){} 158 159 template<typename BoolConstant> swap_(index_base<Value,IndexSpecifierList,Allocator> &,BoolConstant)160 void swap_( 161 index_base<Value,IndexSpecifierList,Allocator>&, 162 BoolConstant /* swap_allocators */) 163 {} 164 swap_elements_(index_base<Value,IndexSpecifierList,Allocator> &)165 void swap_elements_(index_base<Value,IndexSpecifierList,Allocator>&){} 166 replace_(const value_type & v,index_node_type * x,lvalue_tag)167 bool replace_(const value_type& v,index_node_type* x,lvalue_tag) 168 { 169 x->value()=v; 170 return true; 171 } 172 replace_(const value_type & v,index_node_type * x,rvalue_tag)173 bool replace_(const value_type& v,index_node_type* x,rvalue_tag) 174 { 175 x->value()=boost::move(const_cast<value_type&>(v)); 176 return true; 177 } 178 modify_(index_node_type *)179 bool modify_(index_node_type*){return true;} 180 modify_rollback_(index_node_type *)181 bool modify_rollback_(index_node_type*){return true;} 182 check_rollback_(index_node_type *) const183 bool check_rollback_(index_node_type*)const{return true;} 184 185 #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION) 186 /* serialization */ 187 188 template<typename Archive> save_(Archive &,const unsigned int,const index_saver_type &) const189 void save_(Archive&,const unsigned int,const index_saver_type&)const{} 190 191 template<typename Archive> load_(Archive &,const unsigned int,const index_loader_type &)192 void load_(Archive&,const unsigned int,const index_loader_type&){} 193 #endif 194 195 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING) 196 /* invariant stuff */ 197 invariant_() const198 bool invariant_()const{return true;} 199 #endif 200 201 /* access to backbone memfuns of Final class */ 202 final()203 final_type& final(){return *static_cast<final_type*>(this);} final() const204 const final_type& final()const{return *static_cast<const final_type*>(this);} 205 final_header() const206 final_node_type* final_header()const{return final().header();} 207 final_empty_() const208 bool final_empty_()const{return final().empty_();} final_size_() const209 size_type final_size_()const{return final().size_();} final_max_size_() const210 size_type final_max_size_()const{return final().max_size_();} 211 final_insert_(const value_type & x)212 std::pair<final_node_type*,bool> final_insert_(const value_type& x) 213 {return final().insert_(x);} final_insert_rv_(const value_type & x)214 std::pair<final_node_type*,bool> final_insert_rv_(const value_type& x) 215 {return final().insert_rv_(x);} 216 template<typename T> final_insert_ref_(const T & t)217 std::pair<final_node_type*,bool> final_insert_ref_(const T& t) 218 {return final().insert_ref_(t);} 219 template<typename T> final_insert_ref_(T & t)220 std::pair<final_node_type*,bool> final_insert_ref_(T& t) 221 {return final().insert_ref_(t);} final_insert_nh_(final_node_handle_type & nh)222 std::pair<final_node_type*,bool> final_insert_nh_(final_node_handle_type& nh) 223 {return final().insert_nh_(nh);} 224 225 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK> final_emplace_(BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)226 std::pair<final_node_type*,bool> final_emplace_( 227 BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK) 228 { 229 return final().emplace_(BOOST_MULTI_INDEX_FORWARD_PARAM_PACK); 230 } 231 final_insert_(const value_type & x,final_node_type * position)232 std::pair<final_node_type*,bool> final_insert_( 233 const value_type& x,final_node_type* position) 234 {return final().insert_(x,position);} final_insert_rv_(const value_type & x,final_node_type * position)235 std::pair<final_node_type*,bool> final_insert_rv_( 236 const value_type& x,final_node_type* position) 237 {return final().insert_rv_(x,position);} 238 template<typename T> final_insert_ref_(const T & t,final_node_type * position)239 std::pair<final_node_type*,bool> final_insert_ref_( 240 const T& t,final_node_type* position) 241 {return final().insert_ref_(t,position);} 242 template<typename T> final_insert_ref_(T & t,final_node_type * position)243 std::pair<final_node_type*,bool> final_insert_ref_( 244 T& t,final_node_type* position) 245 {return final().insert_ref_(t,position);} final_insert_nh_(final_node_handle_type & nh,final_node_type * position)246 std::pair<final_node_type*,bool> final_insert_nh_( 247 final_node_handle_type& nh,final_node_type* position) 248 {return final().insert_nh_(nh,position);} 249 250 template<BOOST_MULTI_INDEX_TEMPLATE_PARAM_PACK> final_emplace_hint_(final_node_type * position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK)251 std::pair<final_node_type*,bool> final_emplace_hint_( 252 final_node_type* position,BOOST_MULTI_INDEX_FUNCTION_PARAM_PACK) 253 { 254 return final().emplace_hint_( 255 position,BOOST_MULTI_INDEX_FORWARD_PARAM_PACK); 256 } 257 final_extract_(final_node_type * x)258 final_node_handle_type final_extract_(final_node_type* x) 259 { 260 return final().extract_(x); 261 } 262 final_erase_(final_node_type * x)263 void final_erase_(final_node_type* x){final().erase_(x);} 264 final_delete_node_(final_node_type * x)265 void final_delete_node_(final_node_type* x){final().delete_node_(x);} final_delete_all_nodes_()266 void final_delete_all_nodes_(){final().delete_all_nodes_();} final_clear_()267 void final_clear_(){final().clear_();} 268 final_swap_(final_type & x)269 void final_swap_(final_type& x){final().swap_(x);} 270 final_replace_(const value_type & k,final_node_type * x)271 bool final_replace_( 272 const value_type& k,final_node_type* x) 273 {return final().replace_(k,x);} final_replace_rv_(const value_type & k,final_node_type * x)274 bool final_replace_rv_( 275 const value_type& k,final_node_type* x) 276 {return final().replace_rv_(k,x);} 277 278 template<typename Modifier> final_modify_(Modifier & mod,final_node_type * x)279 bool final_modify_(Modifier& mod,final_node_type* x) 280 {return final().modify_(mod,x);} 281 282 template<typename Modifier,typename Rollback> final_modify_(Modifier & mod,Rollback & back,final_node_type * x)283 bool final_modify_(Modifier& mod,Rollback& back,final_node_type* x) 284 {return final().modify_(mod,back,x);} 285 286 #if defined(BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING) final_check_invariant_() const287 void final_check_invariant_()const{final().check_invariant_();} 288 #endif 289 }; 290 291 } /* namespace multi_index::detail */ 292 293 } /* namespace multi_index */ 294 295 } /* namespace boost */ 296 297 #endif 298