fw4spl
AppConfigManager.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2015-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 #pragma once
8 
9 #include "fwServices/config.hpp"
10 #include "fwServices/helper/Config.hpp"
11 #include "fwServices/helper/ProxyConnections.hpp"
12 #include "fwServices/IAppConfigManager.hpp"
13 #include "fwServices/IService.hpp"
14 #include "fwServices/IXMLParser.hpp"
15 #include "fwServices/registry/AppConfig.hpp"
16 
17 #include <fwCom/HasSlots.hpp>
18 #include <fwCom/helper/SigSlotConnection.hpp>
19 
20 #include <string>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <vector>
24 
25 namespace fwData
26 {
27 class Composite;
28 }
29 
30 namespace fwServices
31 {
35 class FWSERVICES_CLASS_API AppConfigManager : public ::fwServices::IAppConfigManager,
36  public ::fwCom::HasSlots
37 {
38 public:
39 
41  (()),
42  std::make_shared< AppConfigManager >)
44 
46  FWSERVICES_API AppConfigManager();
47 
49  FWSERVICES_API virtual ~AppConfigManager();
50 
55  FWSERVICES_API virtual void setConfig(const std::string& configId,
56  const FieldAdaptorType& replaceFields = FieldAdaptorType()) override;
57  FWSERVICES_API virtual void setConfig(const std::string& configId,
58  const ::fwData::Composite::csptr& replaceFields) override;
59  FWSERVICES_API virtual ::fwData::Object::sptr getConfigRoot() const override;
60  FWSERVICES_API virtual void launch() override;
61  FWSERVICES_API virtual void stopAndDestroy() override;
62  FWSERVICES_API virtual void create() override;
63  FWSERVICES_API virtual void start() override;
64  FWSERVICES_API virtual void update() override;
65  FWSERVICES_API virtual void stop() override;
66  FWSERVICES_API virtual void destroy() override;
68 
74  FWSERVICES_API virtual void startBundle();
75 
77  FWSERVICES_API void setIsUnitTest(bool isUnitTest);
78 
79 private:
80 
81  typedef ::std::pair< std::string, bool > ConfigAttribute;
82  typedef ::fwServices::helper::ProxyConnections ProxyConnections;
83  typedef ::fwServices::IService::Config ServiceConfig;
84 
85  ::fwData::Object::sptr findObject(const std::string& uid, const std::string& errMsgTail) const;
86 
87  ::fwData::Object::sptr getNewObject(ConfigAttribute type, const std::string& uid) const;
88 
89  ::fwData::Object::sptr getNewObject(ConfigAttribute type,
90  ConfigAttribute uid = ConfigAttribute("", false)) const;
91 
92  ::fwData::Object::sptr getObject(ConfigAttribute type, const std::string& uid) const;
93 
94  ::fwServices::IService::sptr getNewService(const std::string& uid, const std::string& implType) const;
95 
96  void stopStartedServices();
97  void destroyCreatedServices();
98 
99  void processStartItems();
100  void processUpdateItems();
101 
103  void createObjects(::fwRuntime::ConfigurationElement::csptr cfgElem);
104 
106  void createServices(::fwRuntime::ConfigurationElement::csptr cfgElem);
107 
109  ::fwServices::IService::sptr createService(const ServiceConfig& srvConfig);
110 
112  void createConnections();
113 
115  std::string msgHead() const;
116 
118  void addObjects(::fwData::Object::sptr obj, const std::string& id);
119 
121  void removeObjects(::fwData::Object::sptr obj, const std::string& id);
122 
124  void connectProxy(const std::string& channel, const ProxyConnections& connectCfg);
125 
126  void destroyProxy(const std::string& channel, const ProxyConnections& proxyCfg, const std::string& key = "",
127  fwData::Object::csptr hintObj = nullptr);
128  void destroyProxies();
129 
131  static std::string getUIDListAsString(const std::vector<std::string>& uidList);
132 
133  typedef std::pair< ::fwData::Object::sptr, ::fwServices::IXMLParser::sptr> CreatedObjectType;
135  std::unordered_map<std::string, CreatedObjectType> m_createdObjects;
136 
137  struct DeferredObjectType
138  {
139  std::vector< ServiceConfig > m_servicesCfg;
140  std::unordered_map< std::string, ProxyConnections > m_proxyCnt;
142  ::fwData::Object::sptr m_object;
143  };
144 
146  std::unordered_map<std::string, DeferredObjectType > m_deferredObjects;
147 
149  std::unordered_set<std::string> m_deferredServices;
150 
152  std::unordered_map<std::string, ProxyConnections> m_createdObjectsProxies;
153 
154  struct ServiceProxyType
155  {
156  std::unordered_map< std::string, ProxyConnections > m_proxyCnt;
157  };
158  std::unordered_map< std::string, ServiceProxyType > m_servicesProxies;
159 
161  std::string m_configId;
162 
163  typedef std::vector< ::fwServices::IService::wptr > ServiceContainer;
164 
166  ServiceContainer m_createdSrv;
167 
169  ServiceContainer m_startedSrv;
170 
172  std::vector<std::string> m_deferredStartSrv;
173 
175  std::vector<std::string> m_deferredUpdateSrv;
176 
180  ::fwData::Composite::sptr m_tmpRootObject;
181 
183  unsigned int m_proxyID;
184 
185  ::fwCom::Connection m_addObjectConnection;
186  ::fwCom::Connection m_removeObjectConnection;
187 
189  bool m_isUnitTest;
190 };
191 
192 // ------------------------------------------------------------------------
193 
194 inline std::string AppConfigManager::msgHead() const
195 {
196  return "[" + m_configId + "] ";
197 }
198 
199 } // namespace fwServices
Base class for all services.
Definition: IService.hpp:61
STL namespace.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
This class provides an API to manage config template.
This class proposes a mapping between a SlotKeyType and a SlotBase.
Definition: HasSlots.hpp:22
Class managing Signal-Slot connections.
Definition: Connection.hpp:17
The namespace fwRuntime contains classes to manage bundle, configuration element, extension point in ...
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
Helper class to register proxy connections.
#define fwCoreAllowSharedFromThis()
Generate getSptr and getConstSptr methods.
Contains the representation of the data objects used in the framework.
Service type for the construction of an object and associated services from an XML-based description...
Definition: IXMLParser.hpp:36
This class provides an interface to manage configurations template.
Used to store a service configuration.
Definition: IService.hpp:100