ServiceObjects

template<class S>
class ServiceObjects : private cppmicroservices::ServiceObjectsBase
#include <cppmicroservices/ServiceObjects.h>

Allows multiple service objects for a service to be obtained.

For services with prototype scope, multiple service objects for the service can be obtained. For services with singleton or bundle scope, only one, use-counted service object is available. Any unreleased service objects obtained from this ServiceObjects object are automatically released by the framework when the bundles associated with the BundleContext used to create this ServiceObjects object is stopped.

Template Parameters:

S – Type of Service.

Public Functions

ServiceObjects(ServiceObjects const &other) = delete
ServiceObjects &operator=(ServiceObjects const &other) = delete
inline ServiceObjects(ServiceObjects &&other)
inline ServiceObjects &operator=(ServiceObjects &&other)
inline std::shared_ptr<S> GetService() const

Returns a service object for the referenced service.

This ServiceObjects object can be used to obtain multiple service objects for the referenced service if the service has prototype scope. If the referenced service has singleton or bundle scope, this method behaves the same as calling the BundleContext::GetService(const ServiceReferenceBase&) method for the referenced service. That is, only one, use-counted service object is available from this ServiceObjects object.

This method will always return nullptr when the referenced service has been unregistered.

For a prototype scope service, the following steps are taken to get the service object:

  1. If the referenced service has been unregistered, nullptr is returned.

  2. The PrototypeServiceFactory::GetService(const Bundle&, const ServiceRegistrationBase&) method is called to create a service object for the caller.

  3. If the service object (an instance of InterfaceMap) returned by the PrototypeServiceFactory object is empty, does not contain all the interfaces named when the service was registered or the PrototypeServiceFactory object throws an exception, nullptr is returned and a warning message is issued.

  4. The service object is returned.

Throws:

std::logic_error – If the BundleContext used to create this ServiceObjects object is no longer valid.

Returns:

A shared_ptr to the service object. The returned shared_ptr is empty if the service is not registered, the service object returned by a ServiceFactory does not contain all the classes under which it was registered or the ServiceFactory threw an exception.

inline ServiceReference<S> GetServiceReference() const

Returns the ServiceReference for this ServiceObjects object.

Returns:

The ServiceReference for this ServiceObjects object.

template<>
class ServiceObjects<void> : private cppmicroservices::ServiceObjectsBase, private cppmicroservices::ServiceObjectsBase
#include <cppmicroservices/ServiceObjects.h>

Allows multiple service objects for a service to be obtained.

This is a specialization of the ServiceObjects class template for void, which maps to all service interface types.

See also

ServiceObjects

Public Functions

ServiceObjects(ServiceObjects const &other) = delete
ServiceObjects &operator=(ServiceObjects const &other) = delete
ServiceObjects(ServiceObjects &&other) noexcept
ServiceObjects &operator=(ServiceObjects &&other) noexcept
InterfaceMapConstPtr GetService() const

Returns a service object as a InterfaceMap instance for the referenced service.

This method is the same as ServiceObjects<S>::GetService() except for the return type. Further, this method will always return an empty InterfaceMap object when the referenced service has been unregistered.

See also

ServiceObjects<S>::GetService()

Throws:

std::logic_error – If the BundleContext used to create this ServiceObjects object is no longer valid.

Returns:

A InterfaceMapConstPtr object for the referenced service, which is empty if the service is not registered, the InterfaceMap returned by a ServiceFactory does not contain all the classes under which the service object was registered or the ServiceFactory threw an exception.

ServiceReferenceU GetServiceReference() const

Returns the ServiceReference for this ServiceObjects object.

Returns:

The ServiceReference for this ServiceObjects object.

ServiceObjects(ServiceObjects const &other) = delete
inline ServiceObjects(ServiceObjects &&other)
ServiceObjects &operator=(ServiceObjects const &other) = delete
inline ServiceObjects &operator=(ServiceObjects &&other)
inline std::shared_ptr<void> GetService() const

Returns a service object for the referenced service.

This ServiceObjects object can be used to obtain multiple service objects for the referenced service if the service has prototype scope. If the referenced service has singleton or bundle scope, this method behaves the same as calling the BundleContext::GetService(const ServiceReferenceBase&) method for the referenced service. That is, only one, use-counted service object is available from this ServiceObjects object.

This method will always return nullptr when the referenced service has been unregistered.

For a prototype scope service, the following steps are taken to get the service object:

  1. If the referenced service has been unregistered, nullptr is returned.

  2. The PrototypeServiceFactory::GetService(const Bundle&, const ServiceRegistrationBase&) method is called to create a service object for the caller.

  3. If the service object (an instance of InterfaceMap) returned by the PrototypeServiceFactory object is empty, does not contain all the interfaces named when the service was registered or the PrototypeServiceFactory object throws an exception, nullptr is returned and a warning message is issued.

  4. The service object is returned.

Throws:

std::logic_error – If the BundleContext used to create this ServiceObjects object is no longer valid.

Returns:

A shared_ptr to the service object. The returned shared_ptr is empty if the service is not registered, the service object returned by a ServiceFactory does not contain all the classes under which it was registered or the ServiceFactory threw an exception.

inline ServiceReference<void> GetServiceReference() const

Returns the ServiceReference for this ServiceObjects object.

Returns:

The ServiceReference for this ServiceObjects object.