25 #include <boost/type_index.hpp>
30 namespace service_event
44 virtual ~Event() =
default;
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));
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));
206 bool success =
false;
209 std::lock_guard<std::mutex> lg(
mutex_);
211 std::shared_ptr<void> registration = h.lock();
215 std::static_pointer_cast<RegistrationInfo>(registration);
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);
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>();
267 registration = itr->second;
271 if (registration.use_count() > 3)
289 template <
typename ServiceInterface>
292 std::lock_guard<std::mutex> lg(
mutex_);
294 auto type_index = boost::typeindex::type_id<ServiceInterface>();
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_;