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