CHROMA
funcmap.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /*! @file
3  * @brief Map of function calls
4  */
5 
6 #ifndef __funcmap_h__
7 #define __funcmap_h__
8 
9 #include "chromabase.h"
10 
11 #include "typeinfo.h"
12 #include "typelist.h"
13 #include <map>
14 #include <exception>
15 
16 
17 namespace Chroma
18 {
19 
20  ////////////////////////////////////////////////////////////////////////////////
21  // class template DefaultFunctionMapError
22  // Manages the "Unknown Type" error in an object factory
23  ////////////////////////////////////////////////////////////////////////////////
24 
25  template <typename IdentifierType, class AbstractProduct>
27  {
28  struct Exception : public std::exception
29  {
30  const char* what() const throw() { return "Unknown Type"; }
31  };
32 
33  static AbstractProduct OnUnknownType(const IdentifierType& id)
34  {
35  throw Exception();
36  }
37  };
38 
39  ////////////////////////////////////////////////////////////////////////////////
40  // class template StringFunctionMapError
41  // Manages the "Unknown Type" error in an object factory
42  ////////////////////////////////////////////////////////////////////////////////
43 
44  template <typename IdentifierType, class AbstractProduct>
46  {
47  static AbstractProduct OnUnknownType(const IdentifierType& id)
48  {
49  std::ostringstream os;
50  os << "FunctionMap error: unknown identifier: id = " << id << std::endl;
51  throw os.str();
52  }
53  };
54 
55  ////////////////////////////////////////////////////////////////////////////////
56  // class template NullFunctionMapError
57  // Manages the "Unknown Type" error in an object factory
58  ////////////////////////////////////////////////////////////////////////////////
59 
60  template <typename IdentifierType, class AbstractProduct>
62  {
63  static AbstractProduct OnUnknownType(const IdentifierType&)
64  {
65  return zero;
66  }
67  };
68 
69 
70  ////////////////////////////////////////////////////////////////////////////////
71  // class template DefaultDisambiguator
72  ////////////////////////////////////////////////////////////////////////////////
73 
75 
76 
77  //! Object factory class
78  /*! @ingroup actions
79  *
80  * Supports abstract creation of objects
81  */
82  template<typename Disambiguator,
83  class AbstractProduct,
84  typename IdentifierType,
85  class TList = NullType,
86  typename ProductCall = AbstractProduct (*)(),
87  template<typename, class> class FunctionMapErrorPolicy = DefaultFunctionMapError>
88  class FunctionMap : public FunctionMapErrorPolicy<IdentifierType, AbstractProduct>
89  {
90  public:
91  // Handy type definitions for the body type
92  typedef TList ParmList;
99 
100  // Member functions
101  //! Register the function
102  /*!
103  * \param id function id
104  * \param call the calling function
105  * \return returns true if registration successful
106  */
107  bool registerFunction(const IdentifierType& id, ProductCall call)
108  {
109  return associations_.insert(
110  typename IdToProductMap::value_type(id, call)).second;
111  }
112 
113  //! Unregister the function
114  /*!
115  * \param id function id
116  * \return returns true if function was registered before
117  */
118  bool unregisterFunction(const IdentifierType& id)
119  {
120  return associations_.erase(id) == 1;
121  }
122 
123  //! Call the function
124  /*!
125  * \param id function id
126  * \return returns result of the function call
127  */
128  AbstractProduct callFunction(const IdentifierType& id)
129  {
130  typename IdToProductMap::const_iterator i = associations_.find(id);
131  if (i == associations_.end())
132  {
133  typedef typename IdToProductMap::const_iterator CI;
134  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
135  QDPIO::cerr << "Available Keys are : " << std::endl;
136  for( CI j = associations_.begin();
137  j != associations_.end(); j++) {
138  QDPIO::cerr << j->first << std::endl << std::flush;
139  }
140 
141  return this->OnUnknownType(id);
142  }
143  else {
144  return (i->second)();
145  }
146  }
147 
148  //! Call the function
149  /*!
150  * \param id function id
151  * \param p1 first parameter arg to function
152  * \return returns result of the function call
153  */
154  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1)
155  {
156  typename IdToProductMap::const_iterator i = associations_.find(id);
157  if (i == associations_.end())
158  {
159  typedef typename IdToProductMap::const_iterator CI;
160  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
161  QDPIO::cerr << "Available Keys are : " << std::endl;
162  for( CI j = associations_.begin();
163  j != associations_.end(); j++) {
164  QDPIO::cerr << j->first << std::endl << std::flush;
165  }
166 
167  return this->OnUnknownType(id);
168  }
169  else {
170  return (i->second)(p1);
171  }
172  }
173 
174  //! Call the function
175  /*!
176  * \param id function id
177  * \param p1 1st parameter arg to function
178  * \param p2 2nd parameter arg to function
179  * \return returns result of the function call
180  */
181  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1, Parm2 p2)
182  {
183  typename IdToProductMap::const_iterator i = associations_.find(id);
184  if (i == associations_.end())
185  {
186  typedef typename IdToProductMap::const_iterator CI;
187  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
188  QDPIO::cerr << "Available Keys are : " << std::endl;
189  for( CI j = associations_.begin();
190  j != associations_.end(); j++) {
191  QDPIO::cerr << j->first << std::endl << std::flush;
192  }
193 
194  return this->OnUnknownType(id);
195  }
196  else {
197  return (i->second)(p1, p2);
198  }
199  }
200 
201  //! Call the function
202  /*!
203  * \param id function id
204  * \param p1 1st parameter arg to function
205  * \param p2 2nd parameter arg to function
206  * \param p3 3rd parameter arg to function
207  * \return returns result of the function call
208  */
209  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1, Parm2 p2, Parm3 p3)
210  {
211  typename IdToProductMap::const_iterator i = associations_.find(id);
212  if (i == associations_.end())
213  {
214  typedef typename IdToProductMap::const_iterator CI;
215  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
216  QDPIO::cerr << "Available Keys are : " << std::endl;
217  for( CI j = associations_.begin();
218  j != associations_.end(); j++) {
219  QDPIO::cerr << j->first << std::endl << std::flush;
220  }
221 
222  return this->OnUnknownType(id);
223  }
224  else {
225  return (i->second)(p1, p2, p3);
226  }
227  }
228 
229  //! Call the function
230  /*!
231  * \param id function id
232  * \param p1 1st parameter arg to function
233  * \param p2 2nd parameter arg to function
234  * \param p3 3rd parameter arg to function
235  * \param p4 4th parameter arg to function
236  * \return returns result of the function call
237  */
238  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1, Parm2 p2, Parm3 p3,
239  Parm4 p4)
240  {
241  typename IdToProductMap::const_iterator i = associations_.find(id);
242  if (i == associations_.end())
243  {
244  typedef typename IdToProductMap::const_iterator CI;
245  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
246  QDPIO::cerr << "Available Keys are : " << std::endl;
247  for( CI j = associations_.begin();
248  j != associations_.end(); j++) {
249  QDPIO::cerr << j->first << std::endl << std::flush;
250  }
251 
252  return this->OnUnknownType(id);
253  }
254  else {
255  return (i->second)(p1, p2, p3, p4);
256  }
257  }
258 
259  //! Call the function
260  /*!
261  * \param id function id
262  * \param p1 1st parameter arg to function
263  * \param p2 2nd parameter arg to function
264  * \param p3 3rd parameter arg to function
265  * \param p4 4th parameter arg to function
266  * \param p5 5th parameter arg to function
267  * \return returns result of the function call
268  */
269  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1, Parm2 p2, Parm3 p3,
270  Parm4 p4, Parm5 p5)
271  {
272  typename IdToProductMap::const_iterator i = associations_.find(id);
273  if (i == associations_.end())
274  {
275  typedef typename IdToProductMap::const_iterator CI;
276  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
277  QDPIO::cerr << "Available Keys are : " << std::endl;
278  for( CI j = associations_.begin();
279  j != associations_.end(); j++) {
280  QDPIO::cerr << j->first << std::endl << std::flush;
281  }
282 
283  return this->OnUnknownType(id);
284  }
285  else {
286  return (i->second)(p1, p2, p3, p4, p5);
287  }
288  }
289 
290  //! Call the function
291  /*!
292  * \param id function id
293  * \param p1 1st parameter arg to function
294  * \param p2 2nd parameter arg to function
295  * \param p3 3rd parameter arg to function
296  * \param p4 4th parameter arg to function
297  * \param p5 5th parameter arg to function
298  * \param p6 6th parameter arg to function
299  * \return returns result of the function call
300  */
301  AbstractProduct callFunction(const IdentifierType& id, Parm1 p1, Parm2 p2, Parm3 p3,
302  Parm4 p4, Parm5 p5, Parm6 p6)
303  {
304  typename IdToProductMap::const_iterator i = associations_.find(id);
305  if (i == associations_.end())
306  {
307  typedef typename IdToProductMap::const_iterator CI;
308  QDPIO::cerr << "Couldnt find key " << id << " in the std::map: " << std::endl;
309  QDPIO::cerr << "Available Keys are : " << std::endl;
310  for( CI j = associations_.begin();
311  j != associations_.end(); j++) {
312  QDPIO::cerr << j->first << std::endl << std::flush;
313  }
314 
315  return this->OnUnknownType(id);
316  }
317  else {
318  return (i->second)(p1, p2, p3, p4, p5, p6);
319  }
320  }
321 
322 
323  private:
324  typedef std::map<IdentifierType, ProductCall> IdToProductMap;
326  };
327 
328 } // namespace Chroma
329 
330 #endif
Primary include file for CHROMA library code.
Object factory class.
Definition: funcmap.h:89
std::map< IdentifierType, ProductCall > IdToProductMap
Definition: funcmap.h:324
IdToProductMap associations_
Definition: funcmap.h:325
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1, Parm2 p2, Parm3 p3)
Call the function.
Definition: funcmap.h:209
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1, Parm2 p2)
Call the function.
Definition: funcmap.h:181
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5, Parm6 p6)
Call the function.
Definition: funcmap.h:301
bool unregisterFunction(const IdentifierType &id)
Unregister the function.
Definition: funcmap.h:118
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4, Parm5 p5)
Call the function.
Definition: funcmap.h:269
bool registerFunction(const IdentifierType &id, ProductCall call)
Register the function.
Definition: funcmap.h:107
TL::TypeAtNonStrict< ParmList, 5, NullType >::Result Parm6
Definition: funcmap.h:98
TL::TypeAtNonStrict< ParmList, 0, NullType >::Result Parm1
Definition: funcmap.h:93
TL::TypeAtNonStrict< ParmList, 1, NullType >::Result Parm2
Definition: funcmap.h:94
TL::TypeAtNonStrict< ParmList, 2, NullType >::Result Parm3
Definition: funcmap.h:95
AbstractProduct callFunction(const IdentifierType &id)
Call the function.
Definition: funcmap.h:128
TL::TypeAtNonStrict< ParmList, 4, NullType >::Result Parm5
Definition: funcmap.h:97
TL::TypeAtNonStrict< ParmList, 3, NullType >::Result Parm4
Definition: funcmap.h:96
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1)
Call the function.
Definition: funcmap.h:154
AbstractProduct callFunction(const IdentifierType &id, Parm1 p1, Parm2 p2, Parm3 p3, Parm4 p4)
Call the function.
Definition: funcmap.h:238
unsigned j
Definition: ldumul_w.cc:35
Asqtad Staggered-Dirac operator.
Definition: klein_gord.cc:10
int i
Definition: pbg5p_w.cc:55
Double zero
Definition: invbicg.cc:106
static AbstractProduct OnUnknownType(const IdentifierType &id)
Definition: funcmap.h:33
static AbstractProduct OnUnknownType(const IdentifierType &)
Definition: funcmap.h:63
static AbstractProduct OnUnknownType(const IdentifierType &id)
Definition: funcmap.h:47
Type info support.
Typelist support.