μHAL (v2.7.9)
Part of the IPbus software repository
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 
46 #include <boost/noncopyable.hpp>
47 #include <boost/shared_ptr.hpp>
48 #include <boost/unordered_map.hpp>
49 
50 #include "uhal/log/exception.hpp"
51 #include "uhal/ClientInterface.hpp"
52 #include "uhal/log/log.hpp"
53 
54 
55 namespace uhal
56 {
57  namespace exception
58  {
60  UHAL_DEFINE_EXCEPTION_CLASS ( ProtocolDoesNotExist , "Exception class to handle the case where the protocol requested does not exists in the creator map." )
61 
62 
63  UHAL_DEFINE_EXCEPTION_CLASS ( ProtocolNotEnabled , "Exception class to handle the case where the protocol for a user client has not been enabled." )
64 
66  UHAL_DEFINE_EXCEPTION_CLASS ( FailedToParseURI , "Exception class to handle the case where the factory failed to parse URI." )
67  }
68 
74  class ClientFactory: private boost::noncopyable
75  {
76 
77  private:
82  ClientFactory();
83 
84  public:
86  virtual ~ClientFactory();
87 
92  static ClientFactory& getInstance();
93 
100  boost::shared_ptr<ClientInterface> getClient ( const std::string& aId , const std::string& aUri );
101 
109  boost::shared_ptr<ClientInterface> getClient ( const std::string& aId , const std::string& aUri, const std::vector<std::string>& aUserClientActivationList );
110 
116  template <class T>
117  void registerClient ( const std::string& aProtocol , const std::string& aDescription = "" );
118 
119  template <class T>
121  public:
123  static bool sInitialised;
124 
125  static bool init(const std::string& aUri, const std::string& aDescription);
126  };
127 
128  private:
129 
135  template <class T>
136  void add ( const std::string& aProtocol , const std::string& aDescription, bool aUserDefined );
137 
140  {
141  public:
144  {
145  }
146 
149  {
150  }
151 
158  virtual boost::shared_ptr<ClientInterface> create ( const std::string& aId , const URI& aUri ) = 0;
159  };
160 
162  template <class T>
163  class Creator: public CreatorInterface
164  {
165  public:
166 
169  {
170  }
171 
173  virtual ~Creator()
174  {
175  }
176 
183  boost::shared_ptr<ClientInterface> create ( const std::string& aId , const URI& aUri );
184  };
185 
186 
187  private:
188 
189  struct ClientInfo {
192  std::string description;
193  };
194 
198  boost::unordered_map< std::string , ClientInfo > mClientMap; //map string name of each protocol to a creator for that protocol
199  };
200 
201 }
202 
203 #define UHAL_REGISTER_EXTERNAL_CLIENT(clientclass, clienturi, clientdescription) \
204  template<> bool uhal::ClientFactory::RegistrationHelper<clientclass>::sInitialised = \
205  uhal::ClientFactory::RegistrationHelper<clientclass>::init(clienturi, clientdescription);
206 
207 
209 
210 #endif
uhal::ClientFactory::mInstance
static boost::shared_ptr< ClientFactory > mInstance
The single instance of the class.
Definition: ClientFactory.hpp:196
uhal::ClientFactory::Creator
Templated concrete implementation with a CreatorInterface interface.
Definition: ClientFactory.hpp:164
UHAL_DEFINE_EXCEPTION_CLASS
#define UHAL_DEFINE_EXCEPTION_CLASS(ClassName, ClassDescription)
Definition: exception.hpp:59
uhal::ClientFactory::ClientInfo
Definition: ClientFactory.hpp:189
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
uhal::ClientFactory::CreatorInterface::CreatorInterface
CreatorInterface()
Default constructor.
Definition: ClientFactory.hpp:143
uhal::ClientFactory
A class to construct an IPbus client based on the protocol identifier specified NOTE!...
Definition: ClientFactory.hpp:75
boost
Definition: log.hpp:13
uhal::ClientFactory::mClientMap
boost::unordered_map< std::string, ClientInfo > mClientMap
Hash map associating a creator for a particular protocol with a file name.
Definition: ClientFactory.hpp:198
uhal::ClientFactory::ClientInfo::creator
boost::shared_ptr< CreatorInterface > creator
Definition: ClientFactory.hpp:190
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::ClientFactory::Creator::Creator
Creator()
Default constructor.
Definition: ClientFactory.hpp:168
uhal::ClientFactory::CreatorInterface::~CreatorInterface
virtual ~CreatorInterface()
Destructor.
Definition: ClientFactory.hpp:148
uhal::ClientFactory::CreatorInterface::create
virtual boost::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.
uhal::tests::client
IPbusCore & client
Definition: test_config_space.cpp:79
uhal::tests::for
for(size_t i=0;i< lDepths.size();i++)
Definition: test_block.cpp:135
uhal::ClientFactory::CreatorInterface
An abstract base class for defining the interface to the creators.
Definition: ClientFactory.hpp:140
uhal::ClientFactory::ClientInfo::userDefined
bool userDefined
Definition: ClientFactory.hpp:191
uhal::ClientFactory::ClientInfo::description
std::string description
Definition: ClientFactory.hpp:192
log.hpp
ClientFactory.hxx
uhal::ClientFactory::RegistrationHelper
Definition: ClientFactory.hpp:120
uhal::ClientFactory::RegistrationHelper::sInitialised
static bool sInitialised
Dummy variable required as initialisation target.
Definition: ClientFactory.hpp:123
uhal::URI
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50
exception.hpp
ClientInterface.hpp
uhal::ClientFactory::Creator::~Creator
virtual ~Creator()
Destructor.
Definition: ClientFactory.hpp:173