Currently core services in MITK are provided by the class mitkCoreServices. This class makes heavy use of usServices. The usServices framework has a mechanism to check whether a service is in use by a module or not. This is achieved by an internal reference counting in usServices. The counter is increased by calling GetService<T> and decreased when calling UngetService<T>. The current implementation manages this functionality with an own pointer (CoreServicePointer) which takes a raw pointer of the service as input in the c'tor. In the d'tor the static method CoreServices::Unget() is called. CoreServices uses a static map with service references to handle the Unget calls. This handling has a lot of drawbacks. You need to search for the correct service and take care of thread safty during usage of the map. Furthermore a developer is not forced to use the CoreServicePointer which will lead to a lower confidence whether a service is in use or not.
Proposed solution:
We have two use cases:
- direct call on a service which is only used once in a scope:
GetService<ConcreteService>()->CallMethodOnService() (GetService is called before method call and UngetService is called after method has finished)
- more then one call on a service within a scope like:
{
ServicePointer<ConcreteService> pointer = GetService<ConcreteService>();
pointer->CallMethodOnService();
pointer->CallMethodOnServiceAgain();
} //Unget is called
Implementaion:
We write a new ServicePointer class which:
- takes a reference in the c'tor
- the reference and the service are stored as members of the ServicePointer
- = operator is hidden
- -> is overloaded and returns the raw service pointer
We write a templated method to retrieve a ServicePointer. To avoid several constructions and destruction (C++11 move semantics are not availably yet) during return we internally return the ServiceReference<ConcreteService>. For convenience we also implement templated GetService methods with arguments for service look ups. E.g. GetService<T>("key", 2).
We will also interduce an anlog implementation for lists of services.