summaryrefslogtreecommitdiff
path: root/boost/container/detail/utilities.hpp
blob: 152b5e162ebfd7c7aee7e115a656790ecb897fb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////

#ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP
#define BOOST_CONTAINER_DETAIL_UTILITIES_HPP

#include "config_begin.hpp"
#include <cstdio>
#include <boost/type_traits/is_fundamental.hpp>
#include <boost/type_traits/is_pointer.hpp>
#include <boost/type_traits/is_enum.hpp>
#include <boost/type_traits/is_member_pointer.hpp>
#include <boost/type_traits/is_class.hpp>
#include <boost/move/move.hpp>
#include <boost/container/detail/mpl.hpp>
#include <boost/container/detail/type_traits.hpp>
#include <boost/container/allocator_traits.hpp>
#include <algorithm>

namespace boost {
namespace container {
namespace container_detail {

template <typename T>
inline T* addressof(T& obj)
{
   return static_cast<T*>(
	   static_cast<void*>(
	      const_cast<char*>(
            &reinterpret_cast<const char&>(obj)
   )));
}

template<class T>
const T &max_value(const T &a, const T &b)
{  return a > b ? a : b;   }

template<class T>
const T &min_value(const T &a, const T &b)
{  return a < b ? a : b;   }

template <class SizeType>
SizeType
   get_next_capacity(const SizeType max_size
                    ,const SizeType capacity
                    ,const SizeType n)
{
//   if (n > max_size - capacity)
//      throw std::length_error("get_next_capacity");

   const SizeType m3 = max_size/3;

   if (capacity < m3)
      return capacity + max_value(3*(capacity+1)/5, n);

   if (capacity < m3*2)
      return capacity + max_value((capacity+1)/2, n);

   return max_size;
}

template <class T>
inline T* to_raw_pointer(T* p)
{  return p; }

template <class Pointer>
inline typename Pointer::element_type*
   to_raw_pointer(const Pointer &p)
{  return boost::container::container_detail::to_raw_pointer(p.operator->());  }

//!To avoid ADL problems with swap
template <class T>
inline void do_swap(T& x, T& y)
{
   using std::swap;
   swap(x, y);
}

template<class AllocatorType>
inline void swap_alloc(AllocatorType &, AllocatorType &, container_detail::false_type)
   BOOST_CONTAINER_NOEXCEPT
{}

template<class AllocatorType>
inline void swap_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type)
{  container_detail::do_swap(l, r);   }

template<class AllocatorType>
inline void assign_alloc(AllocatorType &, const AllocatorType &, container_detail::false_type)
   BOOST_CONTAINER_NOEXCEPT
{}

template<class AllocatorType>
inline void assign_alloc(AllocatorType &l, const AllocatorType &r, container_detail::true_type)
{  l = r;   }

template<class AllocatorType>
inline void move_alloc(AllocatorType &, AllocatorType &, container_detail::false_type)
   BOOST_CONTAINER_NOEXCEPT
{}

template<class AllocatorType>
inline void move_alloc(AllocatorType &l, AllocatorType &r, container_detail::true_type)
{  l = ::boost::move(r);   }

//Rounds "orig_size" by excess to round_to bytes
template<class SizeType>
inline SizeType get_rounded_size(SizeType orig_size, SizeType round_to)
{
   return ((orig_size-1)/round_to+1)*round_to;
}

template <std::size_t OrigSize, std::size_t RoundTo>
struct ct_rounded_size
{
   enum { value = ((OrigSize-1)/RoundTo+1)*RoundTo };
};
/*
template <class _TypeT>
struct __rw_is_enum
{
   struct _C_no { };
   struct _C_yes { int _C_dummy [2]; };

   struct _C_indirect {
   // prevent classes with user-defined conversions from matching

   // use double to prevent float->int gcc conversion warnings
   _C_indirect (double);
};

// nested struct gets rid of bogus gcc errors
struct _C_nest {
   // supply first argument to prevent HP aCC warnings
   static _C_no _C_is (int, ...);
   static _C_yes _C_is (int, _C_indirect);

   static _TypeT _C_make_T ();
};

enum {
   _C_val = sizeof (_C_yes) == sizeof (_C_nest::_C_is (0, _C_nest::_C_make_T ()))
   && !::boost::is_fundamental<_TypeT>::value
};

};
*/

template<class T>
struct move_const_ref_type
   : if_c
//   < ::boost::is_fundamental<T>::value || ::boost::is_pointer<T>::value || ::boost::is_member_pointer<T>::value || ::boost::is_enum<T>::value
   < !::boost::is_class<T>::value
   ,const T &
   ,BOOST_CATCH_CONST_RLVALUE(T)
   >
{};

}  //namespace container_detail {

//////////////////////////////////////////////////////////////////////////////
//
//                               uninitialized_move_alloc
//
//////////////////////////////////////////////////////////////////////////////

//! <b>Effects</b>:
//!   \code
//!   for (; first != last; ++result, ++first)
//!      allocator_traits::construct(a, &*result, boost::move(*first));
//!   \endcode
//!
//! <b>Returns</b>: result
template
   <typename A,
    typename I, // I models InputIterator
    typename F> // F models ForwardIterator
F uninitialized_move_alloc(A &a, I f, I l, F r)
{
   while (f != l) {
      allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), boost::move(*f));
      ++f; ++r;
   }
   return r;
}

