|
@@ -32,141 +32,133 @@
|
|
|
#include "ctkPluginFrameworkContext_p.h"
|
|
|
|
|
|
|
|
|
- ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
|
|
|
- : ref(1), registration(reg)
|
|
|
+ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
|
|
|
+ : ref(1), registration(reg)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+QObject* ctkServiceReferencePrivate::getService(ctkPlugin* plugin)
|
|
|
+{
|
|
|
+ QObject* s = 0;
|
|
|
{
|
|
|
- }
|
|
|
-
|
|
|
- QObject* ctkServiceReferencePrivate::getService(ctkPlugin* plugin)
|
|
|
- {
|
|
|
- QObject* s = 0;
|
|
|
+ QMutexLocker lock(®istration->propsLock);
|
|
|
+ if (registration->available)
|
|
|
{
|
|
|
- QMutexLocker lock(®istration->propsLock);
|
|
|
- if (registration->available)
|
|
|
+ int count = registration->dependents.value(plugin);
|
|
|
+ if (count == 0)
|
|
|
{
|
|
|
- int count = registration->dependents.value(plugin);
|
|
|
- if (count == 0)
|
|
|
+ QStringList classes =
|
|
|
+ registration->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
|
|
|
+ registration->dependents[plugin] = 1;
|
|
|
+ if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
|
|
|
{
|
|
|
- QStringList classes =
|
|
|
- registration->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
|
|
|
- registration->dependents[plugin] = 1;
|
|
|
- if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
|
|
|
+ try
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- s = serviceFactory->getService(plugin, registration->q_func());
|
|
|
- }
|
|
|
- catch (const std::exception& pe)
|
|
|
- {
|
|
|
- ctkServiceException se("ctkServiceFactory throw an exception",
|
|
|
- ctkServiceException::FACTORY_EXCEPTION, pe);
|
|
|
- plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
+ s = serviceFactory->getService(plugin, registration->q_func());
|
|
|
+ }
|
|
|
+ catch (const std::exception& pe)
|
|
|
+ {
|
|
|
+ ctkServiceException se("ctkServiceFactory throw an exception",
|
|
|
+ ctkServiceException::FACTORY_EXCEPTION, pe);
|
|
|
+ plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
(registration->plugin->q_func(), se);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- if (s == 0) {
|
|
|
- ctkServiceException se("ctkServiceFactory produced null",
|
|
|
- ctkServiceException::FACTORY_ERROR);
|
|
|
- plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ if (s == 0) {
|
|
|
+ ctkServiceException se("ctkServiceFactory produced null",
|
|
|
+ ctkServiceException::FACTORY_ERROR);
|
|
|
+ plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
(registration->plugin->q_func(), se);
|
|
|
- return 0;
|
|
|
- }
|
|
|
- for (QStringListIterator i(classes); i.hasNext(); )
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ for (QStringListIterator i(classes); i.hasNext(); )
|
|
|
+ {
|
|
|
+ QString cls = i.next();
|
|
|
+ if (!registration->plugin->fwCtx->services.checkServiceClass(s, cls))
|
|
|
{
|
|
|
- QString cls = i.next();
|
|
|
- if (!registration->plugin->fwCtx->services.checkServiceClass(s, cls))
|
|
|
- {
|
|
|
- ctkServiceException se(QString("ctkServiceFactory produced an object ") +
|
|
|
+ ctkServiceException se(QString("ctkServiceFactory produced an object ") +
|
|
|
"that did not implement: " + cls,
|
|
|
ctkServiceException::FACTORY_ERROR);
|
|
|
- plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
+ plugin->d_func()->fwCtx->listeners.frameworkError
|
|
|
(registration->plugin->q_func(), se);
|
|
|
- return 0;
|
|
|
- }
|
|
|
+ return 0;
|
|
|
}
|
|
|
- registration->serviceInstances.insert(plugin, s);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- s = registration->getService();
|
|
|
}
|
|
|
+ registration->serviceInstances.insert(plugin, s);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- registration->dependents.insert(plugin, count + 1);
|
|
|
- if (qobject_cast<ctkServiceFactory*>(registration->getService()))
|
|
|
- {
|
|
|
- s = registration->serviceInstances.value(plugin);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- s = registration->getService();
|
|
|
- }
|
|
|
+ s = registration->getService();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ registration->dependents.insert(plugin, count + 1);
|
|
|
+ if (qobject_cast<ctkServiceFactory*>(registration->getService()))
|
|
|
+ {
|
|
|
+ s = registration->serviceInstances.value(plugin);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ s = registration->getService();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return s;
|
|
|
}
|
|
|
+ return s;
|
|
|
+}
|
|
|
|
|
|
- /**
|
|
|
- * Unget the service object.
|
|
|
- *
|
|
|
- * @param bundle Bundle who wants remove service.
|
|
|
- * @param checkRefCounter If true decrement refence counter and remove service
|
|
|
- * if we reach zero. If false remove service without
|
|
|
- * checking refence counter.
|
|
|
- * @return True if service was remove or false if only refence counter was
|
|
|
- * decremented.
|
|
|
- */
|
|
|
- bool ctkServiceReferencePrivate::ungetService(ctkPlugin* plugin, bool checkRefCounter)
|
|
|
- {
|
|
|
- QMutexLocker lock(®istration->propsLock);
|
|
|
- bool hadReferences = false;
|
|
|
- bool removeService = false;
|
|
|
+bool ctkServiceReferencePrivate::ungetService(ctkPlugin* plugin, bool checkRefCounter)
|
|
|
+{
|
|
|
+ QMutexLocker lock(®istration->propsLock);
|
|
|
+ bool hadReferences = false;
|
|
|
+ bool removeService = false;
|
|
|
|
|
|
- int count= registration->dependents.value(plugin);
|
|
|
- if (count > 0)
|
|
|
- {
|
|
|
- hadReferences = true;
|
|
|
- }
|
|
|
+ int count= registration->dependents.value(plugin);
|
|
|
+ if (count > 0)
|
|
|
+ {
|
|
|
+ hadReferences = true;
|
|
|
+ }
|
|
|
|
|
|
- if(checkRefCounter)
|
|
|
+ if(checkRefCounter)
|
|
|
+ {
|
|
|
+ if (count > 1)
|
|
|
{
|
|
|
- if (count > 1)
|
|
|
- {
|
|
|
- registration->dependents[plugin] = count - 1;
|
|
|
- }
|
|
|
- else if(count == 1)
|
|
|
- {
|
|
|
- removeService = true;
|
|
|
- }
|
|
|
+ registration->dependents[plugin] = count - 1;
|
|
|
}
|
|
|
- else
|
|
|
+ else if(count == 1)
|
|
|
{
|
|
|
removeService = true;
|
|
|
}
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ removeService = true;
|
|
|
+ }
|
|
|
|
|
|
- if (removeService)
|
|
|
+ if (removeService)
|
|
|
+ {
|
|
|
+ QObject* sfi = registration->serviceInstances[plugin];
|
|
|
+ registration->serviceInstances.remove(plugin);
|
|
|
+ if (sfi != 0)
|
|
|
{
|
|
|
- QObject* sfi = registration->serviceInstances[plugin];
|
|
|
- registration->serviceInstances.remove(plugin);
|
|
|
- if (sfi != 0)
|
|
|
+ try
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- qobject_cast<ctkServiceFactory*>(registration->getService())->ungetService(plugin,
|
|
|
- registration->q_func(), sfi);
|
|
|
- }
|
|
|
- catch (const std::exception& e)
|
|
|
- {
|
|
|
- plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
|
|
|
- }
|
|
|
+ qobject_cast<ctkServiceFactory*>(registration->getService())->ungetService(plugin,
|
|
|
+ registration->q_func(), sfi);
|
|
|
+ }
|
|
|
+ catch (const std::exception& e)
|
|
|
+ {
|
|
|
+ plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
|
|
|
}
|
|
|
- registration->dependents.remove(plugin);
|
|
|
}
|
|
|
-
|
|
|
- return hadReferences;
|
|
|
+ registration->dependents.remove(plugin);
|
|
|
}
|
|
|
|
|
|
+ return hadReferences;
|
|
|
+}
|
|
|
|
|
|
-
|
|
|
+ServiceProperties ctkServiceReferencePrivate::getProperties() const
|
|
|
+{
|
|
|
+ return registration->properties;
|
|
|
+}
|