fw4spl
IHasServices.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2014-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwServices/IHasServices.hpp"
8 
9 #include "fwServices/IService.hpp"
10 #include <fwServices/registry/ObjectService.hpp>
11 
12 namespace fwServices
13 {
14 
15 //------------------------------------------------------------------------------
16 
18 {
19 }
20 
21 //------------------------------------------------------------------------------
22 
24 {
25  SLM_ASSERT("Some sub-services were not unregistered, something is probably wrong. "
26  "Please use unregisterService() or unregisterServices() before destroying the sub-services owner.",
27  m_subServices.empty());
28 }
29 
30 //------------------------------------------------------------------------------
31 
32 ::fwServices::IService::csptr IHasServices::getRegisteredService(const fwTools::fwID::IDType& _id) const
33 {
34  ::fwServices::IService::sptr srv;
35  for(const auto& wService : m_subServices)
36  {
37  const ::fwServices::IService::sptr& service = wService.lock();
38  if(service && (service->getID() == _id))
39  {
40  srv = service;
41  break;
42  }
43  }
44  return srv;
45 }
46 
47 //------------------------------------------------------------------------------
48 
49 void IHasServices::unregisterService(const fwTools::fwID::IDType& _id)
50 {
51  for(auto itSrv = m_subServices.begin(); itSrv != m_subServices.end(); )
52  {
53  const ::fwServices::IService::sptr& service = itSrv->lock();
54  if(service && (service->getID() == _id))
55  {
56  service->stop().wait();
57  ::fwServices::OSR::unregisterService(service);
58  itSrv = m_subServices.erase(itSrv);
59  }
60  else
61  {
62  itSrv++;
63  }
64  }
65 }
66 
67 //------------------------------------------------------------------------------
68 
69 void IHasServices::unregisterService(const IService::sptr& _service)
70 {
71  auto iter = std::find_if(m_subServices.begin(), m_subServices.end(),
72  [ = ](const ::fwServices::IService::wptr& adaptor)
73  {
74  return adaptor.lock() == _service;
75  });
76 
77  SLM_ASSERT("service '" + _service->getID() + "' is not registered", iter != m_subServices.end());
78  m_subServices.erase(iter);
79 
80  _service->stop().wait();
81  ::fwServices::OSR::unregisterService(_service);
82 }
83 
84 //------------------------------------------------------------------------------
85 
86 ::fwServices::IService::sptr IHasServices::registerService(const std::string& _implType, const std::string& _id )
87 {
88  auto srv = ::fwServices::add(_implType, _id);
89  m_subServices.push_back(srv);
90 
91  return srv;
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 void IHasServices::unregisterServices(const std::string& _classname)
97 {
98  for(auto itSrv = m_subServices.begin(); itSrv != m_subServices.end(); )
99  {
100  const ::fwServices::IService::sptr& srv = itSrv->lock();
101  if(srv && (_classname.empty() || ( !_classname.empty() && srv->getClassname() == _classname)))
102  {
103  srv->stop().wait();
104  ::fwServices::OSR::unregisterService(srv);
105  itSrv = m_subServices.erase(itSrv);
106  }
107  else
108  {
109  itSrv++;
110  }
111  }
112 }
113 
114 //------------------------------------------------------------------------------
115 
116 } // namespace fwServices
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
FWSERVICES_API void unregisterService(const ::fwTools::fwID::IDType &_id)
Unregister a specific service.
FWSERVICES_API std::shared_ptr< const ::fwServices::IService > getRegisteredService(const ::fwTools::fwID::IDType &_id) const
Return a specific registered service.
virtual FWSERVICES_API ~IHasServices() noexcept
Destructor.
FWSERVICES_API void unregisterServices(const std::string &_implType="")
Unregister all services linked to this service, optionally matches only a given type of services...
FWSERVICES_API std::shared_ptr< ::fwServices::IService > registerService(const std::string &_implType, const std::string &_id="")
Register a new service linked to this service.
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
FWSERVICES_API IHasServices() noexcept
Constructor.