ChimeraTK-DeviceAccess  03.18.00
BackendFactory.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "DeviceAccessVersion.h"
6 #include "DeviceInfoMap.h"
7 #include "ForwardDeclarations.h"
8 
9 #include <boost/function.hpp>
10 
11 #include <map>
12 #include <mutex>
13 
14 /* For test purposes; if a dummies.dmap file is found in the folder from where
15  * the program is being executed it would be used as dmap file. The default dmap
16  * file would be DMAP_FILE_PATH.
17  */
18 #define TEST_DMAP_FILE_PATH "./dummies.dmap" // FIXME remove
19 #define ENV_VAR_DMAP_FILE "DMAP_PATH_ENV" // FIXME remove
20 
21 namespace ChimeraTK {
22 
27  public:
33  void setDMapFilePath(std::string dMapFilePath);
34 
36  std::string getDMapFilePath();
37 
55  void registerBackendType(const std::string& backendType,
56  boost::shared_ptr<DeviceBackend> (*creatorFunction)(
57  std::string address, std::map<std::string, std::string> parameters),
58  const std::vector<std::string>& sdmParameterNames = {},
59  const std::string& deviceAccessVersion = CHIMERATK_DEVICEACCESS_VERSION);
60 
65  [[deprecated]] void registerBackendType(const std::string& interface, const std::string& protocol,
66  boost::shared_ptr<DeviceBackend> (*creatorFunction)(
67  std::string host, std::string instance, std::list<std::string> parameters, std::string mapFileName),
68  const std::string& version);
69 
74  boost::shared_ptr<DeviceBackend> createBackend(const std::string& aliasOrUri);
75 
77  static BackendFactory& getInstance();
78 
81  void loadPluginLibrary(const std::string& soFile);
82 
83  protected:
84  std::string _dMapFile;
85 
88  std::map<std::string,
89  boost::function<boost::shared_ptr<DeviceBackend>(
90  std::string address, std::map<std::string, std::string> parameters)>>
92 
95  std::map<std::pair<std::string, std::string>,
96  boost::function<boost::shared_ptr<DeviceBackend>(
97  std::string host, std::string instance, std::list<std::string> parameters, std::string mapFileName)>>
99 
101  std::string aliasLookUp(std::string aliasName, std::string dmapFilePath);
102 
104  boost::shared_ptr<DeviceBackend> createBackendInternal(const DeviceInfoMap::DeviceInfo& deviceInfo);
105 
108  std::map<std::string, boost::weak_ptr<DeviceBackend>> _existingBackends;
109 
112  std::mutex _mutex;
113 
114  // A function which has the signature of a creator function, plus one string
115  // 'exception_what'. If a plugins fails to register, this function is bound to
116  // an error string and stored in the creatorMap. It later it is tried to open
117  // the backend, an exception with this error message is thrown.
118  static boost::shared_ptr<DeviceBackend> failedRegistrationThrowerFunction(std::string host, std::string instance,
119  std::list<std::string> parameters, std::string mapFileName, std::string exception_what);
120 
124 
128 
129  private:
130  // These functions are not to be inherited. As also the constructor is
131  // private, you cannot derrive from this class.
134  BackendFactory();
135 
137  BackendFactory(BackendFactory const&&) noexcept;
138  void operator=(BackendFactory const&);
139  };
140 
141 } // namespace ChimeraTK
ChimeraTK::BackendFactory::createBackend
boost::shared_ptr< DeviceBackend > createBackend(const std::string &aliasOrUri)
Create a new backend and return the instance as a shared pointer.
Definition: BackendFactory.cc:201
ForwardDeclarations.h
ChimeraTK::BackendFactory::loadPluginLibrary
void loadPluginLibrary(const std::string &soFile)
Load a shared library (.so file) with a Backend at run time.
Definition: BackendFactory.cc:281
ChimeraTK::BackendFactory::getInstance
static BackendFactory & getInstance()
Static function to get an instance of factory.
Definition: BackendFactory.cc:191
ChimeraTK::BackendFactory::_mutex
std::mutex _mutex
A mutex to protect backend creation and returning to keep the maps consistent.
Definition: BackendFactory.h:112
ChimeraTK::BackendFactory::_existingBackends
std::map< std::string, boost::weak_ptr< DeviceBackend > > _existingBackends
A map of all created backends.
Definition: BackendFactory.h:108
ChimeraTK::BackendFactory::setDMapFilePath
void setDMapFilePath(std::string dMapFilePath)
This function sets the _DMapFilePath.
Definition: BackendFactory.cc:161
ChimeraTK::BackendFactory::getDMapFilePath
std::string getDMapFilePath()
Returns the _DMapFilePath.
Definition: BackendFactory.cc:167
ChimeraTK::BackendFactory::createBackendInternal
boost::shared_ptr< DeviceBackend > createBackendInternal(const DeviceInfoMap::DeviceInfo &deviceInfo)
Internal function to return a DeviceBackend.
Definition: BackendFactory.cc:222
ChimeraTK::BackendFactory
BackendFactory is a the factory class to create devices.
Definition: BackendFactory.h:26
ChimeraTK::BackendFactory::loadAllPluginsFromDMapFile
void loadAllPluginsFromDMapFile()
Load all shared libraries specified in the dmap file.
Definition: BackendFactory.cc:302
ChimeraTK::BackendFactory::called_registerBackendType
bool called_registerBackendType
Flag whether the function registerBackendType() was called.
Definition: BackendFactory.h:127
ChimeraTK::BackendFactory::failedRegistrationThrowerFunction
static boost::shared_ptr< DeviceBackend > failedRegistrationThrowerFunction(std::string host, std::string instance, std::list< std::string > parameters, std::string mapFileName, std::string exception_what)
Definition: BackendFactory.cc:328
ChimeraTK::DeviceInfoMap::DeviceInfo
Stores information about one device.
Definition: DeviceInfoMap.h:30
ChimeraTK::BackendFactory::creatorMap
std::map< std::string, boost::function< boost::shared_ptr< DeviceBackend > std::string address, std::map< std::string, std::string > parameters)> > creatorMap
Holds device type and function pointer to the createInstance function of plugin.
Definition: BackendFactory.h:91
ChimeraTK::BackendFactory::aliasLookUp
std::string aliasLookUp(std::string aliasName, std::string dmapFilePath)
Look for the alias and if found return a uri.
DeviceInfoMap.h
ChimeraTK::BackendFactory::_dMapFile
std::string _dMapFile
The dmap file set at run time.
Definition: BackendFactory.h:84
ChimeraTK::BackendFactory::registerBackendType
void registerBackendType(const std::string &backendType, boost::shared_ptr< DeviceBackend >(*creatorFunction)(std::string address, std::map< std::string, std::string > parameters), const std::vector< std::string > &sdmParameterNames={}, const std::string &deviceAccessVersion=CHIMERATK_DEVICEACCESS_VERSION)
Register a backend by the name backendType with the given creatorFunction.
Definition: BackendFactory.cc:45
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::BackendFactory::creatorMap_compat
std::map< std::pair< std::string, std::string >, boost::function< boost::shared_ptr< DeviceBackend > std::string host, std::string instance, std::list< std::string > parameters, std::string mapFileName)> > creatorMap_compat
Compatibility creatorMap for old-style backends which just take a plain list of parameters.
Definition: BackendFactory.h:98