ServiceReference

typedef ServiceReference<void> cppmicroservices::ServiceReferenceU

A service reference of unknown type, which is not bound to any interface identifier.

std::ostream &cppmicroservices::operator<<(std::ostream &os, const ServiceReferenceBase &serviceRef)

Writes a string representation of serviceRef to the stream os.

template <class S>
class cppmicroservices::ServiceReference
#include <cppmicroservices/ServiceReference.h>

A reference to a service.

The framework returns ServiceReference objects from the BundleContext::GetServiceReference and BundleContext::GetServiceReferences methods.

A ServiceReference object may be shared between bundles and can be used to examine the properties of the service and to get the service object.

Every service registered in the framework has a unique ServiceRegistration object and may have multiple, distinct ServiceReference objects referring to it. ServiceReference objects associated with a ServiceRegistration are considered equal (more specifically, their operator==() method will return true when compared).

If the same service object is registered multiple times, ServiceReference objects associated with different ServiceRegistration objects are not equal.

See

BundleContext::GetServiceReference

BundleContext::GetServiceReferences

BundleContext::GetService

Template Parameters
  • S: The class type of the service interface

Inherits from cppmicroservices::ServiceReferenceBase

Public Types

typedef S ServiceType

Public Functions

ServiceReference()

Creates an invalid ServiceReference object.

You can use this object in boolean expressions and it will evaluate to false.

ServiceReference(const ServiceReferenceBase &base)
class cppmicroservices::ServiceReferenceBase
#include <cppmicroservices/ServiceReferenceBase.h>

A reference to a service.

Note

This class is provided as public API for low-level service queries only. In almost all cases you should use the template ServiceReference instead.

Subclassed by cppmicroservices::ServiceReference< S >

Public Functions

ServiceReferenceBase(const ServiceReferenceBase &ref)
operator bool() const

Converts this ServiceReferenceBase instance into a boolean expression.

If this instance was default constructed or the service it references has been unregistered, the conversion returns false, otherwise it returns true.

ServiceReferenceBase &operator=(std::nullptr_t)

Releases any resources held or locked by this ServiceReferenceBase and renders it invalid.

~ServiceReferenceBase()
Any GetProperty(const std::string &key) const

Returns the property value to which the specified property key is mapped in the properties ServiceProperties object of the service referenced by this ServiceReferenceBase object.

Property keys are case-insensitive.

This method continues to return property values after the service has been unregistered. This is so references to unregistered services can still be interrogated.

Return
The property value to which the key is mapped; an invalid Any if there is no property named after the key.
Parameters
  • key: The property key.

void GetPropertyKeys(std::vector<std::string> &keys) const

Returns a list of the keys in the ServiceProperties object of the service referenced by this ServiceReferenceBase object.

This method will continue to return the keys after the service has been unregistered. This is so references to unregistered services can still be interrogated.

Parameters
  • keys: A vector being filled with the property keys.

std::vector<std::string> GetPropertyKeys() const

Returns a list of the keys in the ServiceProperties object of the service referenced by this ServiceReferenceBase object.

This method will continue to return the keys after the service has been unregistered. This is so references to unregistered services can still be interrogated.

Return
A vector being filled with the property keys.

Bundle GetBundle() const

Returns the bundle that registered the service referenced by this ServiceReferenceBase object.

This method must return an invalid bundle when the service has been unregistered. This can be used to determine if the service has been unregistered.

Return
The bundle that registered the service referenced by this ServiceReferenceBase object; an invalid bundle if that service has already been unregistered.
See

BundleContext::RegisterService(const InterfaceMap&, const ServiceProperties&)

Bundle::operator bool() const

std::vector<Bundle> GetUsingBundles() const

Returns the bundles that are using the service referenced by this ServiceReferenceBase object.

Specifically, this method returns the bundles whose usage count for that service is greater than zero.

Return
A list of bundles whose usage count for the service referenced by this ServiceReferenceBase object is greater than zero.

std::string GetInterfaceId() const

Returns the interface identifier this ServiceReferenceBase object is bound to.

A default constructed ServiceReferenceBase object is not bound to any interface identifier and calling this method will return an empty string.

Return
The interface identifier for this ServiceReferenceBase object.

bool IsConvertibleTo(const std::string &interfaceid) const

Checks whether this ServiceReferenceBase object can be converted to another ServiceReferenceBase object, which will be bound to the given interface identifier.

ServiceReferenceBase objects can be converted if the underlying service implementation was registered under multiple service interfaces.

Return
true if this ServiceReferenceBase object can be converted, false otherwise.
Parameters
  • interfaceid:

bool operator<(const ServiceReferenceBase &reference) const

Compares this ServiceReferenceBase with the specified ServiceReferenceBase for order.

If this ServiceReferenceBase and the specified ServiceReferenceBase have the same service id they are equal. This ServiceReferenceBase is less than the specified ServiceReferenceBase if it has a lower service ranking and greater if it has a higher service ranking. Otherwise, if this ServiceReferenceBase and the specified ServiceReferenceBase have the same service ranking, this ServiceReferenceBase is less than the specified ServiceReferenceBase if it has a higher service id and greater if it has a lower service id.

Return
Returns a false or true if this ServiceReferenceBase is less than or greater than the specified ServiceReferenceBase.
Parameters

bool operator==(const ServiceReferenceBase &reference) const
ServiceReferenceBase &operator=(const ServiceReferenceBase &reference)

Friends

friend gr_servicereference::::std::hash< ServiceReferenceBase >
template<>
struct std::hash<cppmicroservices::ServiceReferenceBase>
#include <cppmicroservices/ServiceReferenceBase.h>

Hash functor specialization for ServiceReferenceBase objects.