File: TypeTraits.h

package info (click to toggle)
webkit2gtk 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 429,620 kB
  • sloc: cpp: 3,696,936; javascript: 194,444; ansic: 169,997; python: 46,499; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; sh: 2,098; java: 1,993; lex: 1,327; pascal: 366; makefile: 298
file content (196 lines) | stat: -rw-r--r-- 6,777 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2023 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <type_traits>
#include <wtf/Forward.h>

// SFINAE depends on overload resolution. We indicate the overload we'd prefer
// (if it can compile) using a higher priorty type (int), and the overload
// to fall back to using a lower priority type (long). 0 can convert to int
// or long, so we can trigger overload resolution using 0. C++ is awesome!
#define SFINAE_OVERLOAD 0
#define SFINAE_OVERLOAD_DEFAULT long
#define SFINAE_OVERLOAD_PREFERRED int

namespace WTF {

namespace detail {

// IsRefcountedSmartPointer implementation.
template<typename CVRemoved>
struct IsRefcountedSmartPointerHelper : std::false_type { };

template<typename Pointee>
struct IsRefcountedSmartPointerHelper<RefPtr<Pointee>> : std::true_type { };

template<typename Pointee>
struct IsRefcountedSmartPointerHelper<Ref<Pointee>> : std::true_type { };

} // namespace detail

template<typename T>
struct IsRefcountedSmartPointer : detail::IsRefcountedSmartPointerHelper<std::remove_cv_t<T>> { };

// IsSmartRef implementation
namespace detail {

template<typename CVRemoved>
struct IsSmartRefHelper : std::false_type { };

template<typename Pointee>
struct IsSmartRefHelper<Ref<Pointee>> : std::true_type { };

} // namespace detail

template<typename T>
struct IsSmartRef : detail::IsSmartRefHelper<std::remove_cv_t<T>> { };

// RemoveSmartPointer implementation
namespace detail {

template<typename T, typename CVRemoved>
struct RemoveSmartPointerHelper {
    typedef T type;
};

template<typename T, typename Pointee>
struct RemoveSmartPointerHelper<T, RefPtr<Pointee>> {
    typedef Pointee type;
};

template<typename T, typename Pointee>
struct RemoveSmartPointerHelper<T, Ref<Pointee>> {
    typedef Pointee type;
};

} // namespace detail

template<typename T>
struct RemoveSmartPointer : detail::RemoveSmartPointerHelper<T, std::remove_cv_t<T>> { };

// HasRefPtrMemberFunctions implementation
namespace detail {

template<typename>
struct SFINAE1True : std::true_type { };

template<class T>
static auto HasRefPtrMemberFunctionsTest(SFINAE_OVERLOAD_PREFERRED) -> SFINAE1True<decltype(static_cast<std::remove_cv_t<T>*>(nullptr)->ref(), static_cast<std::remove_cv_t<T>*>(nullptr)->deref())>;
template<class>
static auto HasRefPtrMemberFunctionsTest(SFINAE_OVERLOAD_DEFAULT) -> std::false_type;

} // namespace detail

template<class T>
struct HasRefPtrMemberFunctions : decltype(detail::HasRefPtrMemberFunctionsTest<T>(SFINAE_OVERLOAD)) { };

// HasCheckedPtrMemberFunctions implementation
namespace detail {

template<class T>
static auto HasCheckedPtrMemberFunctionsTest(SFINAE_OVERLOAD_PREFERRED) -> SFINAE1True<decltype(static_cast<std::remove_cv_t<T>*>(nullptr)->incrementCheckedPtrCount(), static_cast<std::remove_cv_t<T>*>(nullptr)->decrementCheckedPtrCount())>;
template<class>
static auto HasCheckedPtrMemberFunctionsTest(SFINAE_OVERLOAD_DEFAULT) -> std::false_type;

} // namespace detail

template<class T>
struct HasCheckedPtrMemberFunctions : decltype(detail::HasCheckedPtrMemberFunctionsTest<T>(SFINAE_OVERLOAD)) { };

// HasIsolatedCopy()
namespace detail {

// FIXME: This test is incorrectly false for RefCounted objects because
// substitution for std::declval<T>() fails when the constructor is private.
template<class T>
static auto HasIsolatedCopyTest(SFINAE_OVERLOAD_PREFERRED) -> SFINAE1True<decltype(std::declval<T>().isolatedCopy())>;
template<class>
static auto HasIsolatedCopyTest(SFINAE_OVERLOAD_DEFAULT) -> std::false_type;

} // namespace detail

template<class T>
struct HasIsolatedCopy : decltype(detail::HasIsolatedCopyTest<T>(SFINAE_OVERLOAD)) { };

// LooksLikeRCSerialDispatcher implementation
namespace detail {

template <bool b, typename>
struct SFINAE1If : std::integral_constant<bool, b> { };

template <bool b, class T>
static auto LooksLikeRCSerialDispatcherTest(SFINAE_OVERLOAD_PREFERRED)
    -> SFINAE1If<b, decltype(std::declval<T>().ref(), std::declval<T>().deref())>;

template <bool, typename>
static auto LooksLikeRCSerialDispatcherTest(SFINAE_OVERLOAD_DEFAULT) -> std::false_type;

} // namespace detail

template <class T>
struct LooksLikeRCSerialDispatcher : decltype(detail::LooksLikeRCSerialDispatcherTest<std::is_base_of_v<SerialFunctionDispatcher, T>, T>(SFINAE_OVERLOAD)) { };

class NativePromiseBase;
class ConvertibleToNativePromise;

template <typename T>
concept IsNativePromise = std::is_base_of<NativePromiseBase, T>::value;

template <typename T>
concept IsConvertibleToNativePromise = std::is_base_of<ConvertibleToNativePromise, T>::value;

template <typename T, typename U>
concept RelatedNativePromise = requires(T, U)
{
    { IsConvertibleToNativePromise<T> };
    { IsConvertibleToNativePromise<U> };
    { std::is_same<typename T::PromiseType, typename U::PromiseType>::value };
};

template <typename T>
struct IsExpected : std::false_type { };

template <typename T, typename E>
struct IsExpected<Expected<T, E>> : std::true_type { };

template <typename... Args>
struct ParameterCountImpl {
    static constexpr std::size_t value = sizeof...(Args);
};

template <typename ReturnType, typename... Args>
constexpr std::size_t parameterCount(ReturnType(*)(Args...))
{
    return ParameterCountImpl<Args...>::value;
}

} // namespace NTF