summaryrefslogtreecommitdiff
path: root/src/ivicore/qivipropertyfactory.h
blob: 392156267ced1e9f5ed5b672d7e384daf2a9d30d (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/****************************************************************************
**
** Copyright (C) 2017 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtIvi module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#ifndef QIVIPROPERTYFACTORY_H
#define QIVIPROPERTYFACTORY_H

#include <QtIviCore/qtiviglobal.h>
#include <QtIviCore/QIviProperty>
#include <QtIviCore/qivitypetraits.h>
#include <QtIviCore/qiviqmlconversion_helper.h>
#include <QMetaEnum>

QT_BEGIN_NAMESPACE

template <typename T>
class QIviPropertyFactory : public QIviProperty
{
public:

    //readwrite property
    template <typename attributeGetterFunc, typename attributeSignalFunc, typename valueGetterFunc, typename valueSignalFunc, typename valueSlotFunc>
    static inline QIviPropertyFactory *create(const typename QtPrivate::FunctionPointer<attributeGetterFunc>::Object *sender, attributeGetterFunc attributeGetter, attributeSignalFunc attributeSignal, valueGetterFunc valueGetter, valueSignalFunc valueSignal, valueSlotFunc valueSlot)
    {
        typedef QtPrivate::FunctionPointer<valueSignalFunc> ValueSignalType;
        typedef QtPrivate::FunctionPointer<valueSlotFunc> SlotType;
        typedef QtPrivate::List<T, void> ValueType;

        Q_STATIC_ASSERT_X(int(SlotType::ArgumentCount) == 1,
                          "The value slot needs to have exactly one argument");

        Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<typename ValueSignalType::Arguments, typename SlotType::Arguments>::value),
                          "Value signal and value slot arguments are not compatible.");
        Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<ValueType, typename SlotType::Arguments>::value),
                          "Property type and value slot arguments are not compatible.");

        QIviPropertyFactory *prop = create(sender, attributeGetter, attributeSignal, valueGetter, valueSignal);
        prop->setValueSetterHelper(new QtPrivate::QSlotObject<valueSlotFunc, typename SlotType::Arguments, void>(valueSlot));

        return prop;
    }

    //readonly property (no write slot)
    template <typename attributeGetterFunc, typename attributeSignalFunc, typename valueGetterFunc, typename valueSignalFunc>
    static inline QIviPropertyFactory *create(const typename QtPrivate::FunctionPointer<attributeGetterFunc>::Object *sender, attributeGetterFunc attributeGetter, attributeSignalFunc attributeSignal, valueGetterFunc valueGetter, valueSignalFunc valueSignal)
    {
        typedef QtPrivate::FunctionPointer<attributeGetterFunc> AttributeGetterType;
        typedef QtPrivate::FunctionPointer<attributeSignalFunc> AttributeSignalType;
        typedef QtPrivate::FunctionPointer<valueGetterFunc> ValueGetterType;
        typedef QtPrivate::FunctionPointer<valueSignalFunc> ValueSignalType;

        Q_STATIC_ASSERT_X(int(AttributeGetterType::ArgumentCount) == 0,
                          "The attribute getter can't have any arguments' ");

        Q_STATIC_ASSERT_X(int(ValueGetterType::ArgumentCount) == 0,
                          "The value getter can't have any arguments' ");

        Q_STATIC_ASSERT_X(int(ValueSignalType::ArgumentCount) == 1,
                          "The value signal needs to have exactly one argument");

        typedef QtPrivate::List<typename ValueGetterType::ReturnType, void> ValueGetterReturnType;
        typedef QtPrivate::List<QIviPropertyAttribute<T>, void> AttributeType;
        typedef QtPrivate::List<typename AttributeGetterType::ReturnType, void> AttributeGetterReturnType;

        Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<ValueGetterReturnType, typename ValueSignalType::Arguments>::value),
                          "Value getter return type and value signal arguments are not compatible.");


        Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<AttributeType, typename AttributeSignalType::Arguments>::value),
                          "Attribute type and attribute signal arguments are not compatible.");

        Q_STATIC_ASSERT_X((QtPrivate::CheckCompatibleArguments<AttributeGetterReturnType, typename AttributeSignalType::Arguments>::value),
                          "Attribute getter return type and attribute signal arguments are not compatible.");

        //Only Enums declared with Q_ENUM can be converted from an int without knowing the MetaObject where it is defined in
        //We also can't check this at compile time, so we check this at runtime.
        //We use a qFatal here to ensure that we die right when we are detecting this.
        int userType = qMetaTypeId<T>();
        QMetaType metaType(userType);
        if (metaType.flags() & QMetaType::IsEnumeration) {
            const QMetaObject *mo = metaType.metaObject();
            if (Q_UNLIKELY(!mo))
                //TODO Do we want to use a qFatal() here or just report a qCritical instead ?
                qFatal("The provided enum needs to be declared as Q_ENUM or Q_FLAG");
        }
        QIviPropertyFactory *prop = new QIviPropertyFactory(userType, sender,
                                                      new QtPrivate::QSlotObject<attributeGetterFunc, typename AttributeGetterType::Arguments, typename AttributeGetterType::ReturnType>(attributeGetter),
                                                      new QtPrivate::QSlotObject<valueGetterFunc, typename ValueGetterType::Arguments, typename ValueGetterType::ReturnType>(valueGetter));

        connect(sender, attributeSignal, prop, [prop](const QIviPropertyAttribute<T> &attribute) {prop->updateAttribute(attribute);});
        connect(sender, valueSignal, prop, [prop](const T &value) {prop->valueChanged(QVariant::fromValue(value));});

        return prop;
    }

    bool isAvailable() const Q_DECL_OVERRIDE
    {
        return callAttributeGetter().isAvailable();
    }

    QVariant minimumValue() const Q_DECL_OVERRIDE
    {
        return QVariant::fromValue<T>(callAttributeGetter().minimumValue());
    }
    QVariant maximumValue() const Q_DECL_OVERRIDE
    {
        return QVariant::fromValue<T>(callAttributeGetter().maximumValue());
    }
    QVariantList availableValues() const Q_DECL_OVERRIDE
    {
        return qtivi_convertAvailableValues(callAttributeGetter().availableValues());
    }

    QVariant value() const Q_DECL_OVERRIDE
    {
        T val;
        void *args[] = { reinterpret_cast<void*>(&val), QVariant().data() };
        valueGetter()->call(parent(), args);
        return qtivi_convertValue(val);
    }

private:
    QIviPropertyFactory(int userType, const QObject *receiver, QtPrivate::QSlotObjectBase *attGetter, QtPrivate::QSlotObjectBase *valGetter)
        : QIviProperty(userType, receiver, attGetter, valGetter)
    {
        registerConverters();
    }

    //We need to know the exact type here to allocate it before calling the getter.
    //The call function will just use the operator=() function to assign it to our local instance,
    //but will not create a new one.
    QIviPropertyAttribute<T> callAttributeGetter() const {
        QIviPropertyAttribute<T> attribute;
        void *args[] = { reinterpret_cast<void*>(&attribute), QVariant().data() };
        attributeGetter()->call(parent(), args);
        return attribute;
    }

    void updateAttribute(const QIviPropertyAttribute<T> &attribute)
    {
        Q_EMIT availableChanged(attribute.isAvailable());
        Q_EMIT minimumValueChanged(QVariant::fromValue<T>(attribute.minimumValue()));
        Q_EMIT maximumValueChanged(QVariant::fromValue<T>(attribute.maximumValue()));
        Q_EMIT availableValuesChanged(qtivi_convertAvailableValues(attribute.availableValues()));
    }

    //Just needed as we can't call the protected function directly in the create() functions
    void setValueSetterHelper(QtPrivate::QSlotObjectBase *valueSetter)
    {
        setValueSetter(valueSetter);
    }

    //If T is NOT convertible into a int e.g. QFlag
    template <typename F=T> Q_INLINE_TEMPLATE typename QtPrivate::QEnableIf<!QtPrivate::is_flag<F>::value, void>::Type registerConverters()
    {
    }

    //If T is convertible into a int e.g. QFlag
    template <typename F=T> Q_INLINE_TEMPLATE typename QtPrivate::QEnableIf<QtPrivate::is_flag<F>::value, void>::Type registerConverters()
    {
        if (!QMetaType::hasRegisteredConverterFunction<F, int>())
            QMetaType::registerConverter<F, int>(&F::operator int);
    }

    friend struct PropertyTestData;
};

QT_END_NAMESPACE

#endif // QIVIPROPERTYFACTORY_H