summaryrefslogtreecommitdiff
path: root/boost/beast/core/detail/type_traits.hpp
blob: 3d379846b9320aea385206780b5e49d479f6571f (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//
// Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
//
// 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)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_DETAIL_TYPE_TRAITS_HPP
#define BOOST_BEAST_DETAIL_TYPE_TRAITS_HPP

#include <boost/beast/core/error.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/type_traits.hpp>
#include <iterator>
#include <tuple>
#include <type_traits>
#include <string>
#include <utility>

namespace boost {
namespace beast {
namespace detail {

template<class U>
inline
std::size_t constexpr
max_sizeof()
{
    return sizeof(U);
}

template<class U0, class U1, class... Us>
inline
std::size_t constexpr
max_sizeof()
{
    return
        max_sizeof<U0>() > max_sizeof<U1, Us...>() ?
        max_sizeof<U0>() : max_sizeof<U1, Us...>();
}

template<class U>
inline
std::size_t constexpr
max_alignof()
{
    return alignof(U);
}

template<class U0, class U1, class... Us>
std::size_t constexpr
max_alignof()
{
    return
        max_alignof<U0>() > max_alignof<U1, Us...>() ?
        max_alignof<U0>() : max_alignof<U1, Us...>();
}

// (since C++17)
template<class... Ts>
using make_void = boost::make_void<Ts...>;
template<class... Ts>
using void_t = boost::void_t<Ts...>;

// (since C++11) missing from g++4.8
template<std::size_t Len, class... Ts>
struct aligned_union
{
    static
    std::size_t constexpr alignment_value =
        max_alignof<Ts...>();

    using type = typename std::aligned_storage<
        (Len > max_sizeof<Ts...>()) ? Len : (max_sizeof<Ts...>()),
            alignment_value>::type;
};

template<std::size_t Len, class... Ts>
using aligned_union_t =
    typename aligned_union<Len, Ts...>::type;

//------------------------------------------------------------------------------

template<class T>
inline
void
accept_rv(T){}

//------------------------------------------------------------------------------

template<unsigned N, class T, class... Tn>
struct repeat_tuple_impl
{
    using type = typename repeat_tuple_impl<
        N - 1, T, T, Tn...>::type;
};

template<class T, class... Tn>
struct repeat_tuple_impl<0, T, Tn...>
{
    using type = std::tuple<T, Tn...>;
};

template<unsigned N, class T>
struct repeat_tuple
{
    using type =
        typename repeat_tuple_impl<N-1, T>::type;
};

template<class T>
struct repeat_tuple<0, T>
{
    using type = std::tuple<>;
};

//------------------------------------------------------------------------------

template<class R, class C, class ...A>
auto
is_invocable_test(C&& c, int, A&& ...a)
    -> decltype(std::is_convertible<
        decltype(c(std::forward<A>(a)...)), R>::value ||
            std::is_same<R, void>::value,
                std::true_type());

template<class R, class C, class ...A>
std::false_type
is_invocable_test(C&& c, long, A&& ...a);

/** Metafunction returns `true` if F callable as R(A...)

    Example:

    @code
        is_invocable<T, void(std::string)>
    @endcode
*/
/** @{ */
template<class C, class F>
struct is_invocable : std::false_type
{
};

template<class C, class R, class ...A>
struct is_invocable<C, R(A...)>
    : decltype(is_invocable_test<R>(
        std::declval<C>(), 1, std::declval<A>()...))
{
};
/** @} */

//------------------------------------------------------------------------------

// for span
template<class T, class E, class = void>
struct is_contiguous_container: std::false_type {};

template<class T, class E>
struct is_contiguous_container<T, E, void_t<
    decltype(
        std::declval<std::size_t&>() = std::declval<T const&>().size(),
        std::declval<E*&>() = std::declval<T&>().data()),
    typename std::enable_if<
        std::is_same<
            typename std::remove_cv<E>::type,
            typename std::remove_cv<
                typename std::remove_pointer<
                    decltype(std::declval<T&>().data())
                >::type
            >::type
        >::value
    >::type>>: std::true_type
{};

//------------------------------------------------------------------------------

template<class...>
struct unwidest_unsigned;

template<class U0>
struct unwidest_unsigned<U0>
{
    using type = U0;
};

template<class U0, class... UN>
struct unwidest_unsigned<U0, UN...>
{
    BOOST_STATIC_ASSERT(std::is_unsigned<U0>::value);
    using type = typename std::conditional<
        (sizeof(U0) < sizeof(typename unwidest_unsigned<UN...>::type)),
        U0, typename unwidest_unsigned<UN...>::type>::type;
};

template<class...>
struct widest_unsigned;

template<class U0>
struct widest_unsigned<U0>
{
    using type = U0;
};

template<class U0, class... UN>
struct widest_unsigned<U0, UN...>
{
    BOOST_STATIC_ASSERT(std::is_unsigned<U0>::value);
    using type = typename std::conditional<
        (sizeof(U0) > sizeof(typename widest_unsigned<UN...>::type)),
        U0, typename widest_unsigned<UN...>::type>::type;
};

template<class U>
inline
constexpr
U
min_all(U u)
{
    BOOST_STATIC_ASSERT(std::is_unsigned<U>::value);
    return u;
}

template<class U0, class U1, class... UN>
inline
constexpr
typename unwidest_unsigned<U0, U1, UN...>::type
min_all(U0 u0, U1 u1, UN... un)
{
    using type =
        typename unwidest_unsigned<U0, U1, UN...>::type;
    return u0 < u1 ?
        static_cast<type>(min_all(u0, un...)) :
        static_cast<type>(min_all(u1, un...));
}

template<class U>
inline
constexpr
U
max_all(U u)
{
    BOOST_STATIC_ASSERT(std::is_unsigned<U>::value);
    return u;
}

template<class U0, class U1, class... UN>
inline
constexpr
typename widest_unsigned<U0, U1, UN...>::type
max_all(U0 u0, U1 u1, UN... un)
{
    return u0 > u1? max_all(u0, un...) : max_all(u1, un...);
}

//------------------------------------------------------------------------------

template<class T, class = void>
struct get_lowest_layer_helper
{
    using type = T;
};

template<class T>
struct get_lowest_layer_helper<T,
    void_t<typename T::lowest_layer_type>>
{
    using type = typename T::lowest_layer_type;
};

//------------------------------------------------------------------------------

//
// buffer concepts
//

// Types that meet the requirements,
// for use with std::declval only.
template<class BufferType>
struct BufferSequence
{
    using value_type = BufferType;
    using const_iterator = BufferType const*;
    ~BufferSequence();
    BufferSequence(BufferSequence const&) = default;
    const_iterator begin() const noexcept;
    const_iterator end() const noexcept;
};
using ConstBufferSequence =
    BufferSequence<boost::asio::const_buffer>;
using MutableBufferSequence =
    BufferSequence<boost::asio::mutable_buffer>;

template<class B1, class... Bn>
struct is_all_const_buffer_sequence
    : std::integral_constant<bool,
        boost::asio::is_const_buffer_sequence<B1>::value &&
        is_all_const_buffer_sequence<Bn...>::value>
{
};

template<class B>
struct is_all_const_buffer_sequence<B>
    : boost::asio::is_const_buffer_sequence<B>
{
};

template<class... Bn>
struct common_buffers_type
{
    using type = typename std::conditional<
        boost::is_convertible<std::tuple<Bn...>,
            typename repeat_tuple<sizeof...(Bn),
                boost::asio::mutable_buffer>::type>::value,
                    boost::asio::mutable_buffer,
                        boost::asio::const_buffer>::type;
};

template<class B>
struct buffer_sequence_iterator
{
    using type = decltype(
        boost::asio::buffer_sequence_begin(
            std::declval<B const&>()));
};

// Types that meet the requirements,
// for use with std::declval only.
struct StreamHandler
{
    StreamHandler(StreamHandler const&) = default;
    void operator()(error_code ec, std::size_t);
};
using ReadHandler = StreamHandler;
using WriteHandler = StreamHandler;

template<class Buffers>
class buffers_range_adaptor
{
    Buffers const& b_;

public:
    using value_type = typename std::conditional<
        boost::is_convertible<
            typename std::iterator_traits<
                typename buffer_sequence_iterator<
                    Buffers>::type>::value_type,
                boost::asio::mutable_buffer>::value,
            boost::asio::mutable_buffer,
            boost::asio::const_buffer>::type;

    class const_iterator
    {
        friend class buffers_range_adaptor;

        using iter_type = typename
            buffer_sequence_iterator<Buffers>::type;

        iter_type it_;

        const_iterator(iter_type const& it)
            : it_(it)
        {
        }

    public:
        using value_type = typename
            buffers_range_adaptor::value_type;
        using pointer = value_type const*;
        using reference = value_type;
        using difference_type = std::ptrdiff_t;
        using iterator_category =
            std::bidirectional_iterator_tag;
        
        bool
        operator==(const_iterator const& other) const
        {
            return it_ == other.it_;
        }

        bool
        operator!=(const_iterator const& other) const
        {
            return ! (*this == other);
        }

        reference
        operator*() const
        {
            return *it_;
        }

        pointer
        operator->() const = delete;

        const_iterator&
        operator++()
        {
            ++it_;
            return *this;
        }

        const_iterator
        operator++(int)
        {
            auto temp = *this;
            ++(*this);
            return temp;
        }

        // deprecated
        const_iterator&
        operator--()
        {
            --it_;
            return *this;
        }

        // deprecated
        const_iterator
        operator--(int)
        {
            auto temp = *this;
            --(*this);
            return temp;
        }
    };

    explicit
    buffers_range_adaptor(Buffers const& b)
        : b_(b)
    {
    }

    const_iterator
    begin() const noexcept
    {
        return boost::asio::buffer_sequence_begin(b_);
    }

    const_iterator
    end() const noexcept
    {
        return boost::asio::buffer_sequence_end(b_);
    }
};

template<class Buffers>
buffers_range_adaptor<Buffers>
buffers_range(Buffers const& buffers)
{
    return buffers_range_adaptor<Buffers>{buffers};
}

/*  If this static assert goes off, it means that the completion
    handler you provided to an asynchronous initiating function did
    not have the right signature. Check the parameter types for your
    completion handler and make sure they match the list of types
    expected by the initiating function,
*/
#define BOOST_BEAST_HANDLER_INIT(type, sig) \
    static_assert(boost::beast::is_completion_handler< \
    BOOST_ASIO_HANDLER_TYPE(type, sig), sig>::value, \
    "CompletionHandler signature requirements not met"); \
    boost::asio::async_completion<type, sig> init{handler}

} // detail
} // beast
} // boost

#endif