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 <[email protected]>
  • Loading branch information
lygstate committed Jul 29, 2022
1 parent 8d6b274 commit 5d2cc11
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 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
34 changes: 19 additions & 15 deletions src/tools/moc/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1157,30 +1157,34 @@ 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;
fprintf(out, " {\n");
fprintf(out, " using _t = %s (%s::*)(",f.type.rawName.constData() , cdef->classname.constData());

int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (j)
fprintf(out, ", ");
fprintf(out, "%s", QByteArray(a.type.name + ' ' + a.rightType).constData());
}
if (f.isPrivateSignal) {
if (argsCount > 0)
fprintf(out, ", ");
fprintf(out, "%s", "QPrivateSignal");
if (f.isRawSlot) {
fprintf(out, "QMethodRawArguments");
} else {
int argsCount = f.arguments.count();
for (int j = 0; j < argsCount; ++j) {
const ArgumentDef &a = f.arguments.at(j);
if (j)
fprintf(out, ", ");
fprintf(out, "%s", QByteArray(a.type.name + ' ' + a.rightType).constData());
}
if (f.isPrivateSignal) {
if (argsCount > 0)
fprintf(out, ", ");
fprintf(out, "%s", "QPrivateSignal");
}
}
if (f.isConst)
fprintf(out, ") const;\n");
Expand Down

0 comments on commit 5d2cc11

Please sign in to comment.