//////////////////////////////////////////////////////////////////////////////
//
//                               uninitialized_copy_alloc
//
//////////////////////////////////////////////////////////////////////////////

//! <b>Effects</b>:
//!   \code
//!   for (; first != last; ++result, ++first)
//!      allocator_traits::construct(a, &*result, *first);
//!   \endcode
//!
//! <b>Returns</b>: result
template
   <typename A,
    typename I, // I models InputIterator
    typename F> // F models ForwardIterator
F uninitialized_copy_alloc(A &a, I f, I l, F r)
{
   while (f != l) {
      allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*r), *f);
      ++f; ++r;
   }
   return r;
}

//////////////////////////////////////////////////////////////////////////////
//
//                               uninitialized_copy_alloc
//
//////////////////////////////////////////////////////////////////////////////

//! <b>Effects</b>:
//!   \code
//!   for (; first != last; ++result, ++first)
//!      allocator_traits::construct(a, &*result, *first);
//!   \endcode
//!
//! <b>Returns</b>: result
template
   <typename A,
    typename F, // F models ForwardIterator
    typename T>
void uninitialized_fill_alloc(A &a, F f, F l, const T &t)
{
   while (f != l) {
      allocator_traits<A>::construct(a, container_detail::to_raw_pointer(&*f), t);
      ++f;
   }
}

//////////////////////////////////////////////////////////////////////////////
//
//                            uninitialized_copy_or_move_alloc
//
//////////////////////////////////////////////////////////////////////////////

template
<typename A
,typename I    // I models InputIterator
,typename F>   // F models ForwardIterator
F uninitialized_copy_or_move_alloc
   (A &a, I f, I l, F r
   ,typename boost::container::container_detail::enable_if
      < boost::move_detail::is_move_iterator<I> >::type* = 0)
{
   return ::boost::container::uninitialized_move_alloc(a, f, l, r);
}

template
<typename A
,typename I    // I models InputIterator
,typename F>   // F models ForwardIterator
F uninitialized_copy_or_move_alloc
   (A &a, I f, I l, F r
   ,typename boost::container::container_detail::disable_if
      < boost::move_detail::is_move_iterator<I> >::type* = 0)
{
   return ::boost::container::uninitialized_copy_alloc(a, f, l, r);
}


}  //namespace container {
}  //namespace boost {


#include <boost/container/detail/config_end.hpp>

#endif   //#ifndef BOOST_CONTAINER_DETAIL_UTILITIES_HPP