summaryrefslogtreecommitdiffstats
path: root/src/include/uses_allocator.h
blob: 35cdbd7093bbc7c5b14eca4410e8f0438b23d684 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

// Derived from:
/* uses_allocator.h                  -*-C++-*-
 *
 * Copyright (C) 2016 Pablo Halpern <phalpern@halpernwightsoftware.com>
 * Distributed under the Boost Software License - Version 1.0
 */
// Downloaded from https://github.com/phalpern/uses-allocator.git

#pragma once

#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>

namespace ceph {

namespace internal {
template <class T, class Tuple, std::size_t... Indexes>
T make_from_tuple_imp(Tuple&& t, std::index_sequence<Indexes...>)
{
  return T(std::get<Indexes>(std::forward<Tuple>(t))...);
}
} // namespace internal

template<class T, class Tuple>
T make_from_tuple(Tuple&& args_tuple)
{
    using namespace internal;
    using Indices = std::make_index_sequence<std::tuple_size_v<
                                               std::decay_t<Tuple>>>;
    return make_from_tuple_imp<T>(std::forward<Tuple>(args_tuple), Indices{});
}

////////////////////////////////////////////////////////////////////////

// Forward declaration
template <class T, class Alloc, class... Args>
auto uses_allocator_construction_args(const Alloc& a, Args&&... args);

namespace internal {

template <class T, class A>
struct has_allocator : std::uses_allocator<T, A> { };

// Specialization of `has_allocator` for `std::pair`
template <class T1, class T2, class A>
struct has_allocator<std::pair<T1, T2>, A>
  : std::integral_constant<bool, has_allocator<T1, A>::value ||
                                 has_allocator<T2, A>::value>
{
};

template <bool V> using boolean_constant = std::integral_constant<bool, V>;

template <class T> struct is_pair : std::false_type { };

template <class T1, class T2>
struct is_pair<std::pair<T1, T2>> : std::true_type { };

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload is handles types for which `has_allocator<T, Alloc>` is false.
template <class T, class Unused1, class Unused2, class Alloc, class... Args>
auto uses_allocator_args_imp(Unused1      /* is_pair */,
                             std::false_type   /* has_allocator */,
                             Unused2      /* uses prefix allocator arg */,
                             const Alloc& /* ignored */,
                             Args&&... args)
{
    // Allocator is ignored
    return std::forward_as_tuple(std::forward<Args>(args)...);
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles non-pair `T` for which `has_allocator<T, Alloc>` is
// true and constructor `T(allocator_arg_t, a, args...)` is valid.
template <class T, class Alloc, class... Args>
auto uses_allocator_args_imp(std::false_type /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::true_type  /* uses prefix allocator arg */,
                             const Alloc& a,
                             Args&&... args)
{
    // Allocator added to front of argument list, after `allocator_arg`.
  return std::tuple<std::allocator_arg_t, const Alloc&,
                    Args&&...>(std::allocator_arg, a, std::forward<Args>(args)...);
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles non-pair `T` for which `has_allocator<T, Alloc>` is
// true and constructor `T(allocator_arg_t, a, args...)` NOT valid.
// This function will produce invalid results unless `T(args..., a)` is valid.
template <class T1, class Alloc, class... Args>
auto uses_allocator_args_imp(std::false_type /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a,
                             Args&&... args)
{
    // Allocator added to end of argument list
    return std::forward_as_tuple(std::forward<Args>(args)..., a);
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// piecewise_construct arguments are passed in.
template <class T, class Alloc, class Tuple1, class Tuple2>
auto uses_allocator_args_imp(std::true_type  /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a,
                             std::piecewise_construct_t,
                             Tuple1&& x, Tuple2&& y)
{
    using T1 = typename T::first_type;
    using T2 = typename T::second_type;

    return std::make_tuple(
      std::piecewise_construct,
      std::apply([&a](auto&&... args1) -> auto {
                   return uses_allocator_construction_args<T1>(
                     a, std::forward<decltype(args1)>(args1)...);
                 }, std::forward<Tuple1>(x)),
      std::apply([&a](auto&&... args2) -> auto {
                   return uses_allocator_construction_args<T2>(
                     a, std::forward<decltype(args2)>(args2)...);
                 }, std::forward<Tuple2>(y))
      );
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// no other constructor arguments are passed in.
template <class T, class Alloc>
auto uses_allocator_args_imp(std::true_type  /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a)
{
    // using T1 = typename T::first_type;
    // using T2 = typename T::second_type;

    // return std::make_tuple(
    //     piecewise_construct,
    //     uses_allocator_construction_args<T1>(a),
    //     uses_allocator_construction_args<T2>(a));
  return uses_allocator_construction_args<T>(a, std::piecewise_construct,
                                             std::tuple<>{}, std::tuple<>{});
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// a single argument of type const-lvalue-of-pair is passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(std::true_type  /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a,
                             const std::pair<U1, U2>& arg)
{
    // using T1 = typename T::first_type;
    // using T2 = typename T::second_type;

    // return std::make_tuple(
    //     piecewise_construct,
    //     uses_allocator_construction_args<T1>(a, arg.first),
    //     uses_allocator_construction_args<T2>(a, arg.second));
  return uses_allocator_construction_args<T>(a, std::piecewise_construct,
                                               std::forward_as_tuple(arg.first),
                                             std::forward_as_tuple(arg.second));
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// a single argument of type rvalue-of-pair is passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(std::true_type  /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a,
                             std::pair<U1, U2>&& arg)
{
    // using T1 = typename T::first_type;
    // using T2 = typename T::second_type;

    // return std::make_tuple(
    //     piecewise_construct,
    //     uses_allocator_construction_args<T1>(a, forward<U1>(arg.first)),
    //     uses_allocator_construction_args<T2>(a, forward<U2>(arg.second)));
  return uses_allocator_construction_args<T>(a, std::piecewise_construct,
                                             std::forward_as_tuple(std::forward<U1>(arg.first)),
                                             std::forward_as_tuple(std::forward<U2>(arg.second)));
}

// Return a tuple of arguments appropriate for uses-allocator construction
// with allocator `Alloc` and ctor arguments `Args`.
// This overload handles specializations of `T` = `std::pair` for which
// `has_allocator<T, Alloc>` is true for either or both of the elements and
// two additional constructor arguments are passed in.
template <class T, class Alloc, class U1, class U2>
auto uses_allocator_args_imp(std::true_type  /* is_pair */,
                             std::true_type  /* has_allocator */,
                             std::false_type /* prefix allocator arg */,
                             const Alloc& a,
                             U1&& arg1, U2&& arg2)
{
    // using T1 = typename T::first_type;
    // using T2 = typename T::second_type;

    // return std::make_tuple(
    //     piecewise_construct,
    //     uses_allocator_construction_args<T1>(a, forward<U1>(arg1)),
    //     uses_allocator_construction_args<T2>(a, forward<U2>(arg2)));
  return uses_allocator_construction_args<T>(
    a, std::piecewise_construct,
    std::forward_as_tuple(std::forward<U1>(arg1)),
    std::forward_as_tuple(std::forward<U2>(arg2)));
}

} // close namespace internal

template <class T, class Alloc, class... Args>
auto uses_allocator_construction_args(const Alloc& a, Args&&... args)
{
    using namespace internal;
    return uses_allocator_args_imp<T>(is_pair<T>(),
                                      has_allocator<T, Alloc>(),
                                      std::is_constructible<T, std::allocator_arg_t,
                                                            Alloc, Args...>(),
                                      a, std::forward<Args>(args)...);
}

template <class T, class Alloc, class... Args>
T make_obj_using_allocator(const Alloc& a, Args&&... args)
{
  return make_from_tuple<T>(
    uses_allocator_construction_args<T>(a, std::forward<Args>(args)...));
}

template <class T, class Alloc, class... Args>
T* uninitialized_construct_using_allocator(T* p,
                                           const Alloc& a,
                                           Args&&... args)
{
  return std::apply([p](auto&&... args2){
                      return ::new(static_cast<void*>(p))
                        T(std::forward<decltype(args2)>(args2)...);
                    }, uses_allocator_construction_args<T>(
		      a, std::forward<Args>(args)...));
}

} // namespace ceph