gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
scanner_utils.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
23 
24 #ifndef __scanner_utils_hpp__
25 #define __scanner_utils_hpp__
26 
27 #include <type_traits>
28 #include <utility>
29 #include <ostream>
30 #include <sstream>
31 #include <unordered_map>
32 #include <algorithm>
33 #include <cctype>
34 #include <boost/iterator/zip_iterator.hpp>
35 #include <boost/range.hpp>
36 
37 #ifdef __GNUG__
38  #include <cstdlib>
39  #include <memory.h>
40  #include <cxxabi.h>
41 #endif
42 
49 
54 #define scan_err SCAN_ERR
55 #define scan_warn SCAN_WARN
56 #define scan_end SCAN_END
57 #define scan_flush SCAN_FLUSH
58 
60 #define SCAN_ERR \
61 Gambit::Scanner::Errors::_bool_() = true, \
62 Gambit::Scanner::Errors::_err_() \
63 
64 #define SCAN_WARN \
65 Gambit::Scanner::Errors::_bool_() = false, \
66 Gambit::Scanner::Errors::_warn_() \
67 
68 #define SCAN_END_INTERNAL \
69 (Gambit::Scanner::Errors::_bool_()) ? \
70 ( \
71  Gambit::Scanner::scan_error().raise(LOCAL_INFO, \
72  Gambit::Scanner::Errors::_err_().str()), \
73  Gambit::Scanner::Errors::_err_().str("") \
74 ) \
75 : \
76 ( \
77  Gambit::Scanner::scan_warning().raise(LOCAL_INFO, \
78  Gambit::Scanner::Errors::_warn_().str()), \
79  Gambit::Scanner::Errors::_warn_().str("") \
80 ) \
81 
82 #define SCAN_END std::endl, SCAN_END_INTERNAL
83 
84 #define SCAN_FLUSH std::flush, SCAN_END_INTERNAL \
85 
86 namespace Gambit
87 {
88  namespace Scanner
89  {
90  /**********************************/
91  /****** error and warnings ********/
92  /**********************************/
93 
94  namespace Errors
95  {
96  inline std::stringstream &_err_()
97  {
98  static std::stringstream error;
99  return error;
100  }
101 
102  inline std::stringstream &_warn_()
103  {
104  static std::stringstream warn;
105  return warn;
106  }
107 
108  inline bool &_bool_()
109  {
110  static bool error;
111  return error;
112  }
113  }
118 
119  /**********************************/
120  /****** zip for range loop ********/
121  /**********************************/
122 
124  template <typename... T>
125  inline auto zip(const T&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>>
126  {
127  return boost::make_iterator_range(
128  boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...)),
129  boost::make_zip_iterator(boost::make_tuple(std::end(containers)...)));
130  }
131 
132  /*********************************/
133  /****** demangle function ********/
134  /*********************************/
135 
137  inline std::string demangle(const std::string &in)
138  {
139 #ifdef __GNUG__
140  int status = -4;
141  char *result = abi::__cxa_demangle (in.c_str(), NULL, NULL, &status);
142 
143  if (status)
144  {
145  scan_err << "Error demangling \"" << in << "\" status code: " << status << scan_end;
146  return in;
147  }
148 
149  std::string ret(result);
150  std::free(result);
151  return ret;
152 #else
153  return in;
154 #endif
155  }
156 
157  /****************************************/
158  /****** get_yaml_vector function ********/
159  /****************************************/
160 
165  template <typename T>
166  inline std::vector<T> get_yaml_vector(const YAML::Node &node)
167  {
168  if (node.IsSequence())
169  {
170  return node.as< std::vector<T> >();
171  }
172  else if (node.IsScalar())
173  {
174  std::string plug = node.as<std::string>();
175 
176  std::string::size_type pos = plug.find_first_of(",;");
177  while (pos != std::string::npos)
178  {
179  plug[pos] = ' ';
180  pos = plug.find_first_of(",;", pos + 1);
181  }
182 
183  std::stringstream ss;
184  ss << plug;
185  std::vector<T> ret;
186  T temp;
187  while (ss >> temp) ret.push_back(temp);
188 
189  return ret;
190  }
191  else if (node.IsMap())
192  {
193  std::vector<T> ret;
194  for (auto it = node.begin(), end = node.end(); it != end; ++it)
195  {
196  ret.push_back(it->first.as<T>());
197  }
198 
199  return ret;
200  }
201  else
202  {
203  scan_err << "\"" << node << "\" input value not usable in the inifile." << scan_end;
204  return std::vector <T> ();
205  }
206  }
207 
208  /*****************************/
209  /****** func_ptr_type ********/
210  /*****************************/
211 
212  template <typename T> struct func_ptr_type;
213 
214  template <typename... args>
215  struct func_ptr_type <void (args...)>
216  {
217  typedef void (*type) (args...);
218  };
219 
220  /*************************************/
221  /****** input_variadic_vector ********/
222  /*************************************/
223 
226  inline void input_variadic_vector(std::vector<void *> &){}
227 
228  template <typename T, typename... args>
229  inline void input_variadic_vector(std::vector<void *> &input, const T& value, const args&... params)
230  {
231  input.push_back((void *)&value);
232  input_variadic_vector(input, params...);
233  }
235 
236  /*****************************/
237  /****** String to Int ********/
238  /*****************************/
239 
241  inline int StringToInt(const std::string &str)
242  {
243  int ret;
244  std::stringstream ss(str);
245  if (ss >> ret)
246  return ret;
247  else
248  return 0;
249  }
250 
252  inline std::string IntToString(const int &in)
253  {
254  std::stringstream ss;
255  ss << in;
256  return ss.str();
257  }
258 
259  /********************************/
260  /********* pi function **********/
261  /********************************/
262 
264  inline double pi() {return 3.14159265358979323846;}
265 
266  /***********************************/
267  /********* convert_to_map **********/
268  /***********************************/
269 
271  inline std::map<std::string, std::vector<std::string>> convert_to_map(const std::vector<std::string> &vec)
272  {
273  std::map<std::string, std::vector<std::string>> ret;
274 
275  for (auto it = vec.begin(), end = vec.end(); it != end; it++)
276  {
277  std::string::size_type pos = it->find("::");
278  ret[it->substr(0, pos)].push_back(*it);
279  }
280 
281  return ret;
282  }
283 
284  /*******************************************/
285  /********* scanner_plugin_def_ret **********/
286  /*******************************************/
287 
290  template <typename ret>
291  typename std::enable_if<!std::is_floating_point<ret>::value, ret>::type scanner_plugin_def_ret()
292  {
293  return ret();
294  }
295 
296  template <typename ret>
297  typename std::enable_if<std::is_floating_point<ret>::value, ret>::type scanner_plugin_def_ret()
298  {
299  return -std::pow(10.0, std::numeric_limits<double>::max_exponent10);
300  };
302 
303  /********************************/
304  /******** pow function **********/
305  /********************************/
306 
309  template <int i>
310  inline double pow(const double &a)
311  {
312  return a*pow<i-1>(a);
313  };
314 
315  template <>
316  inline double pow<0>(const double &)
317  {
318  return 1.0;
319  };
320 
321  template <>
322  inline double pow<1>(const double &a)
323  {
324  return a;
325  };
326 
327  template <int i>
328  inline int pow(const int &a)
329  {
330  return a*pow<i-1>(a);
331  };
332 
333  template <>
334  inline int pow<0>(const int &)
335  {
336  return 1;
337  };
338 
339  template <>
340  inline int pow<1>(const int &a)
341  {
342  return a;
343  };
345 
346  /********************************/
347  /****** Remove All Func *********/
348  /********************************/
349 
350  template <typename T>
352 
353  template <typename ret, typename... args>
354  struct remove_all_func <ret (args...)>
355  {
356  typedef ret type(typename remove_all<args>::type...);
357  };
358 
359  /***************************/
360  /****** return type ********/
361  /***************************/
362 
363  template <typename...>
364  struct return_type;
365 
366  template <typename ret, typename... args>
367  struct return_type <ret (args...)>
368  {
369  typedef ret type;
370  };
371 
372  /******************************/
373  /****** first_arg_type ********/
374  /******************************/
375 
376  template <typename...>
378 
379  template <typename ret, typename T, typename... args>
380  struct first_arg_type <ret (T, args...)>
381  {
382  typedef T type;
383  };
384 
385  template <typename ret>
386  struct first_arg_type <ret ()>
387  {
388  typedef void type;
389  };
390 
391  /***********************************/
392  /****** is_args_convertible ********/
393  /***********************************/
394 
395  template <typename... T>
397  {
398  static const bool value = false;
399  };
400 
401  template <bool, typename T1, typename T2>
403  {
404  static const bool value = is_args_convertible<T1, T2>::value;
405  };
406 
407  template <typename T1, typename T2>
408  struct __is_args_convertible__<false, T1, T2>
409  {
410  static const bool value = false;
411  };
412 
413  template <typename ret1, typename ret2, typename arg1, typename arg2, typename... args1, typename... args2>
414  struct is_args_convertible <ret1 (arg1, args1...), ret2 (arg2, args2...)>
415  {
416  static const bool value = __is_args_convertible__<std::is_convertible<arg1, arg2>::value, ret1 (args1...), ret2 (args2...)>::value;
417  };
418 
419  template <typename ret1, typename ret2>
420  struct is_args_convertible <ret1(), ret2()>
421  {
422  static const bool value = true;
423  };
424 
425  /********************************************/
426  /****** find_variadic_type_not_exact ********/
427  /********************************************/
428 
429  template <typename... T>
431 
432  template <typename... T>
434 
435  template <typename T1, typename T2, typename... T>
436  struct _find_variadic_type_not_exact_<T1, T2, T...>
437  {
439  typedef typename __find_variadic_type_not_exact__ <typename remove_all_func<T1>::type, typename remove_all_func<T2>::type, T2, T...>::func_type func_type;
440  static const bool value = __find_variadic_type_not_exact__ <typename remove_all_func<T1>::type, typename remove_all_func<T2>::type, T2, T...>::value;
441  };
442 
443  template <typename... T>
445  {
448  static const bool value = _find_variadic_type_not_exact_ <T...>::value;
449  };
450 
451  template <typename... args>
453  {
454  typedef int ret_type;
455  typedef void func_type;
456  static const bool value = false;
457  };
458 
459  template <typename ret, typename... args, typename T1, typename... T>
460  struct __find_variadic_type_not_exact__ <void (args...), ret (args...), T1, T...>
461  {
462  typedef ret ret_type;
463  typedef T1 func_type;
464  static const bool value = true;
465  };
466 
467  template <typename ret, typename... args, typename... args2, typename T1, typename... T>
468  struct __find_variadic_type_not_exact__ <void (args...), ret (args2...), T1, T...>
469  {
472  static const bool value = find_variadic_type_not_exact <void (args...), T...>::value;
473  };
474 
475 
476  /****************************************/
477  /****** find_variadic_type_exact ********/
478  /****************************************/
479 
480  template <typename... T>
482 
483  template <typename ret, typename... args, typename... args2, typename... T>
484  struct find_variadic_type_exact<void (args...), ret (args2...), T...>
485  {
486  typedef typename find_variadic_type_exact<void (args...), T...>::ret_type ret_type;
488  static const bool value = find_variadic_type_exact<void (args...), T...>::value;
489  };
490 
491  template <typename ret, typename... args, typename... T>
492  struct find_variadic_type_exact<void (args...), ret (args...), T...>
493  {
494  typedef ret ret_type;
495  typedef ret func_type(args...);
496  static const bool value = true;
497  };
498 
499  template <typename... args>
501  {
502  typedef int ret_type;
503  typedef void func_type;
504  static const bool value = false;
505  };
506 
507  /******************************************/
508  /****** find_variadic_type_convert ********/
509  /******************************************/
510 
511  template<bool, typename... T>
513 
514  template<typename... T>
516 
517  template<typename Tc, typename T1, typename... T>
518  struct _find_variadic_type_convert_<Tc, T1, T...>
519  {
521  typedef typename __find_variadic_type_convert__<is_args_convertible<Tc, T1>::value, T1, T...>::func_type func_type;
522  static const bool value = __find_variadic_type_convert__<is_args_convertible<Tc, T1>::value, T1, T...>::value;
523  };
524 
525  template <typename... T>
527  {
530  static const bool value = _find_variadic_type_convert_<T...>::value;
531  };
532 
533  template <typename ret, typename... args>
535  {
536  typedef int ret_type;
537  typedef void func_type;
538  static const bool value = false;
539  };
540 
541  template <bool b, typename Tc, typename T1, typename... T>
542  struct __find_variadic_type_convert__<b, Tc, T1, T...>
543  {
544  typedef typename find_variadic_type_convert<Tc, T...>::ret_type ret_type;
546  static const bool value = find_variadic_type_convert<Tc, T...>::value;
547  };
548 
549  template <typename Tc, typename T1, typename... T>
550  struct __find_variadic_type_convert__<true, Tc, T1, T...>
551  {
552  typedef typename return_type<T1>::type ret_type;
553  typedef T1 func_type;
554  static const bool value = true;
555  };
556 
557  /**********************************/
558  /****** find_variadic_type ********/
559  /**********************************/
560 
561  template <bool, bool, bool, typename...>
563  {
564  typedef int ret_type;
565  typedef void func_type;
566  static const bool value = false;
567  };
568 
569  template <typename... T>
571  {
574  static const bool value = __find_variadic_type__<find_variadic_type_exact<T...>::value,find_variadic_type_not_exact<T...>::value,find_variadic_type_convert<T...>::value, T...>::value;
575  };
576 
577  template <bool b1, bool b2, typename... T>
578  struct __find_variadic_type__<true, b1, b2, T...>
579  {
582  static const bool value = true;
583  };
584 
585  template <bool b2, typename... T>
586  struct __find_variadic_type__<false, true, b2, T...>
587  {
590  static const bool value = true;
591  };
592 
593  template <typename... T>
594  struct __find_variadic_type__<false, false, true, T...>
595  {
598  static const bool value = true;
599  };
600 
601  /********************************/
602  /****** Stream Operators ********/
603  /********************************/
604 
607  template <typename T>
608  inline typename std::enable_if <is_container<T>::value, std::ostream &>::type
609  operator << (std::ostream &out, const T &in)
610  {
611  if (in.size() == 0)
612  return out << "[]";
613 
614 
615  auto it = in.begin();
616  auto end = in.end();
617  out << "[" << *it;
618  for (++it; it != end; ++it)
619  {
620  out << ", " << *it;
621  }
622 
623  return out << "]";
624  }
625 
626  template <typename T>
627  inline typename std::enable_if <is_pair<T>::value, std::ostream &>::type
628  operator << (std::ostream &out, const T &in)
629  {
630  return out << "{" << in.first << " : " << in.second << "}";
631  }
633 
634  /********************************/
635  /****** Output Functions ********/
636  /********************************/
637 
640  template<typename T>
641  inline typename std::enable_if<!is_container<T>::value && !is_pair<T>::value, void>::type
642  resume_file_output(std::ofstream &out, T &param)
643  {
644  out.write(reinterpret_cast<char *>(&param), sizeof(T));
645  //out << param << std::endl;
646  };
647 
648  template <typename T>
649  inline typename std::enable_if <is_container<T>::value, void>::type
650  resume_file_output (std::ofstream &out, T &param)
651  {
652  for (auto it = param.begin(); it != param.end(); it++)
653  {
654  resume_file_output(out, *it);
655  }
656  }
657 
658  template <typename T>
659  inline typename std::enable_if <is_pair<T>::value, void>::type
660  resume_file_output (std::ofstream &out, T &param)
661  {
662  resume_file_output(out, param.first);
663  resume_file_output(out, param.second);
664  }
665 
666  template<typename T>
667  typename std::enable_if<!is_container<T>::value && !is_pair<T>::value, void>::type
668  resume_file_input(std::ifstream &in, T &param)
669  {
670  in.read((char *)&param, sizeof(T));
671  //in >> param;
672  };
673 
674  template <typename T>
675  inline typename std::enable_if <is_container<T>::value, void>::type
676  resume_file_input (std::ifstream &in, T &param)
677  {
678  for (auto it = param.begin(); it != param.end(); it++)
679  {
680  resume_file_input(in, *it);
681  }
682  }
683 
684  template <typename T>
685  inline typename std::enable_if <is_pair<T>::value, void>::type
686  resume_file_input (std::ifstream &in, T &param)
687  {
688  resume_file_input(in, param.first);
689  resume_file_input(in, param.second);
690  }
691 
692  template<typename T>
693  inline typename std::enable_if<!is_container<T>::value && !is_pair<T>::value, size_t>::type
695  {
696  return sizeof(T);
697  };
698 
699  template <typename T>
700  inline typename std::enable_if <is_container<T>::value, size_t>::type
701  resume_size_of (T &param)
702  {
703  return param.size()*sizeof(typename is_container<T>::type);
704  }
705 
706  template <typename T>
707  inline typename std::enable_if <is_pair<T>::value, size_t>::type
709  {
710  return sizeof(typename is_pair<T>::first_type) + sizeof(typename is_pair<T>::second_type);
711  }
712 
713  inline void resume_file_input(std::ifstream &in, std::string &param)
714  {
715  in.read((char *)param.c_str(), param.length());
716  }
717 
718  inline void resume_file_output(std::ofstream &out, std::string &param)
719  {
720  out.write((char *)param.c_str(), param.length());
721  }
722 
723  inline size_t resume_size_of(std::string &param)
724  {
725  return param.length();
726  }
728  }
729 }
730 
731 #endif
Utility functions used by ScannerBit and the GAMBIT printers to register available scanners and print...
int StringToInt(const std::string &str)
Converts a string to an int.
std::stringstream & _warn_()
General small utility macros.
double pow< 1 >(const double &a)
__is_pair__< typename remove_all< T >::type >::second_type second_type
std::enable_if<!std::is_floating_point< ret >::value, ret >::type scanner_plugin_def_ret()
Turns a type into an object.
LOCAL_INFO macro.
find_variadic_type_exact< T... >::ret_type ret_type
#define EXPORT_SYMBOLS
_find_variadic_type_convert_< T... >::func_type func_type
find_variadic_type_convert< T... >::func_type func_type
Threadsafe exception class declarations.
std::enable_if<!is_container< T >::value &&!is_pair< T >::value, void >::type resume_file_output(std::ofstream &out, T &param)
Functions to output data for the plugin resume functions.
void input_variadic_vector(std::vector< void *> &)
Inputs a varibadic pack into a vector.
__find_variadic_type__< find_variadic_type_exact< T... >::value, find_variadic_type_not_exact< T... >::value, find_variadic_type_convert< T... >::value, T... >::ret_type ret_type
__find_variadic_type_not_exact__< typename remove_all_func< T1 >::type, typename remove_all_func< T2 >::type, T2, T... >::func_type func_type
find_variadic_type_not_exact< T... >::func_type func_type
START_MODEL b
Definition: demo.hpp:270
__is_pair__< typename remove_all< T >::type >::first_type first_type
Declarations for the YAML options class.
GAMBIT warning class.
Definition: exceptions.hpp:165
std::stringstream & _err_()
find_variadic_type_exact< void(args...), T... >::ret_type ret_type
find_variadic_type_not_exact< void(args...), T... >::func_type func_type
_find_variadic_type_not_exact_< T... >::func_type func_type
find_variadic_type_convert< Tc, T... >::func_type func_type
GAMBIT error class.
Definition: exceptions.hpp:136
auto zip(const T &... containers) -> boost::iterator_range< boost::zip_iterator< decltype(boost::make_tuple(std::begin(containers)...))>>
Use for combine container in a range loop: for (auto &&x : zip(a, b)){...}.
find_variadic_type_exact< void(args...), T... >::func_type func_type
#define scan_err
Defined to macros to output errors in the form: scan_err << "error" << scan_end; scan_warn << "warnin...
std::map< std::string, std::vector< std::string > > convert_to_map(const std::vector< std::string > &vec)
Turns a vector with enters [model::parameter, ...] into a map with [{model, parameter}, ...].
std::enable_if<!is_container< T >::value &&!is_pair< T >::value, size_t >::type resume_size_of(T &)
__find_variadic_type_not_exact__< typename remove_all_func< T1 >::type, typename remove_all_func< T2 >::type, T2, T... >::ret_type ret_type
int pow(const int &a)
__find_variadic_type_convert__< is_args_convertible< Tc, T1 >::value, T1, T... >::ret_type ret_type
__find_variadic_type_convert__< is_args_convertible< Tc, T1 >::value, T1, T... >::func_type func_type
Variadic utilty functions.
ret type(typename remove_all< args >::type...)
hb_ModelParameters void
std::string str
Shorthand for a standard string.
Definition: Analysis.hpp:35
_find_variadic_type_not_exact_< T... >::ret_type ret_type
_find_variadic_type_convert_< T... >::ret_type ret_type
std::enable_if<!is_container< T >::value &&!is_pair< T >::value, void >::type resume_file_input(std::ifstream &in, T &param)
__is_container__< typename remove_all< T >::type >::type type
EXPORT_SYMBOLS warning & scan_warning()
Scanner warnings.
double pow(const double &a)
Outputs a^i.
#define scan_end
EXPORT_SYMBOLS error & scan_error()
Scanner errors.
std::vector< T > get_yaml_vector(const YAML::Node &node)
Input a vector from the yaml file of the following forms: vec: [a, b, ...] vec: a, b, ...
find_variadic_type_exact< T... >::func_type func_type
double pi()
Output pi.
double pow< 0 >(const double &)
std::string demangle(const std::string &in)
Demangles gnu c++ name.
std::vector< T > vec(std::vector< T > vector)
Definition: daFunk.hpp:142
find_variadic_type_not_exact< T... >::ret_type ret_type
__find_variadic_type__< find_variadic_type_exact< T... >::value, find_variadic_type_not_exact< T... >::value, find_variadic_type_convert< T... >::value, T... >::func_type func_type
find_variadic_type_convert< Tc, T... >::ret_type ret_type
std::remove_cv< typename std::remove_volatile< typename std::remove_const< typename std::remove_reference< T >::type >::type >::type >::type type
TODO: see if we can use this one:
Definition: Analysis.hpp:33
std::string IntToString(const int &in)
Converts a int into a string.
std::enable_if< is_container< T >::value, std::ostream & >::type operator<<(std::ostream &out, const T &in)
Outputs containers to an output stream.
find_variadic_type_not_exact< void(args...), T... >::ret_type ret_type