about summary refs log tree commit diff
path: root/third_party/immer/immer/detail/type_traits.hpp
blob: afb2652c55a80efbcf0b549229fa905b4d4f6dd6 (plain) (blame)
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
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#pragma once

#include <algorithm>
#include <iterator>
#include <memory>
#include <type_traits>
#include <utility>

namespace immer {
namespace detail {

template <typename... Ts>
struct make_void
{
    using type = void;
};

template <typename... Ts>
using void_t = typename make_void<Ts...>::type;

template <typename T, typename = void>
struct is_dereferenceable : std::false_type
{};

template <typename T>
struct is_dereferenceable<T, void_t<decltype(*(std::declval<T&>()))>>
    : std::true_type
{};

template <typename T>
constexpr bool is_dereferenceable_v = is_dereferenceable<T>::value;

template <typename T, typename U = T, typename = void>
struct is_equality_comparable : std::false_type
{};

template <typename T, typename U>
struct is_equality_comparable<
    T,
    U,
    std::enable_if_t<std::is_same<bool,
                                  decltype(std::declval<T&>() ==
                                           std::declval<U&>())>::value>>
    : std::true_type
{};

template <typename T, typename U = T>
constexpr bool is_equality_comparable_v = is_equality_comparable<T, U>::value;

template <typename T, typename U = T, typename = void>
struct is_inequality_comparable : std::false_type
{};

template <typename T, typename U>
struct is_inequality_comparable<
    T,
    U,
    std::enable_if_t<std::is_same<bool,
                                  decltype(std::declval<T&>() !=
                                           std::declval<U&>())>::value>>
    : std::true_type
{};

template <typename T, typename U = T>
constexpr bool is_inequality_comparable_v =
    is_inequality_comparable<T, U>::value;

template <typename T, typename = void>
struct is_preincrementable : std::false_type
{};

template <typename T>
struct is_preincrementable<
    T,
    std::enable_if_t<std::is_same<T&, decltype(++(std::declval<T&>()))>::value>>
    : std::true_type
{};

template <typename T>
constexpr bool is_preincrementable_v = is_preincrementable<T>::value;

template <typename T, typename U = T, typename = void>
struct is_subtractable : std::false_type
{};

template <typename T, typename U>
struct is_subtractable<
    T,
    U,
    void_t<decltype(std::declval<T&>() - std::declval<U&>())>> : std::true_type
{};

template <typename T, typename U = T>
constexpr bool is_subtractable_v = is_subtractable<T, U>::value;

namespace swappable {

using std::swap;

template <typename T, typename U, typename = void>
struct with : std::false_type
{};

// Does not account for non-referenceable types
template <typename T, typename U>
struct with<T,
            U,
            void_t<decltype(swap(std::declval<T&>(), std::declval<U&>())),
                   decltype(swap(std::declval<U&>(), std::declval<T&>()))>>
    : std::true_type
{};

} // namespace swappable

template <typename T, typename U>
using is_swappable_with = swappable::with<T, U>;

template <typename T>
using is_swappable = is_swappable_with<T, T>;

template <typename T>
constexpr bool is_swappable_v = is_swappable_with<T&, T&>::value;

template <typename T, typename = void>
struct is_iterator : std::false_type
{};

// See http://en.cppreference.com/w/cpp/concept/Iterator
template <typename T>
struct is_iterator<
    T,
    void_t<
        std::enable_if_t<is_preincrementable_v<T> &&
                         is_dereferenceable_v<T>
                         // accounts for non-referenceable types
                         && std::is_copy_constructible<T>::value &&
                         std::is_copy_assignable<T>::value &&
                         std::is_destructible<T>::value && is_swappable_v<T>>,
        typename std::iterator_traits<T>::value_type,
        typename std::iterator_traits<T>::difference_type,
        typename std::iterator_traits<T>::reference,
        typename std::iterator_traits<T>::pointer,
        typename std::iterator_traits<T>::iterator_category>> : std::true_type
{};

template <typename T>
constexpr bool is_iterator_v = is_iterator<T>::value;

template <typename T, typename U, typename = void>
struct compatible_sentinel : std::false_type
{};

template <typename T, typename U>
struct compatible_sentinel<
    T,
    U,
    std::enable_if_t<is_iterator_v<T> && is_equality_comparable_v<T, U> &&
                     is_inequality_comparable_v<T, U>>> : std::true_type
{};

template <typename T, typename U>
constexpr bool compatible_sentinel_v = compatible_sentinel<T, U>::value;

template <typename T, typename = void>
struct is_forward_iterator : std::false_type
{};

template <typename T>
struct is_forward_iterator<
    T,
    std::enable_if_t<is_iterator_v<T> &&
                     std::is_base_of<std::forward_iterator_tag,
                                     typename std::iterator_traits<
                                         T>::iterator_category>::value>>
    : std::true_type
{};

template <typename T>
constexpr bool is_forward_iterator_v = is_forward_iterator<T>::value;

template <typename T, typename U, typename = void>
struct std_distance_supports : std::false_type
{};

template <typename T, typename U>
struct std_distance_supports<
    T,
    U,
    void_t<decltype(std::distance(std::declval<T>(), std::declval<U>()))>>
    : std::true_type
{};

template <typename T, typename U>
constexpr bool std_distance_supports_v = std_distance_supports<T, U>::value;

template <typename T, typename U, typename V, typename = void>
struct std_uninitialized_copy_supports : std::false_type
{};

template <typename T, typename U, typename V>
struct std_uninitialized_copy_supports<
    T,
    U,
    V,
    void_t<decltype(std::uninitialized_copy(
        std::declval<T>(), std::declval<U>(), std::declval<V>()))>>
    : std::true_type
{};

template <typename T, typename U, typename V>
constexpr bool std_uninitialized_copy_supports_v =
    std_uninitialized_copy_supports<T, U, V>::value;

} // namespace detail
} // namespace immer