μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ClientFactory.hpp
Go to the documentation of this file.
1/*
2---------------------------------------------------------------------------
3
4 This file is part of uHAL.
5
6 uHAL is a hardware access library and programming framework
7 originally developed for upgrades of the Level-1 trigger of the CMS
8 experiment at CERN.
9
10 uHAL is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14
15 uHAL is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22
23
24 Andrew Rose, Imperial College, London
25 email: awr01 <AT> imperial.ac.uk
26
27 Marc Magrans de Abril, CERN
28 email: marc.magrans.de.abril <AT> cern.ch
29
30---------------------------------------------------------------------------
31*/
32
40#ifndef _uhal_ClientFactory_hpp_
41#define _uhal_ClientFactory_hpp_
42
43
44#include <map>
45#include <memory>
46#include <unordered_map>
47
50#include "uhal/log/log.hpp"
51
52
53namespace uhal
54{
55 namespace exception
56 {
58 UHAL_DEFINE_EXCEPTION_CLASS ( ProtocolDoesNotExist , "Exception class to handle the case where the protocol requested does not exists in the creator map." )
59
60
61 UHAL_DEFINE_EXCEPTION_CLASS ( ProtocolNotEnabled , "Exception class to handle the case where the protocol for a user client has not been enabled." )
62
64 UHAL_DEFINE_EXCEPTION_CLASS ( FailedToParseURI , "Exception class to handle the case where the factory failed to parse URI." )
65 }
66
73 {
74
75 private:
81
82 public:
83
84 ClientFactory(const ClientFactory&) = delete;
86
88 virtual ~ClientFactory();
89
94 static ClientFactory& getInstance();
95
102 std::shared_ptr<ClientInterface> getClient ( const std::string& aId , const std::string& aUri );
103
111 std::shared_ptr<ClientInterface> getClient ( const std::string& aId , const std::string& aUri, const std::vector<std::string>& aUserClientActivationList );
112
118 template <class T>
119 void registerClient ( const std::string& aProtocol , const std::string& aDescription = "" );
120
121 template <class T>
123 public:
125 static bool sInitialised;
126
127 static bool init(const std::string& aUri, const std::string& aDescription);
128 };
129
130 private:
131
137 template <class T>
138 void add ( const std::string& aProtocol , const std::string& aDescription, bool aUserDefined );
139
142 {
143 public:
146 {
147 }
148
151 {
152 }
153
160 virtual std::shared_ptr<ClientInterface> create ( const std::string& aId , const URI& aUri ) = 0;
161 };
162
164 template <class T>
166 {
167 public:
168
171 {
172 }
173
175 virtual ~Creator()
176 {
177 }
178
185 std::shared_ptr<ClientInterface> create ( const std::string& aId , const URI& aUri );
186 };
187
188
189 private:
190
191 struct ClientInfo {
192 std::shared_ptr<CreatorInterface> creator;
194 std::string description;
195 };
196
198 static std::shared_ptr<ClientFactory> mInstance;
200 std::unordered_map< std::string , ClientInfo > mClientMap; //map string name of each protocol to a creator for that protocol
201 };
202
203}
204
205#define UHAL_REGISTER_EXTERNAL_CLIENT(clientclass, clienturi, clientdescription) \
206 template<> bool uhal::ClientFactory::RegistrationHelper<clientclass>::sInitialised = \
207 uhal::ClientFactory::RegistrationHelper<clientclass>::init(clienturi, clientdescription);
208
209
211
212#endif
Wrapper to generate a new Python exception type.
Definition: pybind11.h:2517
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
Templated concrete implementation with a CreatorInterface interface.
virtual ~Creator()
Destructor.
Creator()
Default constructor.
An abstract base class for defining the interface to the creators.
virtual std::shared_ptr< ClientInterface > create(const std::string &aId, const URI &aUri)=0
Interface to a function which create a new IPbus client based on the protocol identifier specified.
static bool sInitialised
Dummy variable required as initialisation target.
A class to construct an IPbus client based on the protocol identifier specified NOTE!...
ClientFactory & operator=(const ClientFactory &)=delete
std::unordered_map< std::string, ClientInfo > mClientMap
Hash map associating a creator for a particular protocol with a file name.
ClientFactory(const ClientFactory &)=delete
static std::shared_ptr< ClientFactory > mInstance
The single instance of the class.
#define UHAL_DEFINE_EXCEPTION_CLASS(ClassName, ClassDescription)
Definition: exception.hpp:59
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:1900
Definition: init.h:269
std::shared_ptr< CreatorInterface > creator
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50