25 #include <boost/type_index.hpp> 30 namespace service_event
44 virtual ~Event() =
default;
59 , service_interface_type_(interface_type)
64 return service_interface_type_;
76 , service_interface_type_(interface_type)
81 return service_interface_type_;
124 : raw_service(nullptr){};
145 template <
typename ServiceInterface>
150 std::lock_guard<std::mutex> lg(mutex_);
152 auto type_index = boost::typeindex::type_id<ServiceInterface>();
154 "Already has a service registered for this interface: " 155 << type_index.pretty_name());
156 registration = std::make_shared<RegistrationInfo>();
157 registration->service_interface = type_index;
158 registration->unique_service =
159 std::unique_ptr<void, std::function<void(void *)>>(
160 srv.release(), [](
void *service_instance) {
161 auto typed_service_ptr =
162 static_cast<ServiceInterface *
>(service_instance);
163 ASSERT_LOG(service_instance && typed_service_ptr,
164 "service_instance is null.");
165 delete typed_service_ptr;
167 services_.insert(std::make_pair(type_index, registration));
169 signal_registration(registration);
173 template <
typename ServiceInterface>
178 std::lock_guard<std::mutex> lg(mutex_);
180 auto type_index = boost::typeindex::type_id<ServiceInterface>();
182 "Already has a service registered for this interface: " 183 << type_index.pretty_name());
185 registration = std::make_shared<RegistrationInfo>();
186 registration->service_interface = type_index;
187 registration->raw_service = srv;
188 services_.insert(std::make_pair(type_index, registration));
190 signal_registration(registration);
206 bool success =
false;
209 std::lock_guard<std::mutex> lg(mutex_);
211 std::shared_ptr<void> registration = h.lock();
227 if (registration_sptr.use_count() > 3)
235 services_.count(registration_sptr->service_interface),
236 "Trying to unregister a service using an invalid handle.");
237 services_.erase(registration_sptr->service_interface);
242 signal_deregistration(registration_sptr);
255 template <
typename ServiceInterface>
258 bool success =
false;
261 std::lock_guard<std::mutex> lg(mutex_);
262 boost::typeindex::type_index idx =
263 boost::typeindex::type_id<ServiceInterface>();
264 auto itr = services_.find(idx);
265 if (itr != services_.end())
267 registration = itr->second;
271 if (registration.use_count() > 3)
276 services_.erase(idx);
281 signal_deregistration(registration);
289 template <
typename ServiceInterface>
292 std::lock_guard<std::mutex> lg(mutex_);
294 auto type_index = boost::typeindex::type_id<ServiceInterface>();
295 auto itr = services_.find(type_index);
296 if (itr != services_.end())
298 auto ®istration = itr->second;
299 ServiceInterface *service_ptr;
300 if (registration->raw_service)
302 static_cast<ServiceInterface *
>(registration->raw_service);
304 service_ptr =
static_cast<ServiceInterface *
>(
305 registration->unique_service.get());
306 return std::shared_ptr<ServiceInterface>(registration, service_ptr);
315 template <
typename ServiceInterface>
318 auto srv = get_service<ServiceInterface>();
325 auto count = srv.use_count();
326 ASSERT_LOG(count >= 2,
"Hum, we are missing some shared_ptr...");
348 template <
typename T>
351 static_assert(std::is_convertible<T, EventListenerT>::value,
352 "Callable is not convertible to EventListenerT");
353 return signal_.connect(callable);
361 ASSERT_LOG(reg,
"RegistrationInfoPtr is null");
368 ASSERT_LOG(reg,
"RegistrationInfoPtr is null");
374 std::map<boost::typeindex::type_index, RegistrationInfoPtr>
services_;
375 bs2::signal<void(const service_event::Event &)>
signal_;
boost::typeindex::type_index interface_type() const
std::map< boost::typeindex::type_index, RegistrationInfoPtr > services_
A class that manages services.
ServiceRegistered(const boost::typeindex::type_index &interface_type)
bs2::signal< void(const service_event::Event &)> signal_
This is the header file for a generated source file, GitSHA1.cpp.
boost::typeindex::type_index service_interface
boost::typeindex::type_index service_interface_type_
std::weak_ptr< void > RegistrationHandle
void signal_registration(const RegistrationInfoPtr ®)
boost::typeindex::type_index interface_type() const
RegistrationHandle register_service(std::unique_ptr< ServiceInterface > srv)
Register a service by passing an unique_ptr to it.
bool unregister_service(RegistrationHandle h)
Unregister a service using the RegistrationHandle that was returned from the register_service() call...
An internal registration structure.
bool unregister_service()
Unregister the service for the interface ServiceInterface.
#define ASSERT_LOG(cond, msg)
RegistrationHandle register_service(ServiceInterface *srv)
std::shared_ptr< RegistrationInfo > RegistrationInfoPtr
bs2::connection register_event_listener(T &&callable)
Register a service-event listener.
std::unique_ptr< void, std::function< void(void *)> > unique_service
std::function< void(const service_event::Event &)> EventListenerT
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
boost::typeindex::type_index service_interface_type_
ServiceUnregistered(const boost::typeindex::type_index &interface_type)
long use_count() const
How many strong (shared) pointer points to the service that provides ServiceInterface.
void signal_deregistration(const RegistrationInfoPtr ®)