Carousel
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Pages
Public Member Functions | Protected Member Functions | List of all members
IServiceLocator Class Referenceabstract

The abstract IServiceLocator class provides central registry of the types and instances. More...

#include <IServiceLocator.h>

Inherited by ServiceLocator.

Public Member Functions

template<typename TInterface , typename TConcreteClass >
void bindType (const QString &tag)
 
template<typename TInterface , typename TConcreteClass >
void bindType ()
 
template<typename TInterface >
TInterface * buildInstance ()
 
template<typename TInterface >
TInterface * buildInstance (const QString &tag)
 
virtual QObjectbuildObject (const QString &className)=0
 
virtual QObjectbuildObject (const QString &className, const QString &tag)=0
 
template<typename TService >
TService * locate ()
 
template<typename TService >
TService * locate (const QString &tag)
 
virtual QObjectlocateToObject (const QString &className)=0
 
virtual QObjectlocateToObject (const QString &className, const QString &tag)=0
 
template<typename TService >
void registerInstance (TService *instance)
 
template<typename TService >
void registerInstance (TService *instance, const QString &tag)
 
template<typename TInterface >
void registerType (factoryMethod method)
 
template<typename TInterface >
void registerType (factoryMethod method, const QString &tag)
 
virtual QStringList services () const =0
 Gets the services, registered with an empty tag. More...
 
virtual QStringList services (const QString &tag) const =0
 Gets the registered with tag services;. More...
 
template<typename TService >
TService * unregisterInstance (const QString &tag)
 
template<typename TService >
TService * unregisterInstance ()
 

Protected Member Functions

virtual void * buildInstanceImpl (const QString &className, const QString &tag)=0
 
virtual void * getService (const QString &className, const QString &tag)=0
 
virtual void registerInstanceImpl (void *instance, const QString &className, const QString &tag)=0
 
virtual void registerTypeImpl (const QString &className, factoryMethod method, const QString &tag)=0
 
virtual void * unregisterInstanceImpl (const QString &className, const QString &tag)=0
 

Detailed Description

The abstract IServiceLocator class provides central registry of the types and instances.

The main IServiceLocator purpose is application/library sections separating, so the only link between them becomes the service registry. User types and instances (services) may be added during loading sequence, or by components and can be accessed from application.

To register your own global services or type factories, you should to override BootloaderBase::configureServiceLocator() function and register your own services (if you develop own application), or you can extend registry registering the component services and type factories at the IComponent::startup() member (if you develop extensions for the application). Further you can obtain current service through the template IServiceLocator::locate() member, where template parameter is the expected service typemor IServiceLocator::buildInstance() to create pure abstract class (interface) without knowledge of concrete class.

The services and type factories are unique in the registry, and this uniqueness is defined by the two parameters: registered class name (obtained through meta-object) and the tag. So, if you want to register existing service type or existing type factory you have to adding the unique tag for it. The service or type factory registered without special tag will be tagged with "" (empty string) in service locator implementation.

If you call CarouselBootloader::configureServiceLocator() from your derived loader class, the IComponentProvider, QMainWindow and IComponentManager will be registered.

Here is simple example of service locator usage:

* class IA : public QObject {
* Q_OBJECT
* public: virtual ~IA(){}
* };
*
* class A : public IA {
* Q_OBJECT
* public: A(){}
* };
*
* class IB {
* Q_OBJECT
* public: virtual ~IB(){}
* };
*
* class B : public IB {
* public:
* B(IA* a) : m_a(a) {}
*
* IA* m_a;
* };
*
* class IC : public QObject{
* Q_OBJECT
* public: virtual ~IC(){}
* };
*
* class C : public IC {
* Q_OBJECT
* public:
* C(IB* b, IA* a) : m_b(b) , m_a(a) {}
*
* IB* m_b;
* IA* m_a;
* };
*

Register type dependencies:

* IServiceLocator *locator = new ServiceLocator();
* locator->bindType<IA, A>();
*
* locator->registerType<IB>([locator]()
* {
* IA* a = locator->buildInstance<IA>();
* IB* b = new B(a);
* return b;
* });
*
* locator->registerType<IC>([locator]()
* {
* IA* a = locator->buildInstance<IA>();
* IB* b = new B(a);
* IC* c = new C(b, a);
* return c;
* });
*

Usage:

* IC *c1 = locator->buildInstance<IC>();
*

Here the cliens does not know what exactly instance is instantiated, and also they avoid all complex constructor injections.

Since qobject_cast() is used all services should be derived from the QObject and have Q_OBJECT macro.

Note
Does not take ownership of the services or of the created by factories types.

Member Function Documentation

template<typename TInterface , typename TConcreteClass >
void IServiceLocator::bindType ( const QString tag)

Binds an interface type with concrete class and specified tag with the locator. An instance of the concrete class will be returned on each buildInstance() method call.

It is a convinient method for the classes with parameterless constructors This method calls registerType() with specified TInterface and functor that just returns a new TConcreteClass.

template<typename TInterface , typename TConcreteClass >
void IServiceLocator::bindType ( )

This is overload method. Binds an interface type with concrete class and empty tag with the locator.

template<typename TInterface >
TInterface * IServiceLocator::buildInstance ( )

This is overload method. Creates and returns a TInterface pointer using empty tag and registered factory method, if any. Returns nullptr otherwise.

Note
Does not take ownership of the created pointer.
template<typename TInterface >
TInterface * IServiceLocator::buildInstance ( const QString tag)

Creates and returns a TInterface pointer using specified tag and registered factory method, if any. Returns nullptr otherwise.

Note
Does not take ownership of the created pointer.
virtual QObject* IServiceLocator::buildObject ( const QString className)
pure virtual

Creates and returns a className pointer using empty tag and registered factory method, if any. Returns nullptr otherwise. This may be usefull for scpipting, where no templates.

