| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qservicelocator_p.h" |
| 5 | |
| 6 | #include <QtCore/QHash> |
| 7 | |
| 8 | #include <Qt3DCore/private/nullservices_p.h> |
| 9 | #include <Qt3DCore/private/qabstractserviceprovider_p.h> |
| 10 | #include <Qt3DCore/private/qdownloadhelperservice_p.h> |
| 11 | #include <Qt3DCore/private/qeventfilterservice_p.h> |
| 12 | #include <Qt3DCore/private/qtickclockservice_p.h> |
| 13 | #include <Qt3DCore/private/qsysteminformationservice_p.h> |
| 14 | |
| 15 | |
| 16 | QT_BEGIN_NAMESPACE |
| 17 | |
| 18 | namespace Qt3DCore { |
| 19 | |
| 20 | /* !\internal |
| 21 | \class Qt3DCore::QAbstractServiceProvider |
| 22 | \inmodule Qt3DCore |
| 23 | */ |
| 24 | |
| 25 | QAbstractServiceProvider::QAbstractServiceProvider(int type, const QString &description, QObject *parent) |
| 26 | : QObject(*new QAbstractServiceProviderPrivate(type, description), parent) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | /* \internal */ |
| 31 | QAbstractServiceProvider::QAbstractServiceProvider(QAbstractServiceProviderPrivate &dd, QObject *parent) |
| 32 | : QObject(dd, parent) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | QAbstractServiceProvider::~QAbstractServiceProvider() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | int QAbstractServiceProvider::type() const |
| 41 | { |
| 42 | Q_D(const QAbstractServiceProvider); |
| 43 | return d->m_type; |
| 44 | } |
| 45 | |
| 46 | QString QAbstractServiceProvider::description() const |
| 47 | { |
| 48 | Q_D(const QAbstractServiceProvider); |
| 49 | return d->m_description; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | class QAspectEngine; |
| 54 | |
| 55 | class QServiceLocatorPrivate |
| 56 | { |
| 57 | public: |
| 58 | QServiceLocatorPrivate(QAspectEngine *aspectEngine) |
| 59 | : m_systemInfo(aspectEngine) |
| 60 | , m_nonNullDefaultServices(0) |
| 61 | {} |
| 62 | |
| 63 | QHash<int, QAbstractServiceProvider *> m_services; |
| 64 | |
| 65 | QSystemInformationService m_systemInfo; |
| 66 | NullOpenGLInformationService m_nullOpenGLInfo; |
| 67 | QTickClockService m_defaultFrameAdvanceService; |
| 68 | QEventFilterService m_eventFilterService; |
| 69 | QDownloadHelperService m_downloadHelperService; |
| 70 | int m_nonNullDefaultServices; |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | /* !\internal |
| 75 | \class Qt3DCore::QServiceLocator |
| 76 | \inmodule Qt3DCore |
| 77 | \brief Service locator used by aspects to retrieve pointers to concrete service objects |
| 78 | |
| 79 | The Qt3DCore::QServiceLocator class can be used by aspects to obtain pointers to concrete |
| 80 | providers of abstract service interfaces. A subclass of Qt3DCore::QAbstractServiceProvider |
| 81 | encapsulates a service that can be provided by an aspect for other parts of the system. |
| 82 | For example, an aspect may wish to know the current frame number, or how many CPU cores |
| 83 | are available in the Qt3D tasking threadpool. |
| 84 | |
| 85 | Aspects or the Qt3DCore::QAspectEngine are able to register objects as providers of services. |
| 86 | The service locator itself can be accessed via the Qt3DCore::QAbstractAspect::services() |
| 87 | function. |
| 88 | |
| 89 | As a convenience, the service locator provides methods to access services provided by |
| 90 | built in Qt3D aspects. Currently these are Qt3DCore::QSystemInformationService and |
| 91 | Qt3DCore::QOpenGLInformationService. For such services, the service provider will never |
| 92 | return a null pointer. The default implementations of these services are simple null or |
| 93 | do nothing implementations. |
| 94 | */ |
| 95 | |
| 96 | /* |
| 97 | Creates an instance of QServiceLocator. |
| 98 | */ |
| 99 | QServiceLocator::QServiceLocator(QAspectEngine *aspectEngine) |
| 100 | : d_ptr(new QServiceLocatorPrivate(aspectEngine)) |
| 101 | { |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | Destroys a QServiceLocator object |
| 106 | */ |
| 107 | QServiceLocator::~QServiceLocator() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | Registers \a provider service provider for the service \a serviceType. This replaces any |
| 113 | existing provider for this service. The service provider does not take ownership |
| 114 | of the provider. |
| 115 | |
| 116 | \sa unregisterServiceProvider(), serviceCount(), service() |
| 117 | */ |
| 118 | void QServiceLocator::registerServiceProvider(int serviceType, QAbstractServiceProvider *provider) |
| 119 | { |
| 120 | Q_D(QServiceLocator); |
| 121 | d->m_services.insert(key: serviceType, value: provider); |
| 122 | if (serviceType < DefaultServiceCount) |
| 123 | ++(d->m_nonNullDefaultServices); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | Unregisters any existing provider for the \a serviceType. |
| 128 | |
| 129 | \sa registerServiceProvider() |
| 130 | */ |
| 131 | void QServiceLocator::unregisterServiceProvider(int serviceType) |
| 132 | { |
| 133 | Q_D(QServiceLocator); |
| 134 | int removedCount = d->m_services.remove(key: serviceType); |
| 135 | if (serviceType < DefaultServiceCount) |
| 136 | d->m_nonNullDefaultServices -= removedCount; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | Returns the number of registered services. |
| 141 | */ |
| 142 | int QServiceLocator::serviceCount() const |
| 143 | { |
| 144 | Q_D(const QServiceLocator); |
| 145 | return DefaultServiceCount + d->m_services.size() - d->m_nonNullDefaultServices; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | \fn T *Qt3DCore::QServiceLocator::service(int serviceType) |
| 150 | |
| 151 | Returns a pointer to the service provider for \a serviceType. If no provider |
| 152 | has been explicitly registered, this returns a null pointer for non-Qt3D provided |
| 153 | default services and a null pointer for non-default services. |
| 154 | |
| 155 | \sa registerServiceProvider() |
| 156 | |
| 157 | */ |
| 158 | |
| 159 | /* |
| 160 | Returns a pointer to a provider for the system information service. If no provider |
| 161 | has been explicitly registered for this service type, then a pointer to a null, do- |
| 162 | nothing service is returned. |
| 163 | */ |
| 164 | QSystemInformationService *QServiceLocator::systemInformation() |
| 165 | { |
| 166 | Q_D(QServiceLocator); |
| 167 | return static_cast<QSystemInformationService *>(d->m_services.value(key: SystemInformation, defaultValue: &d->m_systemInfo)); |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | Returns a pointer to a provider for the OpenGL information service. If no provider |
| 172 | has been explicitly registered for this service type, then a pointer to a null, do- |
| 173 | nothing service is returned. |
| 174 | */ |
| 175 | QOpenGLInformationService *QServiceLocator::openGLInformation() |
| 176 | { |
| 177 | Q_D(QServiceLocator); |
| 178 | return static_cast<QOpenGLInformationService *>(d->m_services.value(key: OpenGLInformation, defaultValue: &d->m_nullOpenGLInfo)); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | Returns a pointer to a provider for the frame advance service. If no provider |
| 183 | has been explicitly registered for this service type, then a pointer to a simple timer-based |
| 184 | service is returned. |
| 185 | */ |
| 186 | QAbstractFrameAdvanceService *QServiceLocator::frameAdvanceService() |
| 187 | { |
| 188 | Q_D(QServiceLocator); |
| 189 | return static_cast<QAbstractFrameAdvanceService *>(d->m_services.value(key: FrameAdvanceService, defaultValue: &d->m_defaultFrameAdvanceService)); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | Returns a pointer to a provider for the event filter service. If no |
| 194 | provider has been explicitly registered for this service type, then a |
| 195 | pointer to the default event filter service is returned. |
| 196 | */ |
| 197 | QEventFilterService *QServiceLocator::eventFilterService() |
| 198 | { |
| 199 | Q_D(QServiceLocator); |
| 200 | return static_cast<QEventFilterService *>(d->m_services.value(key: EventFilterService, defaultValue: &d->m_eventFilterService)); |
| 201 | } |
| 202 | |
| 203 | QDownloadHelperService *QServiceLocator::downloadHelperService() |
| 204 | { |
| 205 | Q_D(QServiceLocator); |
| 206 | return static_cast<QDownloadHelperService *>(d->m_services.value(key: DownloadHelperService, defaultValue: &d->m_downloadHelperService)); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | \internal |
| 211 | */ |
| 212 | QAbstractServiceProvider *QServiceLocator::_q_getServiceHelper(int type) |
| 213 | { |
| 214 | Q_D(QServiceLocator); |
| 215 | switch (type) { |
| 216 | case SystemInformation: |
| 217 | return systemInformation(); |
| 218 | case OpenGLInformation: |
| 219 | return openGLInformation(); |
| 220 | case FrameAdvanceService: |
| 221 | return frameAdvanceService(); |
| 222 | case EventFilterService: |
| 223 | return eventFilterService(); |
| 224 | case DownloadHelperService: |
| 225 | return downloadHelperService(); |
| 226 | default: |
| 227 | return d->m_services.value(key: type, defaultValue: nullptr); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | |
| 233 | QT_END_NAMESPACE |
| 234 | |
| 235 | #include "moc_qservicelocator_p.cpp" |
| 236 | |