CMS 3D CMS Logo

AnyMethodArgument.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_AnyMethodArgument_h
2 #define CommonTools_Utils_AnyMethodArgument_h
3 
7 
8 #include <algorithm>
9 #include <cstdint>
10 #include <string>
11 #include <type_traits>
12 #include <variant>
13 
14 namespace reco {
15  namespace parser {
16 
17  // true if T matches one of the integral types in the default
18  // AnyMethodArgument variant
19  template <typename T>
25  };
26 
27  // size_t on 32-bit Os X is type unsigned long, which doesn't match uint32_t,
28  // so add unsigned long if it doesn't match any of the other integral types.
29  // Use "unsigned long" rather than size_t as PtrVector has unsigned long as
30  // size_type
31  typedef std::conditional<
33  std::variant<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, double, float, std::string>,
34  std::variant<int8_t,
35  uint8_t,
36  int16_t,
37  uint16_t,
38  int32_t,
39  uint32_t,
40  int64_t,
41  uint64_t,
42  unsigned long,
43  double,
44  float,
46 
48  private:
50  template <typename From, typename To>
51  std::pair<AnyMethodArgument, int> retOk_(const From &f, int cast) const {
52  return std::pair<AnyMethodArgument, int>(AnyMethodArgument(static_cast<To>(f)), cast);
53  }
54 
55  // correct return for each int output type
56  std::pair<AnyMethodArgument, int> doInt(int t) const {
57  if (dataType_ == typeid(int8_t)) {
58  return retOk_<int, int8_t>(t, 0);
59  }
60  if (dataType_ == typeid(uint8_t)) {
61  return retOk_<int, uint8_t>(t, 0);
62  }
63  if (dataType_ == typeid(int16_t)) {
64  return retOk_<int, int16_t>(t, 0);
65  }
66  if (dataType_ == typeid(uint16_t)) {
67  return retOk_<int, uint16_t>(t, 0);
68  }
69  if (dataType_ == typeid(int32_t)) {
70  return retOk_<int, int32_t>(t, 0);
71  }
72  if (dataType_ == typeid(uint32_t)) {
73  return retOk_<int, uint32_t>(t, 0);
74  }
75  if (dataType_ == typeid(int64_t)) {
76  return retOk_<int, int64_t>(t, 0);
77  }
78  if (dataType_ == typeid(uint64_t)) {
79  return retOk_<int, uint64_t>(t, 0);
80  }
81  if (dataType_ == typeid(unsigned long)) {
82  return retOk_<int, unsigned long>(t, 0);
83  } // harmless if unsigned long matches another type
84  if (dataType_ == typeid(double)) {
85  return retOk_<int, double>(t, 1);
86  }
87  if (dataType_ == typeid(float)) {
88  return retOk_<int, float>(t, 1);
89  }
90  return std::pair<AnyMethodArgument, int>(t, -1);
91  }
92 
93  public:
95 
96  // we handle all integer types through 'int', as that's the way they are parsed by boost::spirit
97  template <typename I>
98  typename std::enable_if<std::is_integral<I>::value, std::pair<AnyMethodArgument, int> >::type operator()(
99  const I &t) const {
100  return doInt(t);
101  }
102 
103  template <typename F>
104  typename std::enable_if<std::is_floating_point<F>::value, std::pair<AnyMethodArgument, int> >::type operator()(
105  const F &t) const {
106  if (dataType_ == typeid(double)) {
107  return retOk_<F, double>(t, 0);
108  }
109  if (dataType_ == typeid(float)) {
110  return retOk_<F, float>(t, 0);
111  }
112  return std::pair<AnyMethodArgument, int>(t, -1);
113  }
114 
115  std::pair<AnyMethodArgument, int> operator()(const std::string &t) const {
116  if (dataType_.isEnum()) {
117  if (dataType_.dataMemberSize() == 0) {
118  throw parser::Exception(t.c_str())
119  << "Enumerator '" << dataType_.name() << "' has no keys.\nPerhaps the dictionary is missing?\n";
120  }
121  int ival = dataType_.stringToEnumValue(t);
122  // std::cerr << " value is = " << dataType_.stringToEnumValue(t) << std::endl;
123  return std::pair<AnyMethodArgument, int>(ival, 1);
124  }
125  if (dataType_ == typeid(std::string)) {
126  return std::pair<AnyMethodArgument, int>(t, 0);
127  }
128  return std::pair<AnyMethodArgument, int>(t, -1);
129  }
130  };
131 
133  public:
134  template <typename T>
135  void *operator()(const T &t) const {
136  return const_cast<void *>(static_cast<const void *>(&t));
137  }
138  };
139  } // namespace parser
140 } // namespace reco
141 
142 #endif
int stringToEnumValue(std::string const &) const
std::enable_if< std::is_same< data_T, ap_uint< 1 > >::value &&std::is_same< typename CONFIG_T::weight_t, ap_uint< 1 > >::value, ap_int< nnet::ceillog2(CONFIG_T::n_in)+2 > >::type cast(typename CONFIG_T::accum_t x)
Definition: nnet_mult.h:88
std::pair< AnyMethodArgument, int > doInt(int t) const
std::pair< AnyMethodArgument, int > operator()(const std::string &t) const
std::enable_if< std::is_integral< I >::value, std::pair< AnyMethodArgument, int > >::type operator()(const I &t) const
const std::complex< double > I
Definition: I.h:8
double f[11][100]
Definition: value.py:1
AnyMethodArgumentFixup(const edm::TypeWithDict &type)
std::pair< AnyMethodArgument, int > retOk_(const From &f, int cast) const
bool isEnum() const
unsigned long long uint64_t
Definition: Time.h:13
std::conditional< matches_another_integral_type< unsigned long >::value, std::variant< int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, double, float, std::string >, std::variant< int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, unsigned long, double, float, std::string > >::type AnyMethodArgument
std::string name() const
fixed size matrix
size_t dataMemberSize() const
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
long double T
std::enable_if< std::is_floating_point< F >::value, std::pair< AnyMethodArgument, int > >::type operator()(const F &t) const