aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/qqmlmetaobject.cpp
blob: e84c5a366eccf3e9c5a43e60e51379835a7dba58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qqmlmetaobject_p.h"

#include <private/qqmlengine_p.h>
#include <private/qqmlpropertycachemethodarguments_p.h>

QT_BEGIN_NAMESPACE

// Returns true if \a from is assignable to a property of type \a to
bool QQmlMetaObject::canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to)
{
    Q_ASSERT(!from.isNull() && !to.isNull());

    auto equal = [] (const QMetaObject *lhs, const QMetaObject *rhs) -> bool {
        return lhs == rhs || (lhs && rhs && lhs->d.stringdata == rhs->d.stringdata);
    };

    const QMetaObject *tom = to.metaObject();
    if (tom == &QObject::staticMetaObject) return true;

    const QMetaObject *fromm = from.metaObject();
    while (fromm) {
        if (equal(fromm, tom))
            return true;
        fromm = fromm->superClass();
    }

    return false;
}

void QQmlMetaObject::resolveGadgetMethodOrPropertyIndex(QMetaObject::Call type, const QMetaObject **metaObject, int *index)
{
    int offset;

    switch (type) {
    case QMetaObject::ReadProperty:
    case QMetaObject::WriteProperty:
    case QMetaObject::ResetProperty:
        offset = (*metaObject)->propertyOffset();
        while (*index < offset) {
            *metaObject = (*metaObject)->superClass();
            offset = (*metaObject)->propertyOffset();
        }
        break;
    case QMetaObject::InvokeMetaMethod:
        offset = (*metaObject)->methodOffset();
        while (*index < offset) {
            *metaObject = (*metaObject)->superClass();
            offset = (*metaObject)->methodOffset();
        }
        break;
    default:
        offset = 0;
        Q_UNIMPLEMENTED();
        offset = INT_MAX;
    }

    *index -= offset;
}

QMetaType QQmlMetaObject::methodReturnType(const QQmlPropertyData &data, QByteArray *unknownTypeError) const
{
    Q_ASSERT(_m && data.coreIndex() >= 0);

    QMetaType type = data.propType();
    if (!type.isValid()) {
        // Find the return type name from the method info
        type = _m->method(data.coreIndex()).returnMetaType();
    }
    if (type.flags().testFlag(QMetaType::IsEnumeration))
        type = QMetaType::fromType<int>();
    if (type.isValid())
        return type;
    else if (unknownTypeError)
        *unknownTypeError = _m->method(data.coreIndex()).typeName();
    return QMetaType();
}

bool QQmlMetaObject::methodParameterTypes(int index, ArgTypeStorage *argStorage,
                                          QByteArray *unknownTypeError) const
{
    Q_ASSERT(_m && index >= 0);

    QMetaMethod m = _m->method(index);
    return methodParameterTypes(m, argStorage, unknownTypeError);
}

bool QQmlMetaObject::constructorParameterTypes(int index, ArgTypeStorage *dummy,
                                                     QByteArray *unknownTypeError) const
{
    QMetaMethod m = _m->constructor(index);
    return methodParameterTypes(m, dummy, unknownTypeError);
}

bool QQmlMetaObject::methodParameterTypes(const QMetaMethod &m, ArgTypeStorage *argStorage,
                                          QByteArray *unknownTypeError)
{
    Q_ASSERT(argStorage);

    int argc = m.parameterCount();
    argStorage->resize(argc);
    for (int ii = 0; ii < argc; ++ii) {
        QMetaType type = m.parameterMetaType(ii);
        // we treat enumerations as int
        if (type.flags().testFlag(QMetaType::IsEnumeration))
            type = QMetaType::fromType<int>();
        if (!type.isValid()) {
            if (unknownTypeError)
                *unknownTypeError =  m.parameterTypeName(ii);
            return false;
        }
        argStorage->operator[](ii) = type;
    }
    return true;
}

QT_END_NAMESPACE