1 /*
2 objectbroker.h
3
4 This file is part of GammaRay, the Qt application inspection and
5 manipulation tool.
6
7 Copyright (C) 2013-2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
8 Author: Volker Krause <volker.krause@kdab.com>
9
10 Licensees holding valid commercial KDAB GammaRay licenses may use this file in
11 accordance with GammaRay Commercial License Agreement provided with the Software.
12
13 Contact info@kdab.com if any conditions of this licensing are not clear to you.
14
15 This program is free software; you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 */
28
29 #ifndef GAMMARAY_OBJECTBROKER_H
30 #define GAMMARAY_OBJECTBROKER_H
31
32 #include "gammaray_common_export.h"
33
34 #include <QObject>
35
36 QT_BEGIN_NAMESPACE
37 class QItemSelectionModel;
38 class QAbstractItemModel;
39 QT_END_NAMESPACE
40
41 namespace GammaRay {
42 /*! Retrieve/expose objects independent of whether using in-process or out-of-process UI. */
43 namespace ObjectBroker {
44 /*! Register a newly created QObject under the given name. */
45 GAMMARAY_COMMON_EXPORT void registerObject(const QString &name, QObject *object);
46 template<class T>
registerObject(QObject * object)47 void registerObject(QObject *object)
48 {
49 const QString interfaceName = QString::fromUtf8(qobject_interface_iid<T>());
50 registerObject(interfaceName, object);
51 }
52
53 /*! Checks whether an object with the given name is registered already. */
54 GAMMARAY_COMMON_EXPORT bool hasObject(const QString &name);
55
56 /*! Retrieve object by name. */
57 GAMMARAY_COMMON_EXPORT QObject *objectInternal(const QString &name,
58 const QByteArray &type = QByteArray());
59
60 /*!
61 * Retrieve an object by name implementing interface @p T.
62 *
63 * Use this if multiple objects of the given type have been registered.
64 * Otherwise the function below is simpler and more failsafe.
65 *
66 * @note The "T = nullptr" is just to ensure a pointer type is given.
67 */
68 template<class T>
69 T object(const QString &name, T = nullptr)
70 {
71 T ret = qobject_cast<T>(objectInternal(name, QByteArray(qobject_interface_iid<T>())));
72 Q_ASSERT(ret);
73 return ret;
74 }
75
76 /*!
77 * Retrieve object implementing interface @p T.
78 *
79 * This only works if a single type was registered implementing this interface
80 * using qobject_interface_iid as object name.
81 *
82 * In most cases this is the simplest way for tools to get an object.
83 *
84 * @note The "T = nullptr" is just to ensure a pointer type is given.
85 */
86 template<class T>
87 T object(T = nullptr)
88 {
89 const QByteArray interfaceName(qobject_interface_iid<T>());
90 T ret = qobject_cast<T>(objectInternal(QString::fromUtf8(interfaceName), interfaceName));
91 Q_ASSERT(ret);
92 return ret;
93 }
94
95 typedef QObject *(*ClientObjectFactoryCallback)(const QString &, QObject *parent);
96
97 /*! Register a callback for a factory to create remote object stubs for the given type. */
98 GAMMARAY_COMMON_EXPORT void registerClientObjectFactoryCallbackInternal(const QByteArray &type,
99 ClientObjectFactoryCallback callback);
100
101 /*!
102 * Register a callback for a factory of a given interface to create remote object stubs for the given type.
103 *
104 * @note The "T = nullptr" is just to ensure a pointer type is given.
105 */
106 template<class T>
107 void registerClientObjectFactoryCallback(ClientObjectFactoryCallback callback, T = nullptr)
108 {
109 registerClientObjectFactoryCallbackInternal(QByteArray(qobject_interface_iid<T>()), callback);
110 }
111
112 /*!
113 * Register a newly created model with the given name.
114 *
115 * @note This must not be called directly by anything but the probe/server side.
116 * User code must use Probe::registerModel() instead!
117 */
118 GAMMARAY_COMMON_EXPORT void registerModelInternal(const QString &name, QAbstractItemModel *model);
119
120 /*! Retrieve a model by name. */
121 GAMMARAY_COMMON_EXPORT QAbstractItemModel *model(const QString &name);
122
123 typedef QAbstractItemModel *(*ModelFactoryCallback)(const QString &);
124
125 /*! Set a callback for the case that a model was requested but had not been registered before. */
126 GAMMARAY_COMMON_EXPORT void setModelFactoryCallback(ModelFactoryCallback callback);
127
128 /*! Register a newly created selection model. */
129 GAMMARAY_COMMON_EXPORT void registerSelectionModel(QItemSelectionModel *selectionModel);
130 /*! Unregisters a selection model. You have to take care of deletion yourself. */
131 GAMMARAY_COMMON_EXPORT void unregisterSelectionModel(QItemSelectionModel *selectionModel);
132 /*! Checks whether a selection model for the given @p model is registered already. */
133 GAMMARAY_COMMON_EXPORT bool hasSelectionModel(QAbstractItemModel *model);
134
135 /*! Retrieve the selection model for @p model. */
136 GAMMARAY_COMMON_EXPORT QItemSelectionModel *selectionModel(QAbstractItemModel *model);
137
138 typedef QItemSelectionModel *(*selectionModelFactoryCallback)(QAbstractItemModel *);
139
140 /*! Set a callback for the case that a selection model was requested but had not been registered before. */
141 GAMMARAY_COMMON_EXPORT void setSelectionModelFactoryCallback(selectionModelFactoryCallback callback);
142
143 /*! Clear all registered objects.
144 *
145 * This also destroys all objects created by factory methods registered here, but not externally
146 * created objects that have just been registered here.
147 * Useful when the probe is deleted, or the client lost the connection.
148 */
149 GAMMARAY_COMMON_EXPORT void clear();
150 }
151 }
152
153 #endif // GAMMARAY_OBJECTBROKER_H
154