Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Contact: http://www.qt-project.org/legal
5 : **
6 : ** This file is part of the QtCore module of the Qt Toolkit.
7 : **
8 : ** $QT_BEGIN_LICENSE:LGPL$
9 : ** Commercial License Usage
10 : ** Licensees holding valid commercial Qt licenses may use this file in
11 : ** accordance with the commercial license agreement provided with the
12 : ** Software or, alternatively, in accordance with the terms contained in
13 : ** a written agreement between you and Digia. For licensing terms and
14 : ** conditions see http://qt.digia.com/licensing. For further information
15 : ** use the contact form at http://qt.digia.com/contact-us.
16 : **
17 : ** GNU Lesser General Public License Usage
18 : ** Alternatively, this file may be used under the terms of the GNU Lesser
19 : ** General Public License version 2.1 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 2.1 requirements
23 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 : **
25 : ** In addition, as a special exception, Digia gives you certain additional
26 : ** rights. These rights are described in the Digia Qt LGPL Exception
27 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 : **
29 : ** GNU General Public License Usage
30 : ** Alternatively, this file may be used under the terms of the GNU
31 : ** General Public License version 3.0 as published by the Free Software
32 : ** Foundation and appearing in the file LICENSE.GPL included in the
33 : ** packaging of this file. Please review the following information to
34 : ** ensure the GNU General Public License version 3.0 requirements will be
35 : ** met: http://www.gnu.org/copyleft/gpl.html.
36 : **
37 : **
38 : ** $QT_END_LICENSE$
39 : **
40 : ****************************************************************************/
41 :
42 : #ifndef Q_QDOC
43 :
44 : #ifndef QOBJECT_H
45 : #error Do not include qobject_impl.h directly
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 :
55 :
56 : namespace QtPrivate {
57 : /*
58 : Logic to statically generate the array of qMetaTypeId
59 : ConnectionTypes<FunctionPointer<Signal>::Arguments>::types() returns an array
60 : of int that is suitable for the types arguments of the connection functions.
61 :
62 : The array only exist of all the types are declared as a metatype
63 : (detected using the TypesAreDeclaredMetaType helper struct)
64 : If one of the type is not declared, the function return 0 and the signal
65 : cannot be used in queued connection.
66 : */
67 : #ifndef Q_COMPILER_VARIADIC_TEMPLATES
68 : template <typename ArgList> struct TypesAreDeclaredMetaType { enum { Value = false }; };
69 : template <> struct TypesAreDeclaredMetaType<void> { enum { Value = true }; };
70 : template <typename Arg, typename Tail> struct TypesAreDeclaredMetaType<List<Arg, Tail> > { enum { Value = QMetaTypeId2<Arg>::Defined && TypesAreDeclaredMetaType<Tail>::Value }; };
71 :
72 : template <typename ArgList, bool Declared = TypesAreDeclaredMetaType<ArgList>::Value > struct ConnectionTypes
73 : { static const int *types() { return 0; } };
74 : template <> struct ConnectionTypes<void, true>
75 : { static const int *types() { static const int t[1] = { 0 }; return t; } };
76 : template <typename Arg1> struct ConnectionTypes<List<Arg1, void>, true>
77 : { static const int *types() { static const int t[2] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), 0 }; return t; } };
78 : template <typename Arg1, typename Arg2> struct ConnectionTypes<List<Arg1, List<Arg2, void> >, true>
79 : { static const int *types() { static const int t[3] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg2>::qt_metatype_id(), 0 }; return t; } };
80 : template <typename Arg1, typename Arg2, typename Arg3> struct ConnectionTypes<List<Arg1, List<Arg2, List<Arg3, void> > >, true>
81 : { static const int *types() { static const int t[4] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg2>::qt_metatype_id(),
82 : QtPrivate::QMetaTypeIdHelper<Arg3>::qt_metatype_id(), 0 }; return t; } };
83 : template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> struct ConnectionTypes<List<Arg1, List<Arg2, List<Arg3, List<Arg4, void> > > >, true>
84 : { static const int *types() { static const int t[5] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg2>::qt_metatype_id(),
85 : QtPrivate::QMetaTypeIdHelper<Arg3>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg4>::qt_metatype_id(), 0 }; return t; } };
86 : template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5> struct ConnectionTypes<List<Arg1, List<Arg2, List<Arg3, List<Arg4, List<Arg5, void> > > > >, true>
87 : { static const int *types() { static const int t[6] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg2>::qt_metatype_id(),
88 : QtPrivate::QMetaTypeIdHelper<Arg3>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg4>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg5>::qt_metatype_id(), 0 }; return t; } };
89 : template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5, typename Arg6>
90 : struct ConnectionTypes<List<Arg1, List<Arg2, List<Arg3, List<Arg4, List<Arg5, List<Arg6, void> > > > > >, true>
91 : { static const int *types() { static const int t[7] = { QtPrivate::QMetaTypeIdHelper<Arg1>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg2>::qt_metatype_id(),
92 : QtPrivate::QMetaTypeIdHelper<Arg3>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg4>::qt_metatype_id(), QtPrivate::QMetaTypeIdHelper<Arg5>::qt_metatype_id(),
93 : QtPrivate::QMetaTypeIdHelper<Arg6>::qt_metatype_id(), 0 }; return t; } };
94 : #else
95 : template <typename ArgList> struct TypesAreDeclaredMetaType { enum { Value = false }; };
96 : template <> struct TypesAreDeclaredMetaType<List<>> { enum { Value = true }; };
97 : template <typename Arg, typename... Tail> struct TypesAreDeclaredMetaType<List<Arg, Tail...> >
98 : { enum { Value = QMetaTypeId2<Arg>::Defined && TypesAreDeclaredMetaType<List<Tail...>>::Value }; };
99 :
100 : template <typename ArgList, bool Declared = TypesAreDeclaredMetaType<ArgList>::Value > struct ConnectionTypes
101 0 : { static const int *types() { return 0; } };
102 : template <> struct ConnectionTypes<List<>, true>
103 0 : { static const int *types() { return 0; } };
104 : template <typename... Args> struct ConnectionTypes<List<Args...>, true>
105 0 : { static const int *types() { static const int t[sizeof...(Args) + 1] = { (QtPrivate::QMetaTypeIdHelper<Args>::qt_metatype_id())..., 0 }; return t; } };
106 : #endif
107 :
108 : // internal base class (interface) containing functions required to call a slot managed by a pointer to function.
109 : class QSlotObjectBase {
110 : QAtomicInt m_ref;
111 : // don't use virtual functions here; we don't want the
112 : // compiler to create tons of per-polymorphic-class stuff that
113 : // we'll never need. We just use one function pointer.
114 : typedef void (*ImplFn)(int which, QSlotObjectBase* this_, QObject *receiver, void **args, bool *ret);
115 : const ImplFn m_impl;
116 : protected:
117 : enum Operation {
118 : Destroy,
119 : Call,
120 : Compare,
121 :
122 : NumOperations
123 : };
124 : public:
125 78 : explicit QSlotObjectBase(ImplFn fn) : m_ref(1), m_impl(fn) {}
126 :
127 : inline int ref() Q_DECL_NOTHROW { return m_ref.ref(); }
128 : inline void destroyIfLastRef() Q_DECL_NOTHROW
129 : { if (!m_ref.deref()) m_impl(Destroy, this, 0, 0, 0); }
130 :
131 : inline bool compare(void **a) { bool ret; m_impl(Compare, this, 0, a, &ret); return ret; }
132 : inline void call(QObject *r, void **a) { m_impl(Call, this, r, a, 0); }
133 : protected:
134 78 : ~QSlotObjectBase() {}
135 : private:
136 : Q_DISABLE_COPY(QSlotObjectBase)
137 : };
138 : // implementation of QSlotObjectBase for which the slot is a pointer to member function of a QObject
139 : // Args and R are the List of arguments and the returntype of the signal to which the slot is connected.
140 72 : template<typename Func, typename Args, typename R> class QSlotObject : public QSlotObjectBase
141 : {
142 : typedef QtPrivate::FunctionPointer<Func> FuncType;
143 : Func function;
144 77 : static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)
145 : {
146 77 : switch (which) {
147 : case Destroy:
148 72 : delete static_cast<QSlotObject*>(this_);
149 72 : break;
150 : case Call:
151 5 : FuncType::template call<Args, R>(static_cast<QSlotObject*>(this_)->function, static_cast<typename FuncType::Object *>(r), a);
152 5 : break;
153 : case Compare:
154 0 : *ret = *reinterpret_cast<Func *>(a) == static_cast<QSlotObject*>(this_)->function;
155 0 : break;
156 : case NumOperations: ;
157 : }
158 77 : }
159 : public:
160 72 : explicit QSlotObject(Func f) : QSlotObjectBase(&impl), function(f) {}
161 : };
162 : // implementation of QSlotObjectBase for which the slot is a static function
163 : // Args and R are the List of arguments and the returntype of the signal to which the slot is connected.
164 : template<typename Func, typename Args, typename R> class QStaticSlotObject : public QSlotObjectBase
165 : {
166 : typedef QtPrivate::FunctionPointer<Func> FuncType;
167 : Func function;
168 : static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)
169 : {
170 : switch (which) {
171 : case Destroy:
172 : delete static_cast<QStaticSlotObject*>(this_);
173 : break;
174 : case Call:
175 : FuncType::template call<Args, R>(static_cast<QStaticSlotObject*>(this_)->function, r, a);
176 : break;
177 : case Compare:
178 : *ret = false; // not implemented
179 : break;
180 : case NumOperations: ;
181 : }
182 : }
183 : public:
184 : explicit QStaticSlotObject(Func f) : QSlotObjectBase(&impl), function(f) {}
185 : };
186 : // implementation of QSlotObjectBase for which the slot is a functor (or lambda)
187 : // N is the number of arguments
188 : // Args and R are the List of arguments and the returntype of the signal to which the slot is connected.
189 6 : template<typename Func, int N, typename Args, typename R> class QFunctorSlotObject : public QSlotObjectBase
190 : {
191 : typedef QtPrivate::Functor<Func, N> FuncType;
192 : Func function;
193 13 : static void impl(int which, QSlotObjectBase *this_, QObject *r, void **a, bool *ret)
194 : {
195 13 : switch (which) {
196 : case Destroy:
197 6 : delete static_cast<QFunctorSlotObject*>(this_);
198 6 : break;
199 : case Call:
200 7 : FuncType::template call<Args, R>(static_cast<QFunctorSlotObject*>(this_)->function, r, a);
201 7 : break;
202 : case Compare:
203 0 : *ret = false; // not implemented
204 0 : break;
205 : case NumOperations: ;
206 : }
207 13 : }
208 : public:
209 6 : explicit QFunctorSlotObject(const Func &f) : QSlotObjectBase(&impl), function(f) {}
210 : };
211 : }
212 :
213 :
214 : QT_END_NAMESPACE
215 :
216 : #endif
|