Leosac  0.8.0
Open Source Access Control
Leosac::ServiceRegistry Class Reference

A class that manages services. More...

#include <ServiceRegistry.hpp>

+ Collaboration diagram for Leosac::ServiceRegistry:

Classes

struct  RegistrationInfo
 An internal registration structure. More...
 

Public Types

using RegistrationHandle = std::weak_ptr< void >
 
using EventListenerT = std::function< void(const service_event::Event &)>
 

Public Member Functions

template<typename ServiceInterface >
RegistrationHandle register_service (std::unique_ptr< ServiceInterface > srv)
 Register a service by passing an unique_ptr to it. More...
 
template<typename ServiceInterface >
RegistrationHandle register_service (ServiceInterface *srv)
 
bool unregister_service (RegistrationHandle h)
 Unregister a service using the RegistrationHandle that was returned from the register_service() call. More...
 
template<typename ServiceInterface >
bool unregister_service ()
 Unregister the service for the interface ServiceInterface. More...
 
template<typename ServiceInterface >
std::shared_ptr< ServiceInterface > get_service () const
 Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was registered. More...
 
template<typename ServiceInterface >
long use_count () const
 How many strong (shared) pointer points to the service that provides ServiceInterface. More...
 
template<typename T >
bs2::connection register_event_listener (T &&callable)
 Register a service-event listener. More...
 

Private Types

using RegistrationInfoPtr = std::shared_ptr< RegistrationInfo >
 

Private Member Functions

void signal_registration (const RegistrationInfoPtr &reg)
 
void signal_deregistration (const RegistrationInfoPtr &reg)
 

Private Attributes

std::mutex mutex_
 
std::map< boost::typeindex::type_index, RegistrationInfoPtrservices_
 
bs2::signal< void(const service_event::Event &)> signal_
 

Detailed Description

A class that manages services.

The ServiceRegistry object provides facilities to manage services. It can perform service registration, unregistration, retrieving.

A service is a C++ object that implement a given interface. A limitation of the ServiceRegistry is that it can only store one service instance per interface type.

Important notes regarding object lifecycle:

  • register_service() takes a naked pointer. It is the responsibility of the caller to make sure that the service object will stay alive at least until a successful call to unregister_service() happens.
  • The ServiceRegistry implements reference counting with regards to service object it hands over to client.
  • unregister_service() will fail if the service is currently being used by someone. "Being used" means holding a shared_ptr<> to the service instance, as returned by get_service()).

Definition at line 110 of file ServiceRegistry.hpp.

Member Typedef Documentation

◆ EventListenerT

using Leosac::ServiceRegistry::EventListenerT = std::function<void(const service_event::Event &)>

Definition at line 356 of file ServiceRegistry.hpp.

◆ RegistrationHandle

using Leosac::ServiceRegistry::RegistrationHandle = std::weak_ptr<void>

Definition at line 133 of file ServiceRegistry.hpp.

◆ RegistrationInfoPtr

Definition at line 130 of file ServiceRegistry.hpp.

Member Function Documentation

◆ get_service()

template<typename ServiceInterface >
std::shared_ptr<ServiceInterface> Leosac::ServiceRegistry::get_service ( ) const
inline

Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was registered.

Definition at line 290 of file ServiceRegistry.hpp.

◆ register_event_listener()

template<typename T >
bs2::connection Leosac::ServiceRegistry::register_event_listener ( T &&  callable)
inline

Register a service-event listener.

Service event listener must be both thread-safe and reentrant:

  • They may be invoked in any thread interacting with the registry.
  • A service event listener may interact with the registry, thus dispatching an other event synchronously.
Note
The internal registry lock is not hold while the event listener are invoked.
Warning
You shall not keep hold of the ServiceEvent pointer that is passed as a parameter to the listener. This pointer only valid while the listeners are being invoked.

Definition at line 349 of file ServiceRegistry.hpp.

◆ register_service() [1/2]

template<typename ServiceInterface >
RegistrationHandle Leosac::ServiceRegistry::register_service ( ServiceInterface *  srv)
inline

Definition at line 174 of file ServiceRegistry.hpp.

◆ register_service() [2/2]

template<typename ServiceInterface >
RegistrationHandle Leosac::ServiceRegistry::register_service ( std::unique_ptr< ServiceInterface >  srv)
inline

Register a service by passing an unique_ptr to it.

By calling this overload over the more general register_service(), the caller free themselves from managing the lifetime of the service.

The service will be automatically deleted once a successful call to unregister_service() is made.

Definition at line 146 of file ServiceRegistry.hpp.

◆ signal_deregistration()

void Leosac::ServiceRegistry::signal_deregistration ( const RegistrationInfoPtr reg)
inlineprivate

Definition at line 366 of file ServiceRegistry.hpp.

◆ signal_registration()

void Leosac::ServiceRegistry::signal_registration ( const RegistrationInfoPtr reg)
inlineprivate

Definition at line 359 of file ServiceRegistry.hpp.

◆ unregister_service() [1/2]

template<typename ServiceInterface >
bool Leosac::ServiceRegistry::unregister_service ( )
inline

Unregister the service for the interface ServiceInterface.

Returns
This call returns true if the service was properly unregistered. Otherwise it returns false.
Note
It's not possible to unregister a service if something is holding a reference to it.

Definition at line 256 of file ServiceRegistry.hpp.

◆ unregister_service() [2/2]

bool Leosac::ServiceRegistry::unregister_service ( RegistrationHandle  h)
inline

Unregister a service using the RegistrationHandle that was returned from the register_service() call.

Returns
This call returns true if the service was properly unregistered. Otherwise it returns false.
Note
It's not possible to unregister a service if something is holding a reference to it.

Definition at line 204 of file ServiceRegistry.hpp.

◆ use_count()

template<typename ServiceInterface >
long Leosac::ServiceRegistry::use_count ( ) const
inline

How many strong (shared) pointer points to the service that provides ServiceInterface.

Definition at line 316 of file ServiceRegistry.hpp.

Member Data Documentation

◆ mutex_

std::mutex Leosac::ServiceRegistry::mutex_
mutableprivate

Definition at line 373 of file ServiceRegistry.hpp.

◆ services_

std::map<boost::typeindex::type_index, RegistrationInfoPtr> Leosac::ServiceRegistry::services_
private

Definition at line 374 of file ServiceRegistry.hpp.

◆ signal_

bs2::signal<void(const service_event::Event &)> Leosac::ServiceRegistry::signal_
private

Definition at line 375 of file ServiceRegistry.hpp.


The documentation for this class was generated from the following file: