summaryrefslogtreecommitdiff
path: root/src/lib/corelib/tools/propagate_const.h
blob: 65e35fc8cff3e4338b194ed5f423a7d88fcbafcf (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
/****************************************************************************
**                                MIT License
**
** Copyright (C) 2022-2023 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
** Author: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
**
** This file is part of KDToolBox (https://github.com/KDAB/KDToolBox).
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, ** and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice (including the next paragraph)
** shall be included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF ** CONTRACT, TORT OR OTHERWISE,
** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
** DEALINGS IN THE SOFTWARE.
****************************************************************************/

#ifndef KDTOOLBOX_PROPAGATE_CONST
#define KDTOOLBOX_PROPAGATE_CONST

#include <functional>
#include <type_traits>
#include <utility>

// Our SFINAE is too noisy and clang-format gets confused.
// clang-format off

namespace KDToolBox
{

template <typename T>
class propagate_const;

namespace detail {
// Type traits to detect propagate_const specializations
template <typename T>
struct is_propagate_const : std::false_type
{
};

template <typename T>
struct is_propagate_const<propagate_const<T>> : std::true_type
{
};

// Using SFINAE in a base class to constrain the conversion operators.
// Otherwise, if we make them function templates and apply SFINAE directly on
// them, we would not be able to do certain conversions (cf.
// [over.ics.user/3]).
template <typename T>
using propagate_const_element_type = std::remove_reference_t<decltype(*std::declval<T &>())>;

// Const conversion.
// NON-STANDARD: checks that `const T` is convertible, not `T`.
// See https://wg21.link/lwg3812
template <typename T,
         bool = std::disjunction_v<
             std::is_pointer<T>,
             std::is_convertible<const T, const propagate_const_element_type<T> *>
             >
         >
struct propagate_const_const_conversion_operator_base
{
};

template <typename T>
struct propagate_const_const_conversion_operator_base<T, true>
{
    constexpr operator const propagate_const_element_type<T> *() const;
};

// Non-const conversion
template <typename T,
         bool = std::disjunction_v<
             std::is_pointer<T>,
             std::is_convertible<T, propagate_const_element_type<T> *>
             >
         >
struct propagate_const_non_const_conversion_operator_base
{
};

template <typename T>
struct propagate_const_non_const_conversion_operator_base<T, true>
{
    constexpr operator propagate_const_element_type<T> *();
};

template <typename T>
struct propagate_const_base
    : propagate_const_const_conversion_operator_base<T>,
      propagate_const_non_const_conversion_operator_base<T>
{};

} // namespace detail

/*
    TODO: This code could benefit from a C++20 overhaul:
    * concepts
    * three-way comparisons
    * explicit(bool)

    However we can't depend on C++20 yet...
*/

template <typename T>
class propagate_const : public detail::propagate_const_base<T>
{
public:
    using element_type = detail::propagate_const_element_type<T>;

    // Special member functions
    propagate_const() = default;
    propagate_const(propagate_const &&) = default;
    propagate_const &operator=(propagate_const &&) = default;
    propagate_const(const propagate_const &) = delete;
    propagate_const &operator=(const propagate_const &) = delete;
    ~propagate_const() = default;

    // Constructor from values

    template <
        typename U,
        std::enable_if_t< // This constructor is enabled if:
            std::conjunction_v<
                std::is_constructible<T, U>, // 1) we can build a T from a U,
                std::negation<detail::is_propagate_const<std::decay_t<U>>>, // 2) we are not making a
                // converting constructor,
                std::is_convertible<U, T> // 3) and the conversion from U to T is implicit;
                >,
            bool> = true>
    /* implicit */ // then, this constructor is implicit too.
    propagate_const(U &&other)
        : m_t(std::forward<U>(other))
    {
    }

    template <
        typename U,
        std::enable_if_t< // This constructor is enabled if:
            std::conjunction_v<
                std::is_constructible<T, U>, // 1) we can build a T from a U,
                std::negation<detail::is_propagate_const<std::decay_t<U>>>, // 2) we are not making a
                // converting constructor,
                std::negation<std::is_convertible<U, T>> // 3) and the conversion from U to T is
                // explicit;
                >,
            bool> = true>
    explicit // then, this constructor is explicit.
        propagate_const(U &&other)
        : m_t(std::forward<U>(other))
    {
    }

    // Constructors from other propagate_const (converting constructors)
    template <typename U,
             std::enable_if_t< // This constructor is enabled if:
                 std::conjunction_v<std::is_constructible<T, U>, // 1) we can do the conversion,
                                    std::is_convertible<U, T> // 2) and the conversion is implicit;
                                    >,
                 bool> = true>
    /* implicit */ // then, this constructor is implicit.
    constexpr propagate_const(propagate_const<U> &&other)
        : m_t(std::move(get_underlying(other)))
    {
    }

    template <typename U,
             std::enable_if_t< // This constructor is enabled if:
                 std::conjunction_v<std::is_constructible<T, U>, // 1) we can do the conversion,
                                    std::negation<std::is_convertible<U, T>> // 2) and the
                                    // conversion is
                                    // explicit;
                                    >,
                 bool> = true>
    explicit // then, this constructor is explicit.
        constexpr propagate_const(propagate_const<U> &&other)
        : m_t(std::move(get_underlying(other)))
    {
    }

    // Converting assignment
    template <typename U,
             std::enable_if_t< // This assignment operator is enabled if
                 std::is_convertible_v<U, T>, // the conversion from U to T is implicit
                 bool> = true>
    constexpr propagate_const &operator=(propagate_const<U> &&other)
    {
        m_t = std::move(get_underlying(other));
        return *this;
    }

    template <typename U,
             std::enable_if_t< // This assignment operator is enabled if:
                 std::conjunction_v<
                     std::is_convertible<U, T>, // 1) the conversion from U to T is implicit,
                     std::negation<detail::is_propagate_const<std::decay_t<U>>> // 2) and U is not a
                     // propagate_const
                     >,
                 bool> = true>
    constexpr propagate_const &operator=(U &&other)
    {
        m_t = std::forward<U>(other);
        return *this;
    }

    // Swap
    constexpr void swap(propagate_const &other) noexcept(std::is_nothrow_swappable_v<T>)
    {
        using std::swap;
        swap(m_t, other.m_t);
    }

    // Const observers
    constexpr explicit operator bool() const { return static_cast<bool>(m_t); }

    constexpr const element_type *get() const { return get_impl(m_t); }

    constexpr const element_type &operator*() const { return *get(); }

    constexpr const element_type *operator->() const { return get(); }

    // Non-const observers
    constexpr element_type *get() { return get_impl(m_t); }

    constexpr element_type &operator*() { return *get(); }

    constexpr element_type *operator->() { return get(); }

    // Non-member utilities: extract the contained object
    template <typename U>
    friend constexpr auto &get_underlying(propagate_const<U> &p);

    template <typename U>
    friend constexpr const auto &get_underlying(const propagate_const<U> &p);

private:
    // Implementation of get() that works with raw pointers and smart
    // pointers. Similar to std::to_address, but to_address is C++20,
    // and propagate_const spec does not match it.
    template <typename U>
    static constexpr element_type *get_impl(U *u)
    {
        return u;
    }

    template <typename U>
    static constexpr element_type *get_impl(U &u)
    {
        return u.get();
    }

    template <typename U>
    static constexpr const element_type *get_impl(const U *u)
    {
        return u;
    }

    template <typename U>
    static constexpr const element_type *get_impl(const U &u)
    {
        return u.get();
    }

    T m_t;
};

// Swap
template <typename T,
         std::enable_if_t<std::is_swappable_v<T>, bool> = true>
constexpr void swap(propagate_const<T> &lhs, propagate_const<T> &rhs)
    noexcept(noexcept(lhs.swap(rhs)))
{
    lhs.swap(rhs);
}

// Implementation of get_underlying
template <typename T>
constexpr auto &get_underlying(propagate_const<T> &p)
{
    return p.m_t;
}

template <typename T>
constexpr const auto &get_underlying(const propagate_const<T> &p)
{
    return p.m_t;
}

// Implementation of the conversion operators
template <typename T>
constexpr detail::propagate_const_const_conversion_operator_base<T, true>
    ::operator const detail::propagate_const_element_type<T> *() const
{
    return static_cast<const propagate_const<T> *>(this)->get();
}

template <typename T>
constexpr detail::propagate_const_non_const_conversion_operator_base<T, true>
    ::operator detail::propagate_const_element_type<T> *()
{
    return static_cast<propagate_const<T> *>(this)->get();
}

// Comparisons. As per spec, they're free function templates.

// Comparisons against nullptr.
template <typename T>
constexpr bool operator==(const propagate_const<T> &p, std::nullptr_t)
{
    return get_underlying(p) == nullptr;
}

template <typename T>
constexpr bool operator!=(const propagate_const<T> &p, std::nullptr_t)
{
    return get_underlying(p) != nullptr;
}

template <typename T>
constexpr bool operator==(std::nullptr_t, const propagate_const<T> &p)
{
    return nullptr == get_underlying(p);
}

template <typename T>
constexpr bool operator!=(std::nullptr_t, const propagate_const<T> &p)
{
    return nullptr == get_underlying(p);
}

// Comparisons between propagate_const
#define DEFINE_PROPAGATE_CONST_COMPARISON_OP(op)                                              \
template <typename T, typename U>                                                         \
    constexpr bool operator op (const propagate_const<T> &lhs, const propagate_const<U> &rhs) \
{                                                                                         \
        return get_underlying(lhs) op get_underlying(rhs);                                    \
}                                                                                         \

    DEFINE_PROPAGATE_CONST_COMPARISON_OP(==)
    DEFINE_PROPAGATE_CONST_COMPARISON_OP(!=)
    DEFINE_PROPAGATE_CONST_COMPARISON_OP(<)
    DEFINE_PROPAGATE_CONST_COMPARISON_OP(<=)
    DEFINE_PROPAGATE_CONST_COMPARISON_OP(>)
    DEFINE_PROPAGATE_CONST_COMPARISON_OP(>=)

#undef DEFINE_PROPAGATE_CONST_COMPARISON_OP

// Comparisons against other (smart) pointers
#define DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(op)                       \
    template <typename T, typename U>                                        \
    constexpr bool operator op (const propagate_const<T> &lhs, const U &rhs) \
{                                                                        \
        return get_underlying(lhs) op rhs;                                   \
}                                                                        \
    template <typename T, typename U>                                        \
    constexpr bool operator op (const T &lhs, const propagate_const<U> &rhs) \
{                                                                        \
        return lhs op get_underlying(rhs);                                   \
}                                                                        \

    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(==)
    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(!=)
    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(<)
    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(<=)
    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(>)
    DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP(>=)

#undef DEFINE_PROPAGATE_CONST_MIXED_COMPARISON_OP

} // namespace KDToolBox

// std::hash specialization
namespace std
{
template <typename T>
struct hash<KDToolBox::propagate_const<T>>
{
    constexpr size_t operator()(const KDToolBox::propagate_const<T> &t) const
        noexcept(noexcept(hash<T>{}(get_underlying(t))))
    {
        return hash<T>{}(get_underlying(t));
    }
};

#define DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(COMP)     \
template <typename T>                                               \
    struct COMP<KDToolBox::propagate_const<T>>                          \
{                                                                   \
        constexpr bool operator()(const KDToolBox::propagate_const<T> &lhs, \
                   const KDToolBox::propagate_const<T> &rhs) \
    {                                                                   \
            return COMP<T>{}(get_underlying(lhs), get_underlying(rhs));     \
    }                                                                   \
};                                                                  \

    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(equal_to)
    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(not_equal_to)
    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(less)
    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(greater)
    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(less_equal)
    DEFINE_COMP_OBJECT_SPECIALIZATION_FOR_PROPAGATE_CONST(greater_equal)

#undef DEFINE_COMP_OBJECT_SPECIALIZATION

} // namespace std

#endif // KDTOOLBOX_PROPAGATE_CONST