Skip to content

Commit 5d2cc11

Browse files
committed
QMetaMethod: Add QMetaMethod::from(&MyClass:myFunction) support
Add QMetaMethod::from QMetaMethod::fromInvokable QMetaMethod::fromSlot Task-number: QTBUG-36861 Signed-off-by: Yonggang Luo <[email protected]>
1 parent 8d6b274 commit 5d2cc11

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

src/corelib/kernel/qmetaobject.h

+39-5
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,48 @@ class Q_CORE_EXPORT QMetaMethod
138138
template <typename PointerToMemberFunction>
139139
static inline QMetaMethod fromSignal(PointerToMemberFunction signal)
140140
{
141-
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> SignalType;
142-
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,
143-
"No Q_OBJECT in the class with the signal");
144-
return fromSignalImpl(&SignalType::Object::staticMetaObject,
145-
reinterpret_cast<void **>(&signal));
141+
QMetaMethod method = from(signal);
142+
if (method.methodType() == Signal)
143+
{
144+
return method;
145+
}
146+
return QMetaMethod();
147+
}
148+
149+
template <typename PointerToMemberFunction>
150+
static inline QMetaMethod fromSlot(PointerToMemberFunction slot)
151+
{
152+
QMetaMethod method = from(slot);
153+
if (method.methodType() == Slot)
154+
{
155+
return method;
156+
}
157+
return QMetaMethod();
158+
}
159+
160+
template <typename PointerToMemberFunction>
161+
static inline QMetaMethod fromInvokable(PointerToMemberFunction invokable)
162+
{
163+
QMetaMethod method = from(invokable);
164+
if (method.methodType() == Method)
165+
{
166+
return method;
167+
}
168+
return QMetaMethod();
169+
}
170+
171+
template <typename PointerToMemberFunction>
172+
static inline QMetaMethod from(PointerToMemberFunction func)
173+
{
174+
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> ClassType;
175+
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename ClassType::Object>::Value,
176+
"No Q_OBJECT in the class with the func");
177+
return fromSignalImpl(&ClassType::Object::staticMetaObject,
178+
reinterpret_cast<void **>(&func));
146179
}
147180

148181
private:
182+
// ### Qt 7: rename fromSignalImpl to fromImpl
149183
static QMetaMethod fromSignalImpl(const QMetaObject *, void **);
150184
static QMetaMethod fromRelativeMethodIndex(const QMetaObject *mobj, int index);
151185
static QMetaMethod fromRelativeConstructorIndex(const QMetaObject *mobj, int index);

src/tools/moc/generator.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -1157,30 +1157,34 @@ void Generator::generateStaticMetacall()
11571157
}
11581158

11591159
}
1160-
if (!cdef->signalList.isEmpty()) {
1161-
Q_ASSERT(needElse); // if there is signal, there was method.
1160+
if (!methodList.isEmpty()) {
1161+
Q_ASSERT(needElse); // there was method.
11621162
fprintf(out, " else if (_c == QMetaObject::IndexOfMethod) {\n");
11631163
fprintf(out, " int *result = reinterpret_cast<int *>(_a[0]);\n");
11641164
bool anythingUsed = false;
1165-
for (int methodindex = 0; methodindex < cdef->signalList.size(); ++methodindex) {
1166-
const FunctionDef &f = cdef->signalList.at(methodindex);
1165+
for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
1166+
const FunctionDef &f = methodList.at(methodindex);
11671167
if (f.wasCloned || !f.inPrivateClass.isEmpty() || f.isStatic)
11681168
continue;
11691169
anythingUsed = true;
11701170
fprintf(out, " {\n");
11711171
fprintf(out, " using _t = %s (%s::*)(",f.type.rawName.constData() , cdef->classname.constData());
11721172

1173-
int argsCount = f.arguments.count();
1174-
for (int j = 0; j < argsCount; ++j) {
1175-
const ArgumentDef &a = f.arguments.at(j);
1176-
if (j)
1177-
fprintf(out, ", ");
1178-
fprintf(out, "%s", QByteArray(a.type.name + ' ' + a.rightType).constData());
1179-
}
1180-
if (f.isPrivateSignal) {
1181-
if (argsCount > 0)
1182-
fprintf(out, ", ");
1183-
fprintf(out, "%s", "QPrivateSignal");
1173+
if (f.isRawSlot) {
1174+
fprintf(out, "QMethodRawArguments");
1175+
} else {
1176+
int argsCount = f.arguments.count();
1177+
for (int j = 0; j < argsCount; ++j) {
1178+
const ArgumentDef &a = f.arguments.at(j);
1179+
if (j)
1180+
fprintf(out, ", ");
1181+
fprintf(out, "%s", QByteArray(a.type.name + ' ' + a.rightType).constData());
1182+
}
1183+
if (f.isPrivateSignal) {
1184+
if (argsCount > 0)
1185+
fprintf(out, ", ");
1186+
fprintf(out, "%s", "QPrivateSignal");
1187+
}
11841188
}
11851189
if (f.isConst)
11861190
fprintf(out, ") const;\n");

0 commit comments

Comments
 (0)