ServiceInterface

template<class T>
std::shared_ptr<ServiceFactory> ToFactory(std::shared_ptr<T> const &factory)

Cast the argument to a shared pointer of type ServiceFactory.

Useful when calling BundleContext::RegisterService with a service factory, for example:

std::shared_ptr<MyServiceFactory> factory = std::make_shared<MyServiceFactory>();
context->RegisterService<ISomeInterface>(ToFactory(factory));

See also

BundleContext::RegisterService(ServiceFactory* factory, const ServiceProperties& properties)

Parameters:

factory – The service factory shared_ptr object

Returns:

A shared_ptr object of type ServiceFactory

using InterfaceMap = std::unordered_map<std::string, std::shared_ptr<void>>

A map containing interfaces ids and their corresponding service object smart pointers.

InterfaceMap instances represent a complete service object which implements one or more service interfaces. For each implemented service interface, there is an entry in the map with the key being the service interface id and the value a smart pointer to the service interface implementation.

To create InterfaceMap instances, use the MakeInterfaceMap helper class.

See also

MakeInterfaceMap

Note

This is a low-level type and should only rarely be used.

template<class T>
std::string const &us_service_interface_iid()

Returns a unique id for a given type.

By default, the demangled name of T is returned.

This template method may be specialized directly or by using the macro CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE to return a custom id for each service interface.

Template Parameters:

T – The service interface type.

Returns:

A unique id for the service interface type T.

template<class Interface>
std::shared_ptr<Interface> ExtractInterface(InterfaceMapConstPtr const &map)

Extract a service interface pointer from a given InterfaceMap instance.

See also

MakeInterfaceMap

Parameters:

map – a InterfaceMap instance.

Returns:

A shared pointer object of type Interface. The returned object is empty if the map does not contain an entry for the given type

inline std::shared_ptr<void> ExtractInterface(InterfaceMapConstPtr const &map, std::string const &interfaceId)

Extract a service interface pointer from a given InterfaceMap instance.

Parameters:
  • map – a InterfaceMap instance.

  • interfaceId – The interface id string.

Returns:

The service interface pointer for the service interface id or nullptr if map does not contain an entry for the given type.

template<class ...Interfaces>
class MakeInterfaceMap
#include <cppmicroservices/ServiceInterface.h>

Helper class for constructing InterfaceMap instances based on service implementations or service factories.

Example usage:

std::shared_ptr<MyService> service; // implementes I1 and I2
InterfaceMap im = MakeInterfaceMap<I1,I2>(service);

See also

InterfaceMap

Public Functions

template<class Impl>
inline MakeInterfaceMap(std::shared_ptr<Impl> const &impl)

Constructor taking a service implementation pointer.

Parameters:

impl – A service implementation pointer, which must be castable to a all specified service interfaces.

inline MakeInterfaceMap(std::shared_ptr<ServiceFactory> factory)

Constructor taking a service factory.

Parameters:

factory – A service factory.

inline operator InterfaceMapPtr()
inline operator InterfaceMapConstPtr()
CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE(_service_interface_type, _service_interface_id)

Declare a service interface id.

This macro associates the given identifier _service_interface_id (a string literal) to the interface class called _service_interface_type. The Identifier must be unique. For example:

#include "cppmicroservices/ServiceInterface.h"

struct ISomeInterace { ... };

CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE(ISomeInterface, "com.mycompany.service.ISomeInterface/1.0")

The usage of this macro is optional and the service interface id which is automatically associated with any type is usually good enough (the demangled type name). However, care must be taken if the default id is compared with a string literal hard-coding a service interface id. E.g. the default id for templated types in the STL may differ between platforms. For user-defined types and templates the ids are typically consistent, but platform specific default template arguments will lead to different ids.

This macro is normally used right after the class definition for _service_interface_type, in a header file.

If you want to use CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE with interface classes declared in a namespace then you have to make sure the CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE macro call is not inside a namespace though. For example:

#include "cppmicroservices/ServiceInterface.h"

namespace Foo
{
  struct ISomeInterface { ... };
}

CPPMICROSERVICES_DECLARE_SERVICE_INTERFACE(Foo::ISomeInterface, "com.mycompany.service.ISomeInterface/1.0")
Parameters:
  • _service_interface_type – The service interface type.

  • _service_interface_id – A string literal representing a globally unique identifier.