Returns
the corresponding service if such found. Null pointer otherwise.
Note
Does not take ownership of the created pointer.

Implemented in ServiceLocator.

virtual QObject* IServiceLocator::buildObject ( const QString className,
const QString tag 
)
pure virtual

Creates and returns a className pointer using specified tag and registered factory method, if any. Returns nullptr otherwise. This may be usefull for scpipting, where no templates.

Returns
the corresponding service if such found. Null pointer otherwise.
Note
Does not take ownership of the created pointer.

Implemented in ServiceLocator.

virtual QObject* IServiceLocator::locateToObject ( const QString className)
pure virtual

Finds the service registered with className and empty tag and returns a QObject pointer to it. This may be usefull for scpipting, where no templates.

Returns
the corresponding service if such found. Null pointer otherwise.

Implemented in ServiceLocator.

virtual QObject* IServiceLocator::locateToObject ( const QString className,
const QString tag 
)
pure virtual

Finds the service registered with className and empty tag and returns a QObject pointer to it. This may be usefull for scpipting, where no templates.

Returns
the corresponding service if such found. Null pointer otherwis

Implemented in ServiceLocator.

template<typename TService >
TService * IServiceLocator::locate ( )

Finds the service registered with TService type name and empty tag.

Returns
the corresponding service if such found. Null pointer otherwise.
template<typename TService >
TService * IServiceLocator::locate ( const QString tag)

Finds the service registered with TService type name and specified tag.

Returns
the corresponding service if such found. Null pointer otherwise.
virtual QStringList IServiceLocator::services ( ) const
pure virtual

Gets the services, registered with an empty tag.

Implemented in ServiceLocator.

virtual QStringList IServiceLocator::services ( const QString tag) const
pure virtual

Gets the registered with tag services;.

Implemented in ServiceLocator.

template<typename TService >
void IServiceLocator::registerInstance ( TService *  instance)

This is overload method. Registers a service instance with empty tag with the locator.

See Also
unregisterInstance()
template<typename TService >
void IServiceLocator::registerInstance ( TService *  instance,
const QString tag 
)

Registers a service instance with specified tag with the locator. It might be necessary to specify typename TService explicitly due to the static binding:

* MyService service;
* serviceLocator->registerInstance(&service); // will be registered "class MyService" - seems not what you expected
* IMyService *locatedService = serviceLocator->locate<IMyService>() // returns null, because asked "class IMyService" does not found
*

To avoid this you should explicitly specify registered typename:

* MyService service;
* serviceLocator->registerInstance<IMyService>(&service); // will be registered "class IMyService" - good
* IMyService *locatedService = serviceLocator->locate<IMyService>() // returns service pointer
*

or register pointer to the service interface:

* IMyService *serviceInterface = new MyService();
* serviceLocator->registerInstance(serviceInterface); // will be registered "class IMyService" - good
* IMyService *locatedService = serviceLocator->locate<IMyService>() // returns service pointer
*
Template Parameters
TServiceThe type which type name instance should be associated while registered in locator with.
See Also
unregisterInstance()
template<typename TInterface >
void IServiceLocator::registerType ( factoryMethod  method)

This is overload method. Registers a type with factory method and empty tag with the locator.

template<typename TInterface >
void IServiceLocator::registerType ( factoryMethod  method,
const QString tag 
)

Registers a type with factory method and specified tag with the locator. The method should return new instance of the TInterface type.

template<typename TService >
TService * IServiceLocator::unregisterInstance ( const QString tag)

Unregisters a service instance with specified type and tag from the locator (if any) and removes it from the locator map.

Template Parameters
TServiceThe type which type name will be found while.

Returns found instance or nullptr, if instance with specified type and tag was not found.

See Also
registerInstance()
template<typename TService >
TService * IServiceLocator::unregisterInstance ( )

This is overload method. Unregisters a service instance with specified type from the locator (if any) and removes it from the locator map.

See Also
registerInstance()
virtual void* IServiceLocator::buildInstanceImpl ( const QString className,
const QString tag 
)
protectedpure virtual

When overridden in derived classes finds the factory method associated with specified type id and specified tag in inner objects dictionary and creates instance of the interface using factory method

Returns
The raw pointer corresponded with specified interface id and tag if such found. Null pointer otherwise.

Implemented in ServiceLocator.

virtual void* IServiceLocator::getService ( const QString className,
const QString tag 
)
protectedpure virtual

When overridden in derived classes finds the pointer associated with specified type id and specified tag in inner objects dictionary.

Returns
The raw pointer corresponded with specified type id and tag if such found. Null pointer otherwise.

Implemented in ServiceLocator.

virtual void IServiceLocator::registerInstanceImpl ( void *  instance,
const QString className,
const QString tag 
)
protectedpure virtual

When overridden in derived classes registers a raw pointer with specified tag in inner objects dictionary.

Parameters
classNameThe name of type which instance should be associated with.

Implemented in ServiceLocator.

virtual void* IServiceLocator::unregisterInstanceImpl ( const QString className,
const QString tag 
)
protectedpure virtual

When overridden in derived classes unregisters (removes) a service instance with specified type id and tag from the inner objects dictionary (if any).

Parameters
classNameThe name of type which removed instance should be associated with.
Returns
The raw pointer corresponded with specified type id and tag if such found. Null pointer otherwise.

Implemented in ServiceLocator.

virtual void IServiceLocator::registerTypeImpl ( const QString className,
factoryMethod  method,
const QString tag 
)
protectedpure virtual

When overridden in derived classes binds an interface type id with specified factory method (that should create instance of interface) and with specified tag in inner objects dictionary.

Parameters
classNameThe name of type which factory method should be associated with.

Implemented in ServiceLocator.