fw4spl
ServiceFactory.hpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-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 
11 #include <fwCore/mt/types.hpp>
12 
13 #include <fwRuntime/Bundle.hpp>
14 
15 #include <fwTools/Object.hpp>
16 
17 #include <boost/unordered_map.hpp>
18 
19 #include <unordered_map>
20 
21 namespace fwServices
22 {
23 
24 class IService;
25 
26 namespace registry
27 {
28 
33 class FWSERVICES_CLASS_API ServiceFactory : public ::fwCore::BaseObject
34 {
35 
36 public:
37 
38  typedef std::string KeyType;
39  typedef std::vector<KeyType> KeyVectorType;
40  typedef std::pair<std::string, std::string> StringPair;
41 
43  typedef ::boost::unordered_map< StringPair, bool > SupportMapType;
44  typedef std::function< SPTR(::fwServices::IService)() > FactoryType;
45 
47 
48 
49  FWSERVICES_API static ServiceFactory::sptr getDefault();
50 
52  FWSERVICES_API void parseBundleInformation( );
53 
54  FWSERVICES_API void addServiceFactory( FactoryType _factory,
55  const std::string& simpl,
56  const std::string& stype);
57 
58  FWSERVICES_API void addObjectFactory( const std::string& simpl,
59  const std::string& oimpl);
60 
62  FWSERVICES_API SPTR(IService) create( const std::string& _srvImpl ) const;
63 
65  FWSERVICES_API SPTR(IService) create( const std::string& _srvType, const std::string& _srvImpl ) const;
66 
67  FWSERVICES_API void clearFactory();
68 
70  FWSERVICES_API std::vector< std::string > getImplementationIdFromObjectAndType(const std::string& object,
71  const std::string& type) const;
72 
74  FWSERVICES_API std::string getDefaultImplementationIdFromObjectAndType( const std::string& object,
75  const std::string& type ) const;
76 
78  FWSERVICES_API const std::vector<std::string>& getServiceObjects(const std::string& srvImpl) const;
79 
81  FWSERVICES_API std::string getServiceDescription(const std::string& srvImpl) const;
82 
84  FWSERVICES_API std::string getServiceTags(const std::string& srvImpl) const;
85 
87  FWSERVICES_API bool checkServiceValidity(const std::string& object, const std::string& srvImpl) const;
88 
93  FWSERVICES_API bool support(const std::string& object, const std::string& srvType);
94 
99  FWSERVICES_API bool support(const std::string& object, const std::string& srvType,
100  const std::string& srvImpl) const;
101 
105  FWSERVICES_API virtual KeyVectorType getFactoryKeys() const;
106 
107 private:
108 
109  struct ServiceInfo
110  {
111  std::string serviceType;
112  std::vector<std::string> objectImpl;
113  std::string desc;
114  std::string tags;
115  std::shared_ptr< ::fwRuntime::Bundle > bundle;
116  FactoryType factory;
117  bool objectsSetFromBundle {false}; // True if the objects implementation are set from the bundle information
118  };
119  typedef std::unordered_map< KeyType, ServiceInfo > SrvRegContainer;
120 
125  void printInfoMap( const SrvRegContainer& src ) const;
126 
131  void checkServicesNotDeclaredInPluginXml() const;
132 
134  SrvRegContainer m_srvImplTosrvInfo;
135  SupportMapType m_supportMap;
136 
137  mutable ::fwCore::mt::ReadWriteMutex m_srvImplTosrvInfoMutex;
138  mutable ::fwCore::mt::ReadWriteMutex m_supportMapMutex;
139 };
140 
141 } // namespace registry
142 } // namespace fwServices
Base class for all services.
Definition: IService.hpp:61
#define SPTR(_cls_)
Contains fwAtomsFilter::registry details.
Base class for all FW4SPL&#39;s classes.
Definition: BaseObject.hpp:22
STL namespace.
Namespace fwServices is dedicated to (mimic) the dynamic affectation of methods to (pure data) object...
Contains fwAtomsFilter::factory utilities.
#define fwCoreClassDefinitionsWithFactoryMacro(_classinfo_, _parameters_, _factory_)
Generate common construction methods for classes with one factory.
::boost::unordered_map< StringPair, bool > SupportMapType
We keep boost here because std implementation does not support a pair of std::string as key...