1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qservicelocator_p.h"
41
42#include <QtCore/QHash>
43
44#include <Qt3DCore/private/nullservices_p.h>
45#include <Qt3DCore/private/qabstractserviceprovider_p.h>
46#include <Qt3DCore/private/qdownloadhelperservice_p.h>
47#include <Qt3DCore/private/qeventfilterservice_p.h>
48#include <Qt3DCore/private/qtickclockservice_p.h>
49#include <Qt3DCore/private/qsysteminformationservice_p.h>
50
51
52QT_BEGIN_NAMESPACE
53
54namespace Qt3DCore {
55
56/* !\internal
57 \class Qt3DCore::QAbstractServiceProvider
58 \inmodule Qt3DCore
59*/
60
61QAbstractServiceProvider::QAbstractServiceProvider(int type, const QString &description, QObject *parent)
62 : QObject(*new QAbstractServiceProviderPrivate(type, description), parent)
63{
64}
65
66/* \internal */
67QAbstractServiceProvider::QAbstractServiceProvider(QAbstractServiceProviderPrivate &dd, QObject *parent)
68 : QObject(dd, parent)
69{
70}
71
72QAbstractServiceProvider::~QAbstractServiceProvider()
73{
74}
75
76int QAbstractServiceProvider::type() const
77{
78 Q_D(const QAbstractServiceProvider);
79 return d->m_type;
80}
81
82QString QAbstractServiceProvider::description() const
83{
84 Q_D(const QAbstractServiceProvider);
85 return d->m_description;
86}
87
88
89class QAspectEngine;
90
91class QServiceLocatorPrivate
92{
93public:
94 QServiceLocatorPrivate(QAspectEngine *aspectEngine)
95 : m_systemInfo(aspectEngine)
96 , m_nonNullDefaultServices(0)
97 {}
98
99 QHash<int, QAbstractServiceProvider *> m_services;
100
101 QSystemInformationService m_systemInfo;
102 NullOpenGLInformationService m_nullOpenGLInfo;
103 QTickClockService m_defaultFrameAdvanceService;
104 QEventFilterService m_eventFilterService;
105 QDownloadHelperService m_downloadHelperService;
106 int m_nonNullDefaultServices;
107};
108
109
110/* !\internal
111 \class Qt3DCore::QServiceLocator
112 \inmodule Qt3DCore
113 \brief Service locator used by aspects to retrieve pointers to concrete service objects
114
115 The Qt3DCore::QServiceLocator class can be used by aspects to obtain pointers to concrete
116 providers of abstract service interfaces. A subclass of Qt3DCore::QAbstractServiceProvider
117 encapsulates a service that can be provided by an aspect for other parts of the system.
118 For example, an aspect may wish to know the current frame number, or how many CPU cores
119 are available in the Qt3D tasking threadpool.
120
121 Aspects or the Qt3DCore::QAspectEngine are able to register objects as providers of services.
122 The service locator itself can be accessed via the Qt3DCore::QAbstractAspect::services()
123 function.
124
125 As a convenience, the service locator provides methods to access services provided by
126 built in Qt3D aspects. Currently these are Qt3DCore::QSystemInformationService and
127 Qt3DCore::QOpenGLInformationService. For such services, the service provider will never
128 return a null pointer. The default implementations of these services are simple null or
129 do nothing implementations.
130*/
131
132/*
133 Creates an instance of QServiceLocator.
134*/
135QServiceLocator::QServiceLocator(QAspectEngine *aspectEngine)
136 : d_ptr(new QServiceLocatorPrivate(aspectEngine))
137{
138}
139
140/*
141 Destroys a QServiceLocator object
142*/
143QServiceLocator::~QServiceLocator()
144{
145}
146
147/*
148 Registers \a provider service provider for the service \a serviceType. This replaces any
149 existing provider for this service. The service provider does not take ownership
150 of the provider.
151
152 \sa unregisterServiceProvider(), serviceCount(), service()
153*/
154void QServiceLocator::registerServiceProvider(int serviceType, QAbstractServiceProvider *provider)
155{
156 Q_D(QServiceLocator);
157 d->m_services.insert(akey: serviceType, avalue: provider);
158 if (serviceType < DefaultServiceCount)
159 ++(d->m_nonNullDefaultServices);
160}
161
162/*
163 Unregisters any existing provider for the \a serviceType.
164
165 \sa registerServiceProvider()
166 */
167void QServiceLocator::unregisterServiceProvider(int serviceType)
168{
169 Q_D(QServiceLocator);
170 int removedCount = d->m_services.remove(akey: serviceType);
171 if (serviceType < DefaultServiceCount)
172 d->m_nonNullDefaultServices -= removedCount;
173}
174
175/*
176 Returns the number of registered services.
177 */
178int QServiceLocator::serviceCount() const
179{
180 Q_D(const QServiceLocator);
181 return DefaultServiceCount + d->m_services.size() - d->m_nonNullDefaultServices;
182}
183
184/*
185 \fn T *Qt3DCore::QServiceLocator::service(int serviceType)
186
187 Returns a pointer to the service provider for \a serviceType. If no provider
188 has been explicitly registered, this returns a null pointer for non-Qt3D provided
189 default services and a null pointer for non-default services.
190
191 \sa registerServiceProvider()
192
193*/
194
195/*
196 Returns a pointer to a provider for the system information service. If no provider
197 has been explicitly registered for this service type, then a pointer to a null, do-
198 nothing service is returned.
199*/
200QSystemInformationService *QServiceLocator::systemInformation()
201{
202 Q_D(QServiceLocator);
203 return static_cast<QSystemInformationService *>(d->m_services.value(akey: SystemInformation, adefaultValue: &d->m_systemInfo));
204}
205
206/*
207 Returns a pointer to a provider for the OpenGL information service. If no provider
208 has been explicitly registered for this service type, then a pointer to a null, do-
209 nothing service is returned.
210*/
211QOpenGLInformationService *QServiceLocator::openGLInformation()
212{
213 Q_D(QServiceLocator);
214 return static_cast<QOpenGLInformationService *>(d->m_services.value(akey: OpenGLInformation, adefaultValue: &d->m_nullOpenGLInfo));
215}
216
217/*
218 Returns a pointer to a provider for the frame advance service. If no provider
219 has been explicitly registered for this service type, then a pointer to a simple timer-based
220 service is returned.
221*/
222QAbstractFrameAdvanceService *QServiceLocator::frameAdvanceService()
223{
224 Q_D(QServiceLocator);
225 return static_cast<QAbstractFrameAdvanceService *>(d->m_services.value(akey: FrameAdvanceService, adefaultValue: &d->m_defaultFrameAdvanceService));
226}
227
228/*
229 Returns a pointer to a provider for the event filter service. If no
230 provider has been explicitly registered for this service type, then a
231 pointer to the default event filter service is returned.
232 */
233QEventFilterService *QServiceLocator::eventFilterService()
234{
235 Q_D(QServiceLocator);
236 return static_cast<QEventFilterService *>(d->m_services.value(akey: EventFilterService, adefaultValue: &d->m_eventFilterService));
237}
238
239QDownloadHelperService *QServiceLocator::downloadHelperService()
240{
241 Q_D(QServiceLocator);
242 return static_cast<QDownloadHelperService *>(d->m_services.value(akey: DownloadHelperService, adefaultValue: &d->m_downloadHelperService));
243}
244
245/*
246 \internal
247*/
248QAbstractServiceProvider *QServiceLocator::_q_getServiceHelper(int type)
249{
250 Q_D(QServiceLocator);
251 switch (type) {
252 case SystemInformation:
253 return systemInformation();
254 case OpenGLInformation:
255 return openGLInformation();
256 case FrameAdvanceService:
257 return frameAdvanceService();
258 case EventFilterService:
259 return eventFilterService();
260 case DownloadHelperService:
261 return downloadHelperService();
262 default:
263 return d->m_services.value(akey: type, adefaultValue: nullptr);
264 }
265}
266
267}
268
269QT_END_NAMESPACE
270

source code of qt3d/src/core/services/qservicelocator.cpp