ServiceInterface

template <class T>
std::shared_ptr<ServiceFactory> cppmicroservices::ToFactory(const std::shared_ptr<T> &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));

Return
A shared_ptr object of type ServiceFactory
See
BundleContext::RegisterService(ServiceFactory* factory, const ServiceProperties& properties)
Parameters
  • factory: The service factory shared_ptr object

using cppmicroservices::InterfaceMap = typedef 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.

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

template <class T>
const std::string &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.

Return
A unique id for the service interface type T.
Template Parameters
  • T: The service interface type.

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

Extract a service interface pointer from a given InterfaceMap instance.

Return
A shared pointer object of type Interface. The returned object is empty if the map does not contain an entry for the given type
See
MakeInterfaceMap
Parameters
  • map: a InterfaceMap instance.

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

Extract a service interface pointer from a given InterfaceMap instance.

Return
The service interface pointer for the service interface id or nullptr if map does not contain an entry for the given type.
See
ExtractInterface(const InterfaceMapConstPtr&)
Parameters
  • map: a InterfaceMap instance.
  • interfaceId: The interface id string.

template <class... Interfaces>
class cppmicroservices::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
InterfaceMap

Public Functions

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

Constructor taking a service implementation pointer.

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

MakeInterfaceMap(std::shared_ptr<ServiceFactory> factory)

Constructor taking a service factory.

Parameters
  • factory: A service factory.

operator InterfaceMapPtr()
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.