summaryrefslogtreecommitdiffstats
path: root/src/interfaceframework/qifservicemanager.h
blob: 01ba44a73b2613c56ed591d927a54496f20e21ae (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
// Copyright (C) 2021 The Qt Company Ltd.
// Copyright (C) 2019 Luxoft Sweden AB
// Copyright (C) 2018 Pelagicore AG
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QIFSERVICEMANAGER_H
#define QIFSERVICEMANAGER_H

#include <QtCore/QAbstractListModel>
#include <QtQml/QQmlEngine>

#include <QtInterfaceFramework/qtifglobal.h>
#include <QtInterfaceFramework/QIfServiceObject>

QT_BEGIN_NAMESPACE

class QIfServiceObject;
class QIfServiceManagerPrivate;

class Q_QTINTERFACEFRAMEWORK_EXPORT QIfServiceObjectHandle
{
    Q_GADGET
    QML_VALUE_TYPE(serviceObjectHandle)
    QML_ADDED_IN_VERSION(6, 8)
    Q_PROPERTY(bool valid READ isValid FINAL)
    Q_PROPERTY(bool loaded READ isLoaded FINAL)
    Q_PROPERTY(QIfServiceObject *serviceObject READ serviceObject FINAL)

public:
    QIfServiceObjectHandle() = default;
    ~QIfServiceObjectHandle() = default;
    QIfServiceObjectHandle(const QIfServiceObjectHandle &) = default;

    bool isValid() const;
    bool isLoaded() const;
    QIfServiceObject *serviceObject() const;

    QIfServiceObjectHandle& operator=(const QIfServiceObjectHandle&) = default;
    bool operator==(QIfServiceObjectHandle other) const { return m_handle == other.m_handle; }
    bool operator!=(QIfServiceObjectHandle other) const { return !(*this == other); }

private:
    QIfServiceObjectHandle(void *handle);
    void *m_handle = nullptr;

    friend class QIfServiceManagerPrivate;
    friend class QIfServiceManager;
};

class Q_QTINTERFACEFRAMEWORK_EXPORT QIfServiceManager : public QAbstractListModel
{
    Q_OBJECT
    QML_NAMED_ELEMENT(ServiceManager)
    QML_SINGLETON

public:
    enum Roles {
        NameRole = Qt::DisplayRole,
        ServiceObjectRole = Qt::UserRole,
        InterfacesRole,
        ServiceObjectHandleRole,
    };

    enum SearchFlag {
        IncludeProductionBackends = 0x01,
        IncludeSimulationBackends = 0x02,
        IncludeAll = IncludeProductionBackends | IncludeSimulationBackends,
    };
    Q_DECLARE_FLAGS(SearchFlags, SearchFlag)
    Q_FLAG(SearchFlags)

    enum BackendType {
        ProductionBackend,
        SimulationBackend
    };
    Q_ENUM(BackendType)

    static QIfServiceManager *instance();
    static QIfServiceManager *create(QQmlEngine *, QJSEngine *);
    ~QIfServiceManager() override;

    Q_INVOKABLE QList<QIfServiceObject*> findServiceByInterface(const QString &interface, QIfServiceManager::SearchFlags searchFlags = IncludeAll, const QStringList &preferredBackends = QStringList());
    Q_REVISION(6, 8) Q_INVOKABLE QList<QIfServiceObjectHandle> findServiceHandleByInterface(const QString &interface, QIfServiceManager::SearchFlags searchFlags = IncludeAll, const QStringList &preferredBackends = QStringList());
    Q_INVOKABLE bool hasInterface(const QString &interface) const;

    bool registerService(QObject *serviceBackendInterface, const QStringList &interfaces, QIfServiceManager::BackendType backendType = ProductionBackend);
    void unloadAllBackends();

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    QVariant data(const QModelIndex &index, int role) const override;

    QHash<int, QByteArray> roleNames() const override;

    Q_REVISION(6, 8) Q_INVOKABLE void loadServiceObject(QIfServiceObjectHandle handle, bool async = false);

Q_SIGNALS:
    void serviceObjectLoaded(QIfServiceObjectHandle handle);

private:
    explicit QIfServiceManager(QObject *parent = nullptr);
    QIfServiceManagerPrivate * const d_ptr;
    Q_DECLARE_PRIVATE(QIfServiceManager)

    friend class QIfServiceObjectHandle;
};

QT_END_NAMESPACE

#endif // QIFSERVICEMANAGER_H