Leosac
0.8.0
Open Source Access Control
|
A class that manages services. More...
#include <ServiceRegistry.hpp>
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 ®) |
void | signal_deregistration (const RegistrationInfoPtr ®) |
Private Attributes | |
std::mutex | mutex_ |
std::map< boost::typeindex::type_index, RegistrationInfoPtr > | services_ |
bs2::signal< void(const service_event::Event &)> | signal_ |
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.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.
using Leosac::ServiceRegistry::EventListenerT = std::function<void(const service_event::Event &)> |
Definition at line 356 of file ServiceRegistry.hpp.
using Leosac::ServiceRegistry::RegistrationHandle = std::weak_ptr<void> |
Definition at line 133 of file ServiceRegistry.hpp.
|
private |
Definition at line 130 of file ServiceRegistry.hpp.
|
inline |
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was registered.
Definition at line 290 of file ServiceRegistry.hpp.
|
inline |
Register a service-event listener.
Service event listener must be both thread-safe and reentrant:
Definition at line 349 of file ServiceRegistry.hpp.
|
inline |
Definition at line 174 of file ServiceRegistry.hpp.
|
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.
|
inlineprivate |
Definition at line 366 of file ServiceRegistry.hpp.
|
inlineprivate |
Definition at line 359 of file ServiceRegistry.hpp.
|
inline |
Unregister the service for the interface ServiceInterface.
Definition at line 256 of file ServiceRegistry.hpp.
|
inline |
Unregister a service using the RegistrationHandle that was returned from the register_service()
call.
Definition at line 204 of file ServiceRegistry.hpp.
|
inline |
How many strong (shared) pointer points to the service that provides ServiceInterface.
Definition at line 316 of file ServiceRegistry.hpp.
|
mutableprivate |
Definition at line 373 of file ServiceRegistry.hpp.
|
private |
Definition at line 374 of file ServiceRegistry.hpp.
|
private |
Definition at line 375 of file ServiceRegistry.hpp.