The ServiceTracker
class simplifies using services from the framework's service registry.
More...
Public Types | |
typedef S | ServiceType |
The type of the service being tracked. More... | |
typedef TTT::TrackedType | T |
The type of the tracked object. More... | |
typedef ServiceReference< S > | ServiceReferenceType |
typedef std::map< ServiceReference< S >, T > | TrackingMap |
Public Member Functions | |
~ServiceTracker () | |
ServiceTracker (ModuleContext *context, const ServiceReferenceType &reference, ServiceTrackerCustomizer< S, T > *customizer=nullptr) | |
Create a ServiceTracker on the specified ServiceReference . More... | |
ServiceTracker (ModuleContext *context, const std::string &clazz, ServiceTrackerCustomizer< S, T > *customizer=nullptr) | |
Create a ServiceTracker on the specified class name. More... | |
ServiceTracker (ModuleContext *context, const LDAPFilter &filter, ServiceTrackerCustomizer< S, T > *customizer=nullptr) | |
Create a ServiceTracker on the specified LDAPFilter object. More... | |
ServiceTracker (ModuleContext *context, ServiceTrackerCustomizer< S, T > *customizer=nullptr) | |
Create a ServiceTracker on the class template argument S. More... | |
virtual void | Open () |
Open this ServiceTracker and begin tracking services. More... | |
virtual void | Close () |
Close this ServiceTracker . More... | |
template<class Rep , class Period > | |
T | WaitForService (const std::chrono::duration< Rep, Period > &rel_time) |
Wait for at least one service to be tracked by this ServiceTracker . More... | |
virtual std::vector< ServiceReferenceType > | GetServiceReferences () const |
Return a list of ServiceReference s for all services being tracked by this ServiceTracker . More... | |
virtual ServiceReferenceType | GetServiceReference () const |
Returns a ServiceReference for one of the services being tracked by this ServiceTracker . More... | |
virtual T | GetService (const ServiceReferenceType &reference) const |
Returns the service object for the specified ServiceReference if the specified referenced service is being tracked by this ServiceTracker . More... | |
virtual std::vector< T > | GetServices () const |
Return a list of service objects for all services being tracked by this ServiceTracker . More... | |
virtual T | GetService () const |
Returns a service object for one of the services being tracked by this ServiceTracker . More... | |
virtual void | Remove (const ServiceReferenceType &reference) |
Remove a service from this ServiceTracker . More... | |
virtual int | Size () const |
Return the number of services being tracked by this ServiceTracker . More... | |
virtual int | GetTrackingCount () const |
Returns the tracking count for this ServiceTracker . More... | |
virtual void | GetTracked (TrackingMap &tracked) const |
Return a sorted map of the ServiceReference s and service objects for all services being tracked by this ServiceTracker . More... | |
virtual bool | IsEmpty () const |
Return if this ServiceTracker is empty. More... | |
Protected Member Functions | |
T | AddingService (const ServiceReferenceType &reference) |
Default implementation of the ServiceTrackerCustomizer::AddingService method. More... | |
void | ModifiedService (const ServiceReferenceType &reference, T service) |
Default implementation of the ServiceTrackerCustomizer::ModifiedService method. More... | |
void | RemovedService (const ServiceReferenceType &reference, T service) |
Default implementation of the ServiceTrackerCustomizer::RemovedService method. More... | |
Protected Member Functions inherited from ServiceTrackerCustomizer< S, TTT::TrackedType > | |
virtual | ~ServiceTrackerCustomizer () |
virtual TrackedType | AddingService (const ServiceReferenceType &reference)=0 |
A service is being added to the ServiceTracker . More... | |
virtual void | ModifiedService (const ServiceReferenceType &reference, TrackedType service)=0 |
A service tracked by the ServiceTracker has been modified. More... | |
virtual void | RemovedService (const ServiceReferenceType &reference, TrackedType service)=0 |
A service tracked by the ServiceTracker has been removed. More... | |
Additional Inherited Members | |
Protected Types inherited from ServiceTrackerCustomizer< S, TTT::TrackedType > | |
typedef S | ServiceType |
typedef TTT::TrackedType | TrackedType |
typedef ServiceReference< ServiceType > | ServiceReferenceType |
The ServiceTracker
class simplifies using services from the framework's service registry.
A ServiceTracker
object is constructed with search criteria and a ServiceTrackerCustomizer
object. A ServiceTracker
can use a ServiceTrackerCustomizer
to customize the service objects to be tracked. The ServiceTracker
can then be opened to begin tracking all services in the framework's service registry that match the specified search criteria. The ServiceTracker
correctly handles all of the details of listening to ServiceEvent
s and getting and ungetting services.
The GetServiceReferences
method can be called to get references to the services being tracked. The GetService
and GetServices
methods can be called to get the service objects for the tracked service.
ServiceTracker
class is thread-safe. It does not call a ServiceTrackerCustomizer
while holding any locks. ServiceTrackerCustomizer
implementations must also be thread-safe.Customization of the services to be tracked requires a custom tracked type traits class if the custom tracked type is not a pointer type. To customize a tracked service using a custom type with value-semantics like
the custom tracked type traits class should look like this:
For a custom tracked type, a ServiceTrackerCustomizer is required, which knows how to associate the tracked service with the custom tracked type:
The custom tracking type traits class and customizer can now be used to instantiate a ServiceTracker:
If the custom tracked type is a pointer type, a suitable tracked type traits template is provided by the framework and only a ServiceTrackerCustomizer needs to be provided:
S | The type of the service being tracked. The type S* must be an assignable datatype. |
TTT | Type traits of the tracked object. The type traits class provides information about the customized service object, see TrackedTypeTraitsBase. |
typedef ServiceReference<S> ServiceTracker< S, TTT >::ServiceReferenceType |
typedef S ServiceTracker< S, TTT >::ServiceType |
The type of the service being tracked.
typedef TTT::TrackedType ServiceTracker< S, TTT >::T |
The type of the tracked object.
typedef std::map<ServiceReference<S>,T> ServiceTracker< S, TTT >::TrackingMap |
ServiceTracker< S, TTT >::~ServiceTracker | ( | ) |
ServiceTracker< S, TTT >::ServiceTracker | ( | ModuleContext * | context, |
const ServiceReferenceType & | reference, | ||
ServiceTrackerCustomizer< S, T > * | customizer = nullptr |
||
) |
Create a ServiceTracker
on the specified ServiceReference
.
The service referenced by the specified ServiceReference
will be tracked by this ServiceTracker
.
context | The ModuleContext against which the tracking is done. |
reference | The ServiceReference for the service to be tracked. |
customizer | The customizer object to call when services are added, modified, or removed in this ServiceTracker . If customizer is null , then this ServiceTracker will be used as the ServiceTrackerCustomizer and this ServiceTracker will call the ServiceTrackerCustomizer methods on itself. |
ServiceTracker< S, TTT >::ServiceTracker | ( | ModuleContext * | context, |
const std::string & | clazz, | ||
ServiceTrackerCustomizer< S, T > * | customizer = nullptr |
||
) |
Create a ServiceTracker
on the specified class name.
Services registered under the specified class name will be tracked by this ServiceTracker
.
context | The ModuleContext against which the tracking is done. |
clazz | The class name of the services to be tracked. |
customizer | The customizer object to call when services are added, modified, or removed in this ServiceTracker . If customizer is null , then this ServiceTracker will be used as the ServiceTrackerCustomizer and this ServiceTracker will call the ServiceTrackerCustomizer methods on itself. |
ServiceTracker< S, TTT >::ServiceTracker | ( | ModuleContext * | context, |
const LDAPFilter & | filter, | ||
ServiceTrackerCustomizer< S, T > * | customizer = nullptr |
||
) |
Create a ServiceTracker
on the specified LDAPFilter
object.
Services which match the specified LDAPFilter
object will be tracked by this ServiceTracker
.
context | The ModuleContext against which the tracking is done. |
filter | The LDAPFilter to select the services to be tracked. |
customizer | The customizer object to call when services are added, modified, or removed in this ServiceTracker . If customizer is null, then this ServiceTracker will be used as the ServiceTrackerCustomizer and this ServiceTracker will call the ServiceTrackerCustomizer methods on itself. |
ServiceTracker< S, TTT >::ServiceTracker | ( | ModuleContext * | context, |
ServiceTrackerCustomizer< S, T > * | customizer = nullptr |
||
) |
Create a ServiceTracker
on the class template argument S.
Services registered under the interface name of the class template argument S will be tracked by this ServiceTracker
.
context | The ModuleContext against which the tracking is done. |
customizer | The customizer object to call when services are added, modified, or removed in this ServiceTracker . If customizer is null, then this ServiceTracker will be used as the ServiceTrackerCustomizer and this ServiceTracker will call the ServiceTrackerCustomizer methods on itself. |
|
protected |
Default implementation of the ServiceTrackerCustomizer::AddingService
method.
This method is only called when this ServiceTracker
has been constructed with a null
ServiceTrackerCustomizer argument.
This implementation returns the result of calling GetService
on the ModuleContext
with which this ServiceTracker
was created passing the specified ServiceReference
.
This method can be overridden in a subclass to customize the service object to be tracked for the service being added. In that case, take care not to rely on the default implementation of removedService to unget the service.
reference | The reference to the service being added to this ServiceTracker . |
ServiceTracker
.
|
virtual |
Close this ServiceTracker
.
This method should be called when this ServiceTracker
should end the tracking of services.
This implementation calls GetServiceReferences() to get the list of tracked services to remove.
|
virtual |
Returns the service object for the specified ServiceReference
if the specified referenced service is being tracked by this ServiceTracker
.
reference | The reference to the desired service. |
null
if the service referenced by the specified ServiceReference
is not being tracked.
|
virtual |
Returns a service object for one of the services being tracked by this ServiceTracker
.
If any services are being tracked, this implementation returns the result of calling GetService(GetServiceReference())
.
null
if no services are being tracked.
|
virtual |
Returns a ServiceReference
for one of the services being tracked by this ServiceTracker
.
If multiple services are being tracked, the service with the highest ranking (as specified in its service.ranking
property) is returned. If there is a tie in ranking, the service with the lowest service ID (as specified in its service.id
property); that is, the service that was registered first is returned. This is the same algorithm used by ModuleContext::GetServiceReference()
.
This implementation calls GetServiceReferences() to get the list of references for the tracked services.
ServiceReference
for a tracked service. ServiceException | if no services are being tracked. |
|
virtual |
Return a list of ServiceReference
s for all services being tracked by this ServiceTracker
.
ServiceReference
s.
|
virtual |
Return a list of service objects for all services being tracked by this ServiceTracker
.
This implementation calls GetServiceReferences() to get the list of references for the tracked services and then calls GetService(const ServiceReference&) for each reference to get the tracked service object.
|
virtual |
Return a sorted map of the ServiceReference
s and service objects for all services being tracked by this ServiceTracker
.
The map is sorted in natural order of ServiceReference
. That is, the last entry is the service with the highest ranking and the lowest service id.
tracked | A TrackingMap with the ServiceReference s and service objects for all services being tracked by this ServiceTracker . If no services are being tracked, then the returned map is empty. |
|
virtual |
Returns the tracking count for this ServiceTracker
.
The tracking count is initialized to 0 when this ServiceTracker
is opened. Every time a service is added, modified or removed from this ServiceTracker
, the tracking count is incremented.
The tracking count can be used to determine if this ServiceTracker
has added, modified or removed a service by comparing a tracking count value previously collected with the current tracking count value. If the value has not changed, then no service has been added, modified or removed from this ServiceTracker
since the previous tracking count was collected.
ServiceTracker
or -1 if this ServiceTracker
is not open.
|
virtual |
Return if this ServiceTracker
is empty.
true
if this ServiceTracker
is not tracking any services.
|
protected |
Default implementation of the ServiceTrackerCustomizer::ModifiedService
method.
This method is only called when this ServiceTracker
has been constructed with a null
ServiceTrackerCustomizer argument.
This implementation does nothing.
reference | The reference to modified service. |
service | The service object for the modified service. |
|
virtual |
Open this ServiceTracker
and begin tracking services.
Services which match the search criteria specified when this ServiceTracker
was created are now tracked by this ServiceTracker
.
std::logic_error | If the ModuleContext with which this ServiceTracker was created is no longer valid. |
|
virtual |
Remove a service from this ServiceTracker
.
The specified service will be removed from this ServiceTracker
. If the specified service was being tracked then the ServiceTrackerCustomizer::RemovedService
method will be called for that service.
reference | The reference to the service to be removed. |
|
protected |
Default implementation of the ServiceTrackerCustomizer::RemovedService
method.
This method is only called when this ServiceTracker
has been constructed with a null
ServiceTrackerCustomizer argument.
This implementation calls UngetService
, on the ModuleContext
with which this ServiceTracker
was created, passing the specified ServiceReference
.
This method can be overridden in a subclass. If the default implementation of AddingService method was used, this method must unget the service.
reference | The reference to removed service. |
service | The service object for the removed service. |
|
virtual |
Return the number of services being tracked by this ServiceTracker
.
T ServiceTracker< S, TTT >::WaitForService | ( | const std::chrono::duration< Rep, Period > & | rel_time | ) |
Wait for at least one service to be tracked by this ServiceTracker
.
This method will also return when this ServiceTracker
is closed.
It is strongly recommended that WaitForService
is not used during the calling of the ModuleActivator
methods. ModuleActivator
methods are expected to complete in a short period of time.
This implementation calls GetService() to determine if a service is being tracked.