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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_DETAIL_NODE_HANDLE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_NODE_HANDLE_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/core/addressof.hpp>
19 #include <boost/detail/workaround.hpp>
20 #include <boost/move/core.hpp>
21 #include <boost/move/utility_core.hpp>
22 #include <boost/multi_index_container_fwd.hpp>
23 #include <boost/multi_index/detail/allocator_traits.hpp>
24 #include <boost/type_traits/aligned_storage.hpp>
25 #include <boost/type_traits/alignment_of.hpp>
28 #if !defined(BOOST_NO_SFINAE)
29 #include <boost/type_traits/is_const.hpp>
30 #include <boost/type_traits/is_same.hpp>
31 #include <boost/utility/enable_if.hpp>
36 namespace multi_index{
40 /* Node handle template class following [container.node] specs.
43 #include <boost/multi_index/detail/define_if_constexpr_macro.hpp>
45 template<typename Node,typename Allocator>
49 typedef typename Node::value_type value_type;
50 typedef Allocator allocator_type;
53 typedef allocator_traits<allocator_type> alloc_traits;
56 node_handle()BOOST_NOEXCEPT:node(0){}
58 node_handle(BOOST_RV_REF(node_handle) x)BOOST_NOEXCEPT:node(x.node)
61 move_construct_allocator(boost::move(x));
62 x.destroy_allocator();
75 node_handle& operator=(BOOST_RV_REF(node_handle) x)
81 BOOST_MULTI_INDEX_IF_CONSTEXPR(
82 alloc_traits::propagate_on_container_move_assignment::value){
83 move_assign_allocator(boost::move(x));
85 x.destroy_allocator();
92 move_construct_allocator(boost::move(x));
93 x.destroy_allocator();
101 value_type& value()const{return node->value();}
102 allocator_type get_allocator()const{return *allocator_ptr();}
104 #if !defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
107 operator bool()const BOOST_NOEXCEPT{return (node!=0);}
109 #if BOOST_WORKAROUND(BOOST_GCC_VERSION,>=70000)&&__cplusplus<201103L
110 /* https://github.com/boostorg/config/issues/336 */
112 BOOST_ATTRIBUTE_NODISCARD
114 bool empty()const BOOST_NOEXCEPT{return (node==0);}
116 void swap(node_handle& x)
118 alloc_traits::propagate_on_container_swap::value||
119 alloc_traits::is_always_equal::value)
123 BOOST_MULTI_INDEX_IF_CONSTEXPR(
124 alloc_traits::propagate_on_container_swap::value){
126 swap(*allocator_ptr(),*x.allocator_ptr());
130 x.move_construct_allocator(boost::move(*this));
135 move_construct_allocator(boost::move(x));
136 x.destroy_allocator();
138 std::swap(node,x.node);
141 friend void swap(node_handle& x,node_handle& y)
142 BOOST_NOEXCEPT_IF(noexcept(x.swap(y)))
148 BOOST_MOVABLE_BUT_NOT_COPYABLE(node_handle)
150 template <typename,typename,typename>
151 friend class boost::multi_index::multi_index_container;
153 node_handle(Node* node_,const allocator_type& al):node(node_)
155 ::new (static_cast<void*>(allocator_ptr())) allocator_type(al);
166 #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
168 const allocator_type* allocator_ptr()const
170 return reinterpret_cast<const allocator_type*>(&space);
173 allocator_type* allocator_ptr()
175 return reinterpret_cast<allocator_type*>(&space);
178 #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
180 void move_construct_allocator(BOOST_RV_REF(node_handle) x)
182 ::new (static_cast<void*>(allocator_ptr()))
183 allocator_type(boost::move(*x.allocator_ptr()));
186 void move_assign_allocator(BOOST_RV_REF(node_handle) x)
188 *allocator_ptr()=boost::move(*x.allocator_ptr());
191 void destroy_allocator(){allocator_ptr()->~allocator_type();}
195 typedef typename rebind_alloc_for<
197 >::type node_allocator;
198 typedef detail::allocator_traits<node_allocator> node_alloc_traits;
199 typedef typename node_alloc_traits::pointer node_pointer;
201 alloc_traits::destroy(*allocator_ptr(),boost::addressof(node->value()));
202 node_allocator nal(*allocator_ptr());
203 node_alloc_traits::deallocate(nal,static_cast<node_pointer>(node),1);
207 typename aligned_storage<
208 sizeof(allocator_type),
209 alignment_of<allocator_type>::value
213 #include <boost/multi_index/detail/undef_if_constexpr_macro.hpp>
215 /* node handle insert return type template class following
216 * [container.insert.return] specs.
219 template<typename Iterator,typename NodeHandle>
220 struct insert_return_type
223 Iterator position_,bool inserted_,BOOST_RV_REF(NodeHandle) node_):
224 position(position_),inserted(inserted_),node(boost::move(node_)){}
225 insert_return_type(BOOST_RV_REF(insert_return_type) x):
226 position(x.position),inserted(x.inserted),node(boost::move(x.node)){}
228 insert_return_type& operator=(BOOST_RV_REF(insert_return_type) x)
232 node=boost::move(x.node);
241 BOOST_MOVABLE_BUT_NOT_COPYABLE(insert_return_type)
244 /* utility for SFINAEing merge and related operations */
246 #if !defined(BOOST_NO_SFINAE)
248 #define BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(Dst,Src,T) \
249 typename enable_if_c< \
250 !is_const< Dst >::value&&!is_const< Src >::value&& \
251 is_same<typename Dst::node_type,typename Src::node_type>::value, \
257 #define BOOST_MULTI_INDEX_ENABLE_IF_MERGEABLE(Dst,Src,T) T
261 } /* namespace multi_index::detail */
263 } /* namespace multi_index */
265 } /* namespace boost */