![]() |
Carousel
|
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 QObject * | buildObject (const QString &className)=0 |
virtual QObject * | buildObject (const QString &className, const QString &tag)=0 |
template<typename TService > | |
TService * | locate () |
template<typename TService > | |
TService * | locate (const QString &tag) |
virtual QObject * | locateToObject (const QString &className)=0 |
virtual QObject * | locateToObject (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 |
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:
Register type dependencies:
Usage:
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.
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.
void IServiceLocator::bindType | ( | ) |
This is overload method. Binds an interface type with concrete class and empty tag with the locator.
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.
TInterface * IServiceLocator::buildInstance | ( | const QString & | tag | ) |
Creates and returns a TInterface pointer using specified tag and registered factory method, if any. Returns nullptr otherwise.
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.
Implemented in ServiceLocator.
|
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.
Implemented in ServiceLocator.
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.
Implemented in ServiceLocator.
|
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.
Implemented in ServiceLocator.
TService * IServiceLocator::locate | ( | ) |
Finds the service registered with TService type name and empty tag.
TService * IServiceLocator::locate | ( | const QString & | tag | ) |
Finds the service registered with TService type name and specified tag.
|
pure virtual |
Gets the services, registered with an empty tag.
Implemented in ServiceLocator.
|
pure virtual |
Gets the registered with tag services;.
Implemented in ServiceLocator.
void IServiceLocator::registerInstance | ( | TService * | instance | ) |
This is overload method. Registers a service instance with empty tag with the locator.
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:
To avoid this you should explicitly specify registered typename:
or register pointer to the service interface:
TService | The type which type name instance should be associated while registered in locator with. |
void IServiceLocator::registerType | ( | factoryMethod | method | ) |
This is overload method. Registers a type with factory method and empty tag with the locator.
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.
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.
TService | The type which type name will be found while. |
Returns found instance or nullptr, if instance with specified type and tag was not found.
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.
|
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
Implemented in ServiceLocator.
|
protectedpure virtual |
When overridden in derived classes finds the pointer associated with specified type id and specified tag in inner objects dictionary.
Implemented in ServiceLocator.
|
protectedpure virtual |
When overridden in derived classes registers a raw pointer with specified tag in inner objects dictionary.
className | The name of type which instance should be associated with. |
Implemented in ServiceLocator.
|
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).
className | The name of type which removed instance should be associated with. |
Implemented in ServiceLocator.
|
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.
className | The name of type which factory method should be associated with. |
Implemented in ServiceLocator.