1
//////////////////////////////////////////////////////////////////////////////
3
// (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4
// Software License, Version 1.0. (See accompanying file
5
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
// See http://www.boost.org/libs/container for documentation.
9
//////////////////////////////////////////////////////////////////////////////
11
#ifndef BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP
12
#define BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP
14
#include "config_begin.hpp"
15
#include <boost/container/container_fwd.hpp>
16
#include <boost/container/detail/utilities.hpp>
17
#include <boost/container/detail/type_traits.hpp>
18
#include <boost/container/detail/transform_iterator.hpp>
19
#include <boost/intrusive/slist.hpp>
20
#include <boost/intrusive/pointer_traits.hpp>
21
#include <boost/type_traits/make_unsigned.hpp>
22
#include <boost/move/utility.hpp>
26
namespace container_detail {
28
template<class VoidPointer>
29
class basic_multiallocation_chain
32
typedef bi::slist_base_hook<bi::void_pointer<VoidPointer>
33
,bi::link_mode<bi::normal_link>
36
typedef typename boost::intrusive::pointer_traits
37
<VoidPointer>::template rebind_pointer<char>::type char_ptr;
38
typedef typename boost::intrusive::
39
pointer_traits<char_ptr>::difference_type difference_type;
41
typedef bi::slist< node
43
, bi::cache_last<true>
44
, bi::size_type<typename boost::make_unsigned<difference_type>::type>
46
slist_impl_t slist_impl_;
48
typedef typename boost::intrusive::pointer_traits
49
<VoidPointer>::template rebind_pointer<node>::type node_ptr;
50
typedef typename boost::intrusive::
51
pointer_traits<node_ptr> node_ptr_traits;
53
static node & to_node(const VoidPointer &p)
54
{ return *static_cast<node*>(static_cast<void*>(container_detail::to_raw_pointer(p))); }
56
static VoidPointer from_node(node &n)
57
{ return node_ptr_traits::pointer_to(n); }
59
static node_ptr to_node_ptr(const VoidPointer &p)
60
{ return node_ptr_traits::static_cast_from(p); }
62
BOOST_MOVABLE_BUT_NOT_COPYABLE(basic_multiallocation_chain)
66
typedef VoidPointer void_pointer;
67
typedef typename slist_impl_t::iterator iterator;
68
typedef typename slist_impl_t::size_type size_type;
70
basic_multiallocation_chain()
74
basic_multiallocation_chain(const void_pointer &b, const void_pointer &before_e, size_type n)
75
: slist_impl_(to_node_ptr(b), to_node_ptr(before_e), n)
78
basic_multiallocation_chain(BOOST_RV_REF(basic_multiallocation_chain) other)
79
: slist_impl_(::boost::move(other.slist_impl_))
82
basic_multiallocation_chain& operator=(BOOST_RV_REF(basic_multiallocation_chain) other)
84
slist_impl_ = ::boost::move(other.slist_impl_);
89
{ return slist_impl_.empty(); }
91
size_type size() const
92
{ return slist_impl_.size(); }
94
iterator before_begin()
95
{ return slist_impl_.before_begin(); }
98
{ return slist_impl_.begin(); }
101
{ return slist_impl_.end(); }
104
{ return slist_impl_.last(); }
107
{ slist_impl_.clear(); }
109
iterator insert_after(iterator it, void_pointer m)
110
{ return slist_impl_.insert_after(it, to_node(m)); }
112
void push_front(const void_pointer &m)
113
{ return slist_impl_.push_front(to_node(m)); }
115
void push_back(const void_pointer &m)
116
{ return slist_impl_.push_back(to_node(m)); }
118
void_pointer pop_front()
120
node & n = slist_impl_.front();
121
void_pointer ret = from_node(n);
122
slist_impl_.pop_front();
126
void splice_after(iterator after_this, basic_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
127
{ slist_impl_.splice_after(after_this, x.slist_impl_, before_b, before_e, n); }
129
void splice_after(iterator after_this, basic_multiallocation_chain &x)
130
{ slist_impl_.splice_after(after_this, x.slist_impl_); }
132
void erase_after(iterator before_b, iterator e, size_type n)
133
{ slist_impl_.erase_after(before_b, e, n); }
135
void_pointer incorporate_after(iterator after_this, const void_pointer &b, size_type unit_bytes, size_type num_units)
137
typedef typename boost::intrusive::pointer_traits<char_ptr> char_pointer_traits;
138
char_ptr elem = char_pointer_traits::static_cast_from(b);
140
char_ptr prev_elem = elem;
142
for(size_type i = 0; i != num_units-1; ++i, elem += unit_bytes){
143
::new (container_detail::to_raw_pointer(prev_elem)) void_pointer(elem);
146
slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(prev_elem), num_units);
151
void incorporate_after(iterator after_this, void_pointer b, void_pointer before_e, size_type n)
152
{ slist_impl_.incorporate_after(after_this, to_node_ptr(b), to_node_ptr(before_e), n); }
154
void swap(basic_multiallocation_chain &x)
155
{ slist_impl_.swap(x.slist_impl_); }
157
static iterator iterator_to(const void_pointer &p)
158
{ return slist_impl_t::s_iterator_to(to_node(p)); }
160
std::pair<void_pointer, void_pointer> extract_data()
162
std::pair<void_pointer, void_pointer> ret
163
(slist_impl_.begin().operator->()
164
,slist_impl_.last().operator->());
173
typedef typename container_detail::add_reference<T>::type result_type;
175
result_type operator()(U &ptr) const
176
{ return *static_cast<T*>(static_cast<void*>(&ptr)); }
179
template<class MultiallocationChain, class T>
180
class transform_multiallocation_chain
181
: public MultiallocationChain
184
BOOST_MOVABLE_BUT_NOT_COPYABLE(transform_multiallocation_chain)
185
//transform_multiallocation_chain(const transform_multiallocation_chain &);
186
//transform_multiallocation_chain & operator=(const transform_multiallocation_chain &);
188
typedef typename MultiallocationChain::void_pointer void_pointer;
189
typedef typename boost::intrusive::pointer_traits
190
<void_pointer> void_pointer_traits;
191
typedef typename void_pointer_traits::template
192
rebind_pointer<T>::type pointer;
193
typedef typename boost::intrusive::pointer_traits
194
<pointer> pointer_traits;
196
static pointer cast(const void_pointer &p)
197
{ return pointer_traits::static_cast_from(p); }
200
typedef transform_iterator
201
< typename MultiallocationChain::iterator
202
, container_detail::cast_functor <T> > iterator;
203
typedef typename MultiallocationChain::size_type size_type;
205
transform_multiallocation_chain()
206
: MultiallocationChain()
209
transform_multiallocation_chain(BOOST_RV_REF(transform_multiallocation_chain) other)
210
: MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
213
transform_multiallocation_chain(BOOST_RV_REF(MultiallocationChain) other)
214
: MultiallocationChain(::boost::move(static_cast<MultiallocationChain&>(other)))
217
transform_multiallocation_chain& operator=(BOOST_RV_REF(transform_multiallocation_chain) other)
219
return static_cast<MultiallocationChain&>
220
(this->MultiallocationChain::operator=(::boost::move(static_cast<MultiallocationChain&>(other))));
223
void push_front(const pointer &mem)
224
{ holder_.push_front(mem); }
226
void push_back(const pointer &mem)
227
{ return holder_.push_back(mem); }
229
void swap(transform_multiallocation_chain &other_chain)
230
{ holder_.swap(other_chain.holder_); }
232
void splice_after(iterator after_this, transform_multiallocation_chain &x, iterator before_b, iterator before_e, size_type n)
233
{ holder_.splice_after(after_this.base(), x.holder_, before_b.base(), before_e.base(), n); }
235
void incorporate_after(iterator after_this, pointer b, pointer before_e, size_type n)
236
{ holder_.incorporate_after(after_this.base(), b, before_e, n); }
239
{ return cast(this->MultiallocationChain::pop_front()); }
242
{ return holder_.empty(); }
244
iterator before_begin()
245
{ return iterator(holder_.before_begin()); }
248
{ return iterator(this->MultiallocationChain::begin()); }
251
{ return iterator(holder_.end()); }
254
{ return iterator(holder_.last()); }
256
size_type size() const
257
{ return holder_.size(); }
262
iterator insert_after(iterator it, pointer m)
263
{ return iterator(this->MultiallocationChain::insert_after(it.base(), m)); }
265
static iterator iterator_to(const pointer &p)
266
{ return iterator(MultiallocationChain::iterator_to(p)); }
268
std::pair<pointer, pointer> extract_data()
270
std::pair<void_pointer, void_pointer> data(this->MultiallocationChain::extract_data());
271
return std::pair<pointer, pointer>(cast(data.first), cast(data.second));
274
MultiallocationChain &extract_multiallocation_chain()
275
{ return holder_; }*/
280
// namespace container_detail {
281
// namespace container {
284
#include <boost/container/detail/config_end.hpp>
286
#endif //BOOST_CONTAINER_DETAIL_MULTIALLOCATION_CHAIN_HPP