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