1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2013 Olivier Goffart <ogoffart@woboq.com>
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QOBJECTDEFS_H
42#error Do not include qobjectdefs_impl.h directly
43#include <QtCore/qnamespace.h>
44#endif
45
46#if 0
47#pragma qt_sync_skip_header_check
48#pragma qt_sync_stop_processing
49#endif
50
51QT_BEGIN_NAMESPACE
52class QObject;
53
54namespace QtPrivate {
55 template <typename T> struct RemoveRef { typedef T Type; };
56 template <typename T> struct RemoveRef<T&> { typedef T Type; };
57 template <typename T> struct RemoveConstRef { typedef T Type; };
58 template <typename T> struct RemoveConstRef<const T&> { typedef T Type; };
59
60 /*
61 The following List classes are used to help to handle the list of arguments.
62 It follow the same principles as the lisp lists.
63 List_Left<L,N> take a list and a number as a parameter and returns (via the Value typedef,
64 the list composed of the first N element of the list
65 */
66 // With variadic template, lists are represented using a variadic template argument instead of the lisp way
67 template <typename...> struct List {};
68 template <typename Head, typename... Tail> struct List<Head, Tail...> { typedef Head Car; typedef List<Tail...> Cdr; };
69 template <typename, typename> struct List_Append;
70 template <typename... L1, typename...L2> struct List_Append<List<L1...>, List<L2...>> { typedef List<L1..., L2...> Value; };
71 template <typename L, int N> struct List_Left {
72 typedef typename List_Append<List<typename L::Car>,typename List_Left<typename L::Cdr, N - 1>::Value>::Value Value;
73 };
74 template <typename L> struct List_Left<L, 0> { typedef List<> Value; };
75 // List_Select<L,N> returns (via typedef Value) the Nth element of the list L
76 template <typename L, int N> struct List_Select { typedef typename List_Select<typename L::Cdr, N - 1>::Value Value; };
77 template <typename L> struct List_Select<L,0> { typedef typename L::Car Value; };
78
79 /*
80 trick to set the return value of a slot that works even if the signal or the slot returns void
81 to be used like function(), ApplyReturnValue<ReturnType>(&return_value)
82 if function() returns a value, the operator,(T, ApplyReturnValue<ReturnType>) is called, but if it
83 returns void, the builtin one is used without an error.
84 */
85 template <typename T>
86 struct ApplyReturnValue {
87 void *data;
88 explicit ApplyReturnValue(void *data_) : data(data_) {}
89 };
90 template<typename T, typename U>
91 void operator,(T &&value, const ApplyReturnValue<U> &container) {
92 if (container.data)
93 *reinterpret_cast<U *>(container.data) = std::forward<T>(value);
94 }
95 template<typename T>
96 void operator,(T, const ApplyReturnValue<void> &) {}
97
98
99 /*
100 The FunctionPointer<Func> struct is a type trait for function pointer.
101 - ArgumentCount is the number of argument, or -1 if it is unknown
102 - the Object typedef is the Object of a pointer to member function
103 - the Arguments typedef is the list of argument (in a QtPrivate::List)
104 - the Function typedef is an alias to the template parameter Func
105 - the call<Args, R>(f,o,args) method is used to call that slot
106 Args is the list of argument of the signal
107 R is the return type of the signal
108 f is the function pointer
109 o is the receiver object
110 and args is the array of pointer to arguments, as used in qt_metacall
111
112 The Functor<Func,N> struct is the helper to call a functor of N argument.
113 its call function is the same as the FunctionPointer::call function.
114 */
115 template <int...> struct IndexesList {};
116 template <typename IndexList, int Right> struct IndexesAppend;
117 template <int... Left, int Right> struct IndexesAppend<IndexesList<Left...>, Right>
118 { typedef IndexesList<Left..., Right> Value; };
119 template <int N> struct Indexes
120 { typedef typename IndexesAppend<typename Indexes<N - 1>::Value, N - 1>::Value Value; };
121 template <> struct Indexes<0> { typedef IndexesList<> Value; };
122 template<typename Func> struct FunctionPointer { enum {ArgumentCount = -1, IsPointerToMemberFunction = false}; };
123
124 template <typename, typename, typename, typename> struct FunctorCall;
125 template <int... II, typename... SignalArgs, typename R, typename Function>
126 struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, Function> {
127 static void call(Function &f, void **arg) {
128 f((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
129 }
130 };
131 template <int... II, typename... SignalArgs, typename R, typename... SlotArgs, typename SlotRet, class Obj>
132 struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, SlotRet (Obj::*)(SlotArgs...)> {
133 static void call(SlotRet (Obj::*f)(SlotArgs...), Obj *o, void **arg) {
134 (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
135 }
136 };
137 template <int... II, typename... SignalArgs, typename R, typename... SlotArgs, typename SlotRet, class Obj>
138 struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, SlotRet (Obj::*)(SlotArgs...) const> {
139 static void call(SlotRet (Obj::*f)(SlotArgs...) const, Obj *o, void **arg) {
140 (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
141 }
142 };
143#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
144 template <int... II, typename... SignalArgs, typename R, typename... SlotArgs, typename SlotRet, class Obj>
145 struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, SlotRet (Obj::*)(SlotArgs...) noexcept> {
146 static void call(SlotRet (Obj::*f)(SlotArgs...) noexcept, Obj *o, void **arg) {
147 (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
148 }
149 };
150 template <int... II, typename... SignalArgs, typename R, typename... SlotArgs, typename SlotRet, class Obj>
151 struct FunctorCall<IndexesList<II...>, List<SignalArgs...>, R, SlotRet (Obj::*)(SlotArgs...) const noexcept> {
152 static void call(SlotRet (Obj::*f)(SlotArgs...) const noexcept, Obj *o, void **arg) {
153 (o->*f)((*reinterpret_cast<typename RemoveRef<SignalArgs>::Type *>(arg[II+1]))...), ApplyReturnValue<R>(arg[0]);
154 }
155 };
156#endif
157
158 template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret (Obj::*) (Args...)>
159 {
160 typedef Obj Object;
161 typedef List<Args...> Arguments;
162 typedef Ret ReturnType;
163 typedef Ret (Obj::*Function) (Args...);
164 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true};
165 template <typename SignalArgs, typename R>
166 static void call(Function f, Obj *o, void **arg) {
167 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, o, arg);
168 }
169 };
170 template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret (Obj::*) (Args...) const>
171 {
172 typedef Obj Object;
173 typedef List<Args...> Arguments;
174 typedef Ret ReturnType;
175 typedef Ret (Obj::*Function) (Args...) const;
176 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true};
177 template <typename SignalArgs, typename R>
178 static void call(Function f, Obj *o, void **arg) {
179 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, o, arg);
180 }
181 };
182
183 template<typename Ret, typename... Args> struct FunctionPointer<Ret (*) (Args...)>
184 {
185 typedef List<Args...> Arguments;
186 typedef Ret ReturnType;
187 typedef Ret (*Function) (Args...);
188 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = false};
189 template <typename SignalArgs, typename R>
190 static void call(Function f, void *, void **arg) {
191 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, arg);
192 }
193 };
194
195#if defined(__cpp_noexcept_function_type) && __cpp_noexcept_function_type >= 201510
196 template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret (Obj::*) (Args...) noexcept>
197 {
198 typedef Obj Object;
199 typedef List<Args...> Arguments;
200 typedef Ret ReturnType;
201 typedef Ret (Obj::*Function) (Args...) noexcept;
202 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true};
203 template <typename SignalArgs, typename R>
204 static void call(Function f, Obj *o, void **arg) {
205 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, o, arg);
206 }
207 };
208 template<class Obj, typename Ret, typename... Args> struct FunctionPointer<Ret (Obj::*) (Args...) const noexcept>
209 {
210 typedef Obj Object;
211 typedef List<Args...> Arguments;
212 typedef Ret ReturnType;
213 typedef Ret (Obj::*Function) (Args...) const noexcept;
214 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = true};
215 template <typename SignalArgs, typename R>
216 static void call(Function f, Obj *o, void **arg) {
217 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, o, arg);
218 }
219 };
220
221 template<typename Ret, typename... Args> struct FunctionPointer<Ret (*) (Args...) noexcept>
222 {
223 typedef List<Args...> Arguments;
224 typedef Ret ReturnType;
225 typedef Ret (*Function) (Args...) noexcept;
226 enum {ArgumentCount = sizeof...(Args), IsPointerToMemberFunction = false};
227 template <typename SignalArgs, typename R>
228 static void call(Function f, void *, void **arg) {
229 FunctorCall<typename Indexes<ArgumentCount>::Value, SignalArgs, R, Function>::call(f, arg);
230 }
231 };
232#endif
233
234 template<typename Function, int N> struct Functor
235 {
236 template <typename SignalArgs, typename R>
237 static void call(Function &f, void *, void **arg) {
238 FunctorCall<typename Indexes<N>::Value, SignalArgs, R, Function>::call(f, arg);
239 }
240 };
241
242 /*
243 Logic that checks if the underlying type of an enum is signed or not.
244 Needs an external, explicit check that E is indeed an enum. Works
245 around the fact that it's undefined behavior to instantiate
246 std::underlying_type on non-enums (cf. §20.13.7.6 [meta.trans.other]).
247 */
248 template<typename E, typename Enable = void>
249 struct IsEnumUnderlyingTypeSigned : std::false_type
250 {
251 };
252
253 template<typename E>
254 struct IsEnumUnderlyingTypeSigned<E, typename std::enable_if<std::is_enum<E>::value>::type>
255 : std::integral_constant<bool, std::is_signed<typename std::underlying_type<E>::type>::value>
256 {
257 };
258
259 /*
260 Logic that checks if the argument of the slot does not narrow the
261 argument of the signal when used in list initialization. Cf. §8.5.4.7
262 [dcl.init.list] for the definition of narrowing.
263 For incomplete From/To types, there's no narrowing.
264 */
265 template<typename From, typename To, typename Enable = void>
266 struct AreArgumentsNarrowedBase : std::false_type
267 {
268 };
269
270 template<typename From, typename To>
271 struct AreArgumentsNarrowedBase<From, To, typename std::enable_if<sizeof(From) && sizeof(To)>::type>
272 : std::integral_constant<bool,
273 (std::is_floating_point<From>::value && std::is_integral<To>::value) ||
274 (std::is_floating_point<From>::value && std::is_floating_point<To>::value && sizeof(From) > sizeof(To)) ||
275 ((std::is_integral<From>::value || std::is_enum<From>::value) && std::is_floating_point<To>::value) ||
276 (std::is_integral<From>::value && std::is_integral<To>::value
277 && (sizeof(From) > sizeof(To)
278 || (std::is_signed<From>::value ? !std::is_signed<To>::value
279 : (std::is_signed<To>::value && sizeof(From) == sizeof(To))))) ||
280 (std::is_enum<From>::value && std::is_integral<To>::value
281 && (sizeof(From) > sizeof(To)
282 || (IsEnumUnderlyingTypeSigned<From>::value ? !std::is_signed<To>::value
283 : (std::is_signed<To>::value && sizeof(From) == sizeof(To)))))
284 >
285 {
286 };
287
288 /*
289 Logic that check if the arguments of the slot matches the argument of the signal.
290 To be used like this:
291 Q_STATIC_ASSERT(CheckCompatibleArguments<FunctionPointer<Signal>::Arguments, FunctionPointer<Slot>::Arguments>::value)
292 */
293 template<typename A1, typename A2> struct AreArgumentsCompatible {
294 static int test(const typename RemoveRef<A2>::Type&);
295 static char test(...);
296 static const typename RemoveRef<A1>::Type &dummy();
297 enum { value = sizeof(test(dummy())) == sizeof(int) };
298#ifdef QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
299 using AreArgumentsNarrowed = AreArgumentsNarrowedBase<typename RemoveRef<A1>::Type, typename RemoveRef<A2>::Type>;
300 Q_STATIC_ASSERT_X(!AreArgumentsNarrowed::value, "Signal and slot arguments are not compatible (narrowing)");
301#endif
302 };
303 template<typename A1, typename A2> struct AreArgumentsCompatible<A1, A2&> { enum { value = false }; };
304 template<typename A> struct AreArgumentsCompatible<A&, A&> { enum { value = true }; };
305 // void as a return value
306 template<typename A> struct AreArgumentsCompatible<void, A> { enum { value = true }; };
307 template<typename A> struct AreArgumentsCompatible<A, void> { enum { value = true }; };
308 template<> struct AreArgumentsCompatible<void, void> { enum { value = true }; };
309
310 template <typename List1, typename List2> struct CheckCompatibleArguments { enum { value = false }; };
311 template <> struct CheckCompatibleArguments<List<>, List<>> { enum { value = true }; };
312 template <typename List1> struct CheckCompatibleArguments<List1, List<>> { enum { value = true }; };
313 template <typename Arg1, typename Arg2, typename... Tail1, typename... Tail2>
314 struct CheckCompatibleArguments<List<Arg1, Tail1...>, List<Arg2, Tail2...>>
315 {
316 enum { value = AreArgumentsCompatible<typename RemoveConstRef<Arg1>::Type, typename RemoveConstRef<Arg2>::Type>::value
317 && CheckCompatibleArguments<List<Tail1...>, List<Tail2...>>::value };
318 };
319
320 /*
321 Find the maximum number of arguments a functor object can take and be still compatible with
322 the arguments from the signal.
323 Value is the number of arguments, or -1 if nothing matches.
324 */
325 template <typename Functor, typename ArgList> struct ComputeFunctorArgumentCount;
326
327 template <typename Functor, typename ArgList, bool Done> struct ComputeFunctorArgumentCountHelper
328 { enum { Value = -1 }; };
329 template <typename Functor, typename First, typename... ArgList>
330 struct ComputeFunctorArgumentCountHelper<Functor, List<First, ArgList...>, false>
331 : ComputeFunctorArgumentCount<Functor,
332 typename List_Left<List<First, ArgList...>, sizeof...(ArgList)>::Value> {};
333
334 template <typename Functor, typename... ArgList> struct ComputeFunctorArgumentCount<Functor, List<ArgList...>>
335 {
336 template <typename D> static D dummy();
337 template <typename F> static auto test(F f) -> decltype(((f.operator()((dummy<ArgList>())...)), int()));
338 static char test(...);
339 enum {
340 Ok = sizeof(test(dummy<Functor>())) == sizeof(int),
341 Value = Ok ? int(sizeof...(ArgList)) : int(ComputeFunctorArgumentCountHelper<Functor, List<ArgList...>, Ok>::Value)
342 };
343 };
344
345 /* get the return type of a functor, given the signal argument list */
346 template <typename Functor, typename ArgList> struct FunctorReturnType;
347 template <typename Functor, typename ... ArgList> struct FunctorReturnType<Functor, List<ArgList...>> {
348 template <typename D> static D dummy();
349 typedef decltype(dummy<Functor>().operator()((dummy<ArgList>())...)) Value;
350 };
351
352 // internal base class (interface) containing functions required to call a slot managed by a pointer to function.
353 class QSlotObjectBase {
354 QAtomicInt m_ref;
355 // don't use virtual functions here; we don't want the
356 // compiler to create tons of per-polymorphic-class stuff that
357 // we'll never need. We just use one function pointer.
358 typedef void (*ImplFn)(int which, QSlotObjectBase* this_, QObject *receiver, void **args, bool *ret);
359 const ImplFn m_impl;
360 protected:
361 enum Operation {
362 Destroy,
363 Call,
364 Compare,
365
366 NumOperations
367 };
368 public:
369 explicit QSlotObjectBase(ImplFn fn) : m_ref(1), m_impl(fn) {}
370
371 inline int ref() Q_DECL_NOTHROW { return m_ref.ref(); }
372 inline void destroyIfLastRef() Q_DECL_NOTHROW
373 { if (!m_ref.deref()) m_impl(Destroy, this, nullptr, nullptr, nullptr); }
374
375 inline bool compare(void **a) { bool ret = false; m_impl(Compare, this, nullptr, a, &ret); return ret; }
376 inline void call(QObject *r, void **a) { m_impl(Call, this, r, a, nullptr); }
377 protected:
378 ~QSlotObjectBase() {}
379 private:
380 Q_DISABLE_COPY(QSlotObjectBase)
381 };
382
383 // implementation of QSlotObjectBase for which the slot is a pointer to member function of a QObject
384 // Args and R are the List of arguments and the returntype of the signal to which the slot is connected.
385 template<typename Func, typename Args, typename R> class QSlotObject : public QSlotObjectBase
386 {
387 typedef QtPrivate::FunctionPointer<Func> FuncType;
388 Func function;
389 static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)
390 {
391 switch (which) {
392 case Destroy:
393 delete static_cast<QSlotObject*>(this_);
394 break;
395 case Call:
396 FuncType::template call<Args, R>(static_cast<QSlotObject*>(this_)->function, static_cast<typename FuncType::Object *>(r), a);
397 break;
398 case Compare:
399 *ret = *reinterpret_cast<Func *>(a) == static_cast<QSlotObject*>(this_)->function;
400 break;
401 case NumOperations: ;
402 }
403 }
404 public:
405 explicit QSlotObject(Func f) : QSlotObjectBase(&impl), function(f) {}
406 };
407 // implementation of QSlotObjectBase for which the slot is a functor (or lambda)
408 // N is the number of arguments
409 // Args and R are the List of arguments and the returntype of the signal to which the slot is connected.
410 template<typename Func, int N, typename Args, typename R> class QFunctorSlotObject : public QSlotObjectBase
411 {
412 typedef QtPrivate::Functor<Func, N> FuncType;
413 Func function;
414 static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)
415 {
416 switch (which) {
417 case Destroy:
418 delete static_cast<QFunctorSlotObject*>(this_);
419 break;
420 case Call:
421 FuncType::template call<Args, R>(static_cast<QFunctorSlotObject*>(this_)->function, r, a);
422 break;
423 case Compare: // not implemented
424 case NumOperations:
425 Q_UNUSED(ret);
426 }
427 }
428 public:
429 explicit QFunctorSlotObject(Func f) : QSlotObjectBase(&impl), function(std::move(f)) {}
430 };
431
432 // typedefs for readability for when there are no parameters
433 template <typename Func>
434 using QSlotObjectWithNoArgs = QSlotObject<Func,
435 QtPrivate::List<>,
436 typename QtPrivate::FunctionPointer<Func>::ReturnType>;
437
438 template <typename Func, typename R>
439 using QFunctorSlotObjectWithNoArgs = QFunctorSlotObject<Func, 0, QtPrivate::List<>, R>;
440
441 template <typename Func>
442 using QFunctorSlotObjectWithNoArgsImplicitReturn = QFunctorSlotObjectWithNoArgs<Func, typename QtPrivate::FunctionPointer<Func>::ReturnType>;
443}
444
445QT_END_NAMESPACE
446
447