summaryrefslogtreecommitdiffstats
path: root/src/corelib/global/qxpfunctional.h
blob: 67350c56ed7ddb4b0499d978256253042aed9146 (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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QXPFUNCTIONAL_H
#define QXPFUNCTIONAL_H

#include <QtCore/qglobal.h>

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API. Types and functions defined
// in this file will behave exactly as their std counterparts. You
// may use these definitions in your own code, but be aware that we
// will remove them once Qt depends on the C++ version that supports
// them in namespace std. There will be NO deprecation warning, the
// definitions will JUST go away.
//
// If you can't agree to these terms, don't use these definitions!
//
// We mean it.
//

#include <QtCore/q23functional.h>
#include <type_traits>
#include <utility>

QT_BEGIN_NAMESPACE

namespace qxp {
// like P0792r9's function_ref:

// [func.wrap.ref], non-owning wrapper
template<class... S> class function_ref;              // not defined

// template<class R, class... ArgTypes>
// class function_ref<R(ArgTypes...) cv noexcept(noex)>; // see below
//
// [func.wrap.ref.general]
// The header provides partial specializations of function_ref for each combination
// of the possible replacements of the placeholders cv and noex where:
// - cv is either const or empty.
// - noex is either true or false.

namespace detail {

template <typename T>
using if_function = std::enable_if_t<std::is_function_v<T>, bool>;
template <typename T>
using if_non_function = std::enable_if_t<!std::is_function_v<T>, bool>;

template <typename From, typename To>
using copy_const_t = std::conditional_t<
        std::is_const_v<From>,
        std::add_const_t<To>,
        To
    >;

template <class Const>
union BoundEntityType {
    template <typename F, if_function<F> = true>
    explicit constexpr BoundEntityType(F *f)
        : fun(reinterpret_cast<QFunctionPointer>(f)) {}
    template <typename T, if_non_function<T> = true>
    explicit constexpr BoundEntityType(T *t)
        : obj(static_cast<Const*>(t)) {}
    Const *obj;
    QFunctionPointer fun;
};

template <bool noex, class Const, class R, class... ArgTypes>
class function_ref_base
{
protected:
    ~function_ref_base() = default;

    using BoundEntityType = detail::BoundEntityType<Const>;

    template <typename... Ts>
    using is_invocable_using = std::conditional_t<
            noex,
            std::is_nothrow_invocable_r<R, Ts..., ArgTypes...>,
            std::is_invocable_r<R, Ts..., ArgTypes...>
        >;

    using ThunkPtr = R(*)(BoundEntityType, ArgTypes&&...) noexcept(noex);

    BoundEntityType m_bound_entity;
    ThunkPtr m_thunk_ptr;

public:
    template<
        class F,
        std::enable_if_t<std::conjunction_v<
            std::is_function<F>,
            is_invocable_using<F>
        >, bool> = true
    >
    Q_IMPLICIT function_ref_base(F* f) noexcept
        : m_bound_entity(f),
          m_thunk_ptr([](BoundEntityType ctx, ArgTypes&&... args) noexcept(noex) -> R {
                return q23::invoke_r<R>(reinterpret_cast<F*>(ctx.fun),
                                        std::forward<ArgTypes>(args)...);
            })
    {}

    template<
        class F,
        std::enable_if_t<std::conjunction_v<
            std::negation<std::is_same<q20::remove_cvref_t<F>, function_ref_base>>,
            std::negation<std::is_member_pointer<std::remove_reference_t<F>>>,
            is_invocable_using<copy_const_t<Const, std::remove_reference_t<F>>&>
        >, bool> = true
    >
    Q_IMPLICIT constexpr function_ref_base(F&& f) noexcept
        : m_bound_entity(std::addressof(f)),
          m_thunk_ptr([](BoundEntityType ctx, ArgTypes&&... args) noexcept(noex) -> R {
                using That = copy_const_t<Const, std::remove_reference_t<F>>;
                return q23::invoke_r<R>(*static_cast<That*>(ctx.obj),
                                        std::forward<ArgTypes>(args)...);
            })
    {}

protected:
    template <
        class T,
        std::enable_if_t<std::conjunction_v<
            std::negation<std::is_same<q20::remove_cvref_t<T>, function_ref_base>>,
            std::negation<std::is_pointer<T>>
        >, bool> = true
    >
    function_ref_base& operator=(T) = delete;

    // Invocation [func.wrap.ref.inv]
    R operator()(ArgTypes... args) const noexcept(noex)
    {
        return m_thunk_ptr(m_bound_entity, std::forward<ArgTypes>(args)...);
    }

};

} // namespace detail

#define QT_SPECIALIZE_FUNCTION_REF(cv, noex) \
    template<class R, class... ArgTypes> \
    class function_ref<R(ArgTypes...) cv noexcept( noex )> \
        : private detail::function_ref_base< noex , cv void, R, ArgTypes...> \
    { \
        using base = detail::function_ref_base< noex , cv void, R, ArgTypes...>; \
        \
    public: \
        using base::base; \
        using base::operator(); \
    } \
    /* end */

QT_SPECIALIZE_FUNCTION_REF(     , false);
QT_SPECIALIZE_FUNCTION_REF(const, false);
QT_SPECIALIZE_FUNCTION_REF(     , true );
QT_SPECIALIZE_FUNCTION_REF(const, true );

#undef QT_SPECIALIZE_FUNCTION_REF

// deduction guides [func.wrap.ref.deduct]

template <
    class F,
    std::enable_if_t<std::is_function_v<F>, bool> = true
>
function_ref(F*) -> function_ref<F>;

} // namespace qxp

QT_END_NAMESPACE

#endif /* QXPFUNCTIONAL_H */