OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / libs / extensionsystem / invoker.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #ifndef EXTENSIONSYSTEM_INVOKER_H
34 #define EXTENSIONSYSTEM_INVOKER_H
35
36 #include "extensionsystem_global.h"
37
38 #include <QtCore/QMetaMethod>
39 #include <QtCore/QMetaObject>
40 #include <QtCore/QMetaType>
41 #include <QtCore/QVarLengthArray>
42
43 namespace ExtensionSystem {
44
45 class EXTENSIONSYSTEM_EXPORT InvokerBase
46 {
47 public:
48     InvokerBase();
49     ~InvokerBase();
50
51     bool wasSuccessful() const;
52
53     template <class T> void addArgument(const T &t)
54     {
55         arg[lastArg++] = QGenericArgument(typeName<T>(), &t);
56     }
57
58     template <class T> void setReturnValue(T &t)
59     {
60         useRet = true;
61         ret = QGenericReturnArgument(typeName<T>(), &t);
62     }
63
64     void invoke(QObject *target, const char *slot);
65
66 private:
67     InvokerBase(const InvokerBase &); // Unimplemented.
68     template <class T> const char *typeName()
69     {
70         return QMetaType::typeName(qMetaTypeId<T>());
71     }
72     QObject *target;
73     QGenericArgument arg[10];
74     QGenericReturnArgument ret;
75     QVarLengthArray<char, 512> sig;
76     int lastArg;
77     bool success;
78     bool useRet;
79     mutable bool nag;
80 };
81
82 template <class Result>
83 class Invoker : public InvokerBase
84 {
85 public:
86     Invoker(QObject *target, const char *slot)
87     {
88         InvokerBase::invoke(target, slot);
89     }
90
91     template <class T0>
92     Invoker(QObject *target, const char *slot, const T0 &t0)
93     {
94         setReturnValue(result);
95         addArgument(t0);
96         InvokerBase::invoke(target, slot);
97     }
98
99     template <class T0, class T1>
100     Invoker(QObject *target, const char *slot, const T0 &t0, const T1 &t1)
101     {
102         setReturnValue(result);
103         addArgument(t0);
104         addArgument(t1);
105         InvokerBase::invoke(target, slot);
106     }
107
108     template <class T0, class T1, class T2>
109     Invoker(QObject *target, const char *slot, const T0 &t0,
110         const T1 &t1, const T2 &t2)
111     {
112         setReturnValue(result);
113         addArgument(t0);
114         addArgument(t1);
115         addArgument(t2);
116         InvokerBase::invoke(target, slot);
117     }
118
119     operator Result() const { return result; }
120
121 private:
122     Result result;
123 };
124
125 template<> class Invoker<void> : public InvokerBase
126 {
127 public:
128     Invoker(QObject *target, const char *slot)
129     {
130         InvokerBase::invoke(target, slot);
131     }
132
133     template <class T0>
134     Invoker(QObject *target, const char *slot, const T0 &t0)
135     {
136         addArgument(t0);
137         InvokerBase::invoke(target, slot);
138     }
139
140     template <class T0, class T1>
141     Invoker(QObject *target, const char *slot, const T0 &t0, const T1 &t1)
142     {
143         addArgument(t0);
144         addArgument(t1);
145         InvokerBase::invoke(target, slot);
146     }
147
148     template <class T0, class T1, class T2>
149     Invoker(QObject *target, const char *slot, const T0 &t0,
150         const T1 &t1, const T2 &t2)
151     {
152         addArgument(t0);
153         addArgument(t1);
154         addArgument(t2);
155         InvokerBase::invoke(target, slot);
156     }
157 };
158
159 template <class Result>
160 Result invokeHelper(InvokerBase &in, QObject *target, const char *slot)
161 {
162     Result result;
163     in.setReturnValue(result);
164     in.invoke(target, slot);
165     return result;
166 }
167
168 template <>
169 inline void invokeHelper<void>(InvokerBase &in, QObject *target, const char *slot)
170 {
171     in.invoke(target, slot);
172 }
173
174 template<class Result>
175 Result invoke(QObject *target, const char *slot)
176 {
177     InvokerBase in;
178     return invokeHelper<Result>(in, target, slot);
179 }
180
181 template<class Result, class T0>
182 Result invoke(QObject *target, const char *slot, const T0 &t0)
183 {
184     InvokerBase in;
185     in.addArgument(t0);
186     return invokeHelper<Result>(in, target, slot);
187 }
188
189 template<class Result, class T0, class T1>
190 Result invoke(QObject *target, const char *slot, const T0 &t0, const T1 &t1)
191 {
192     InvokerBase in;
193     in.addArgument(t0);
194     in.addArgument(t1);
195     return invokeHelper<Result>(in, target, slot);
196 }
197
198 template<class Result, class T0, class T1, class T2>
199 Result invoke(QObject *target, const char *slot,
200     const T0 &t0, const T1 &t1, const T2 &t2)
201 {
202     InvokerBase in;
203     in.addArgument(t0);
204     in.addArgument(t1);
205     in.addArgument(t2);
206     return invokeHelper<Result>(in, target, slot);
207 }
208
209 } // namespace ExtensionSystem
210
211 #endif // EXTENSIONSYSTEM_INVOKER_H