Skip to content

Commit

Permalink
QMetaMethod: Add QMetaMethod::from(&MyClass:myFunction) support
Browse files Browse the repository at this point in the history
Add QMetaMethod::from QMetaMethod::fromInvokable QMetaMethod::fromSlot

Task-number: QTBUG-36861

Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
  • Loading branch information
lygstate committed Jul 29, 2022
1 parent 8d6b274 commit 4de6dc4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
44 changes: 39 additions & 5 deletions src/corelib/kernel/qmetaobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,48 @@ class Q_CORE_EXPORT QMetaMethod
template <typename PointerToMemberFunction>
static inline QMetaMethod fromSignal(PointerToMemberFunction signal)
{
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> SignalType;
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename SignalType::Object>::Value,
"No Q_OBJECT in the class with the signal");
return fromSignalImpl(&SignalType::Object::staticMetaObject,
reinterpret_cast<void **>(&signal));
QMetaMethod method = from(signal);
if (method.methodType() == Signal)
{
return method;
}
return QMetaMethod();
}

template <typename PointerToMemberFunction>
static inline QMetaMethod fromSlot(PointerToMemberFunction slot)
{
QMetaMethod method = from(slot);
if (method.methodType() == Slot)
{
return method;
}
return QMetaMethod();
}

template <typename PointerToMemberFunction>
static inline QMetaMethod fromInvokable(PointerToMemberFunction invokable)
{
QMetaMethod method = from(invokable);
if (method.methodType() == Method)
{
return method;
}
return QMetaMethod();
}

template <typename PointerToMemberFunction>
static inline QMetaMethod from(PointerToMemberFunction func)
{
typedef QtPrivate::FunctionPointer<PointerToMemberFunction> ClassType;
static_assert(QtPrivate::HasQ_OBJECT_Macro<typename ClassType::Object>::Value,
"No Q_OBJECT in the class with the func");
return fromSignalImpl(&ClassType::Object::staticMetaObject,
reinterpret_cast<void **>(&func));
}

private:
// ### Qt 7: rename fromSignalImpl to fromImpl
static QMetaMethod fromSignalImpl(const QMetaObject *, void **);
static QMetaMethod fromRelativeMethodIndex(const QMetaObject *mobj, int index);
static QMetaMethod fromRelativeConstructorIndex(const QMetaObject *mobj, int index);
Expand Down
8 changes: 4 additions & 4 deletions src/tools/moc/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,13 +1157,13 @@ void Generator::generateStaticMetacall()
}

}
if (!cdef->signalList.isEmpty()) {
Q_ASSERT(needElse); // if there is signal, there was method.
if (!methodList.isEmpty()) {
Q_ASSERT(needElse); // there was method.
fprintf(out, " else if (_c == QMetaObject::IndexOfMethod) {\n");
fprintf(out, " int *result = reinterpret_cast<int *>(_a[0]);\n");
bool anythingUsed = false;
for (int methodindex = 0; methodindex < cdef->signalList.size(); ++methodindex) {
const FunctionDef &f = cdef->signalList.at(methodindex);
for (int methodindex = 0; methodindex < methodList.size(); ++methodindex) {
const FunctionDef &f = methodList.at(methodindex);
if (f.wasCloned || !f.inPrivateClass.isEmpty() || f.isStatic)
continue;
anythingUsed = true;
Expand Down

0 comments on commit 4de6dc4

Please sign in to comment.