gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
plugin_macros.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
10 //
21 
26 
27 #ifndef SCANNER_PLUGIN_MACROS_HPP
28 #define SCANNER_PLUGIN_MACROS_HPP
29 
30 // Tell clang not to warn about usage of extern "C" even when the types involved are C++ (we just use it to avoid name-mangling)
31 #ifdef __clang__
32  #pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
33 #endif
34 
39 #define export_abstract(name, ...) EXPORT_ABSTRACT(name, __VA_ARGS__)
40 #define export_object(name, ...) EXPORT_OBJECT(name, __VA_ARGS__)
42 #define plugin_main(...) PLUGIN_MAIN( __VA_ARGS__ )
44 #define gambit_plugin(...) GAMBIT_PLUGIN( __VA_ARGS__ )
46 #define plugin_constructor PLUGIN_CONSTRUCTOR
48 #define plugin_deconstructor PLUGIN_DECONSTRUCTOR
50 #define __PLUGIN_version(...) __PLUGIN_VERSION( __VA_ARGS__ )
52 #define __PLUGIN_no_version __PLUGIN_VERSION()
54 #define reqd_inifile_entries(...) void reqd_inifile_entries()
56 #define reqd_libraries(...) void reqd_libraries()
58 #define reqd_headers(...) void reqd_headers()
60 #define set_flag(...) void set_flag()
62 #define cxx_flags(...) void scc_flags()
63 
65 #define REQD_INIFILE_ENTRIES(...)
66 #define REQD_LIRBARIES(...)
67 #define REQD_HEADERS(...)
68 #define SET_FLAG(...)
69 #define CXX_FLAGS(...)
70 
71 #define _ARG_N_(_1_, _2_, _3_, _4_, ret, ...) ret
72 #define __ARG_N__(...) _ARG_N_(__VA_ARGS__ , 4, 3, 2, 1, 0)
73 #define __COMBINE__(a, b) _COMBINE_(a, b)
74 #define _COMBINE_(a, b) a ## b
75 #define _STR_(a) #a
76 #define __STR__(a) _STR_(a)
77 
78 #define __PLUGIN_VERSION___4(major, minor, patch, release) major ## _ ## minor ## _ ## patch ## _ ## release
79 #define __PLUGIN_VERSION___3(major, minor, patch) __PLUGIN_VERSION___4(major, minor, patch, )
80 #define __PLUGIN_VERSION___2(major, minor) __PLUGIN_VERSION___4(major, minor,,)
81 #define __PLUGIN_VERSION___1(major) __PLUGIN_VERSION___4(major,,,)
82 #define __PLUGIN_VERSION___0() __PLUGIN_VERSION___(,,,)
83 #define __PLUGIN_VERSION(...) __COMBINE__(__PLUGIN_VERSION___,__ARG_N__(__VA_ARGS__))( __VA_ARGS__ )
84 
85 /*Allows Gambit to declare an object of type "..."*/
86 #define EXPORT_ABSTRACT(name, ...) \
87 namespace __gambit_plugin_namespace__ \
88 { \
89  namespace LoadTags \
90  { \
91  struct name{}; \
92  } \
93  \
94  namespace \
95  { \
96  template<> \
97  class interface <LoadTags::name> \
98  { \
99  public: \
100  \
101  interface(pluginData &myData) \
102  { \
103  myData.inits.push_back(interface <LoadTags::name>::init); \
104  } \
105  \
106  static void init(pluginData &myData) \
107  { \
108  myData.outputFuncs[#name] = new Gambit::Scanner::Plugins::classFactory<__VA_ARGS__>; \
109  } \
110  \
111  ~interface() \
112  { \
113  if (myData.outputFuncs.find(#name) != myData.outputFuncs.end()) \
114  delete myData.outputFuncs[#name]; \
115  } \
116  \
117  }; \
118  \
119  template <> \
120  interface <LoadTags::name> reg_init <LoadTags::name>::reg(myData); \
121  } \
122 } \
123 
124 /*Allows Gambit to use object "obj" of type "..."*/
125 #define EXPORT_OBJECT(name, ...) \
126 namespace __gambit_plugin_namespace__ \
127 { \
128  namespace LoadTags \
129  { \
130  struct name{}; \
131  } \
132  \
133  namespace \
134  { \
135  template<> \
136  class interface <LoadTags::name> \
137  { \
138  public: \
139  \
140  interface(pluginData &myData) \
141  { \
142  myData.inits.push_back(interface <LoadTags::name>::init); \
143  } \
144  \
145  static void init(pluginData &myData) \
146  { \
147  myData.outputFuncs[#name] \
148  = new Gambit::Scanner::Plugins::funcFactory <decltype(__VA_ARGS__)>(&__VA_ARGS__); \
149  } \
150  \
151  ~interface() \
152  { \
153  if (myData.outputFuncs.find(#name) != myData.outputFuncs.end()) \
154  delete myData.outputFuncs[#name]; \
155  } \
156  \
157  }; \
158  \
159  template <> \
160  interface <LoadTags::name> reg_init <LoadTags::name>::reg(myData); \
161  } \
162 } \
163 
164 //constructor
165 #define PLUGIN_CONSTRUCTOR \
166 void PLUGIN_CONSTRUCTOR(); \
167 namespace __gambit_plugin_namespace__ \
168 { \
169  namespace \
170  { \
171  template<> \
172  class interface <void (int)> \
173  { \
174  public: \
175  \
176  interface(pluginData &myData) \
177  { \
178  myData.inits.push_back(interface <void (int)>::init); \
179  } \
180  \
181  static void init(pluginData&) \
182  { \
183  PLUGIN_CONSTRUCTOR(); \
184  } \
185  }; \
186  \
187  template <> \
188  interface <void (int)> reg_init <void (int)>::reg(myData); \
189  } \
190 } \
191 void PLUGIN_CONSTRUCTOR() \
192 
193 #define PLUGIN_DECONSTRUCTOR \
194 void PLUGIN_DECONSTRUCTOR(); \
195 namespace __gambit_plugin_namespace__ \
196 { \
197  namespace \
198  { \
199  template<> \
200  class interface <void (void)> \
201  { \
202  public: \
203  \
204  interface(pluginData &myData) \
205  { \
206  myData.inits.push_back(interface <void (void)>::init); \
207  } \
208  \
209  static void init(pluginData &myData) \
210  { \
211  myData.deconstructor = PLUGIN_DECONSTRUCTOR; \
212  } \
213  }; \
214  \
215  template <> \
216  interface <void (void)> reg_init <void (void)>::reg(myData); \
217  } \
218 } \
219 void PLUGIN_DECONSTRUCTOR() \
220 
221 #define __PLUGIN_MAIN_RET_VAL__(...) \
222 decltype(__plugin_main_ret_val__(typename Gambit::Scanner::func_ptr_type<void (__VA_ARGS__)>::type())) \
223 
224 /*Declared the "main" for the module. This is function that will be ran by the module interface*/
225 #define PLUGIN_MAIN(...) \
226  __plugin_main_ret_val__(void (*)(__VA_ARGS__)); \
227 __PLUGIN_MAIN_RET_VAL__(__VA_ARGS__) PLUGIN_MAIN (__VA_ARGS__); \
228 namespace __gambit_plugin_namespace__ \
229 { \
230  namespace \
231  { \
232  template<> \
233  class interface <int (__VA_ARGS__)> \
234  { \
235  public: \
236  \
237  interface(pluginData &myData) \
238  { \
239  myData.inits.push_back(interface <int (__VA_ARGS__)>::init); \
240  } \
241  \
242  static void init(pluginData &myData) \
243  { \
244  myData.plugin_mains[typeid(__PLUGIN_MAIN_RET_VAL__(__VA_ARGS__) (__VA_ARGS__))] = \
245  Gambit::Scanner::Plugins::funcFactory \
246  <__PLUGIN_MAIN_RET_VAL__(__VA_ARGS__) (__VA_ARGS__)>(PLUGIN_MAIN)(); \
247  } \
248  }; \
249  \
250  template <> \
251  interface <int (__VA_ARGS__)> reg_init <int (__VA_ARGS__)>::reg(myData); \
252  } \
253 } \
254 __PLUGIN_MAIN_RET_VAL__(__VA_ARGS__) PLUGIN_MAIN (__VA_ARGS__) \
255 
256 /*Defines a Gambit plugin*/
257 #define __GAMBIT_PLUGIN__(plug_name, plug_type, plug_version) \
258 namespace __gambit_plugin_ ## plug_name ## __t__ ## plug_type ## __v__ ## plug_version ## _namespace__ \
259 { \
260  namespace __gambit_plugin_namespace__ \
261  { \
262  using Gambit::Scanner::Plugins::pluginData; \
263  \
264  namespace LoadTags \
265  { \
266  struct plug_name{}; \
267  } \
268  \
269  namespace \
270  { \
271  pluginData myData( #plug_name , #plug_type, #plug_version ); \
272  \
273  template <class T> \
274  class interface {}; \
275  \
276  template <class T> \
277  struct reg_init \
278  { \
279  static interface <T> reg; \
280  }; \
281  } \
282  \
283  extern "C" const std::map<Gambit::type_index, void *> & \
284  __gambit_plugin_pluginInit_ ## plug_name ## __t__ ## plug_type ## __v__ ## plug_version ## __ \
285  (const std::string *tag, const YAML::Node *node, Gambit::Scanner::printer_interface &printer, \
286  Gambit::Scanner::prior_interface &prior, std::vector<void *> *input ) \
287  { \
288  if (!myData.loaded) \
289  { \
290  myData.tag = *tag; \
291  myData.printer = &printer; \
292  myData.prior = &prior; \
293  myData.loaded = true; \
294  \
295  if (input != 0) \
296  myData.inputData = *input; \
297  \
298  myData.node = *node; \
299  \
300  for(auto it = myData.inits.begin(), end = myData.inits.end(); it != end; it++) \
301  { \
302  (*it)(myData); \
303  } \
304  \
305  myData.inits.clear(); \
306  } \
307  else \
308  { \
309  scan_err << "plugin:\n" << __gambit_plugin_namespace__::myData.print() \
310  << "is already loaded" << scan_end; \
311  } \
312  \
313  return myData.plugin_mains; \
314  } \
315  \
316  extern "C" void * \
317  __gambit_plugin_getMember_ ## plug_name ## __t__ ## plug_type ## __v__ ## plug_version ## __ \
318  (std::string in)\
319  { \
320  if (myData.outputFuncs.find(in) != myData.outputFuncs.end()) \
321  { \
322  return (*myData.outputFuncs[in])(); \
323  } \
324  else \
325  return NULL; \
326  } \
327  } \
328  \
329  inline Gambit::Scanner::printer_interface &get_printer() \
330  { \
331  return *__gambit_plugin_namespace__::myData.printer; \
332  } \
333  \
334  inline Gambit::Scanner::prior_interface &get_prior() \
335  { \
336  return *__gambit_plugin_namespace__::myData.prior; \
337  } \
338  \
339  template <typename T> \
340  inline T get_inifile_value(std::string in) \
341  { \
342  if (!__gambit_plugin_namespace__::myData.node[in]) \
343  { \
344  scan_err << "Missing iniFile entry \""<< in << "\" needed by a gambit plugin: \n" \
345  << __gambit_plugin_namespace__::myData.print() << scan_end; \
346  return T(); \
347  } \
348  \
349  return __gambit_plugin_namespace__::myData.node[in].as<T>(); \
350  } \
351  \
352  inline YAML::Node get_inifile_node(std::string in) \
353  { \
354  return __gambit_plugin_namespace__::myData.node[in]; \
355  } \
356  \
357  inline YAML::Node get_inifile_node() \
358  { \
359  return __gambit_plugin_namespace__::myData.node; \
360  } \
361  \
362  template <typename T> \
363  inline T get_inifile_value(std::string in, T defaults) \
364  { \
365  if (!__gambit_plugin_namespace__::myData.node[in]) \
366  { \
367  return defaults; \
368  } \
369  \
370  return __gambit_plugin_namespace__::myData.node[in].as<T>(); \
371  } \
372  \
373  template <> \
374  inline YAML::Node get_inifile_value<YAML::Node>(std::string in) \
375  { \
376  if (!__gambit_plugin_namespace__::myData.node[in]) \
377  { \
378  scan_err << "Missing iniFile node \""<< in << "\" needed by a gambit plugin: \n" \
379  << __gambit_plugin_namespace__::myData.print() << scan_end; \
380  YAML::Node node; \
381  return node; \
382  } \
383  return __gambit_plugin_namespace__::myData.node[in]; \
384  } \
385  \
386  template <> \
387  inline YAML::Node get_inifile_value<YAML::Node>(std::string in, YAML::Node defaults) \
388  { \
389  if (!__gambit_plugin_namespace__::myData.node[in]) \
390  { \
391  return defaults; \
392  } \
393  \
394  return __gambit_plugin_namespace__::myData.node[in]; \
395  } \
396  \
397  template <typename T> \
398  inline T &get_input_value(int i) \
399  { \
400  return *static_cast<T*>(__gambit_plugin_namespace__::myData.inputData[i]); \
401  } \
402  \
403  Gambit::Scanner::resume_params_func \
404  set_resume_params(__STR__(plug_name ## __t__ ## plug_type ## __v__ ## plug_version)); \
405 } \
406 namespace __gambit_plugin_ ## plug_name ## __t__ ## plug_type ## __v__ ## plug_version ## _namespace__ \
407 
408 
409 #define _GAMBIT_PLUGIN_(plug_name, plug_type, plug_version) \
410  __GAMBIT_PLUGIN__(plug_name, plug_type, plug_version) \
411 
412 #define GAMBIT_PLUGIN_1(plugin_name) \
413  _GAMBIT_PLUGIN_(plug_name,, __PLUGIN_no_version) \
414 
415 #define GAMBIT_PLUGIN_2(plugin_name, plug_type) \
416  _GAMBIT_PLUGIN_(plug_name, plug_type, __PLUGIN__no_version) \
417 
418 #define GAMBIT_PLUGIN_3(plug_name, plug_type, plug_version) \
419  _GAMBIT_PLUGIN_(plug_name, plug_type, __PLUGIN_ ## plug_version) \
420 
421 #define GAMBIT_PLUGIN(...) __COMBINE__(GAMBIT_PLUGIN_, __ARG_N__(__VA_ARGS__))(__VA_ARGS__ )
422 
423 
424 #define ___GAMBIT_PLUGIN_NAMESPACE___(plug_name, plug_type, plug_version) \
425 namespace __gambit_plugin_ ## plug_name ## __t__ ## plug_type ## __v__ ## plug_version ## _namespace__ \
426 
427 #define _GAMBIT_PLUGIN_NAMESPACE_(plug_name, plug_type, plug_version) \
428  ___GAMBIT_PLUGIN_NAMESPACE___(plug_name, plug_type, plug_version) \
429 
430 #define __GAMBIT_PLUGIN_NAMESPACE___1(plug_name) \
431  _GAMBIT_PLUGIN_NAMESPACE_(plug_name,, __PLUGIN_no_version) \
432 
433 #define __GAMBIT_PLUGIN_NAMESPACE___2(plug_name, plug_type) \
434  _GAMBIT_PLUGIN_NAMESPACE_(plug_name, plug_type, __PLUGIN_no_version) \
435 
436 #define __GAMBIT_PLUGIN_NAMESPACE___3(plug_name, plug_type, plug_version) \
437  _GAMBIT_PLUGIN_NAMESPACE_(plug_name, plug_type, __PLUGIN_ ## plug_version) \
438 
439 #define __GAMBIT_PLUGIN_NAMESPACE__(...) \
440  __COMBINE__(__GAMBIT_PLUGIN_NAMESPACE___, __ARG_N__(__VA_ARGS__))(__VA_ARGS__) \
441 
442 
443 #define GAMBIT_PLUGIN_INITIALIZE(setup, ...) \
444 GAMBIT_PLUGIN(__VA_ARGS__) \
445 { \
446  setup \
447 } \
448 __GAMBIT_PLUGIN_NAMESPACE__(__VA_ARGS__) \
449 
450 #endif
Variadic utilty functions.
declaration for scanner module
Class to hold details of scanner plugins and define simple comparison operations on them...
Variadic utilty functions.