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
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
reco::parser::matches_another_integral_type
Definition: AnyMethodArgument.h:20
edm::TypeWithDict::stringToEnumValue
int stringToEnumValue(std::string const &) const
Definition: TypeWithDict.cc:774
reco::parser::AnyMethodArgumentFixup::operator()
std::enable_if< std::is_integral< I >::value, std::pair< AnyMethodArgument, int > >::type operator()(const I &t) const
Definition: AnyMethodArgument.h:98
reco::parser::AnyMethodArgumentFixup::AnyMethodArgumentFixup
AnyMethodArgumentFixup(const edm::TypeWithDict &type)
Definition: AnyMethodArgument.h:94
TypeID.h
writedatasetfile.parser
parser
Definition: writedatasetfile.py:7
TypeWithDict.h
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
reco::parser::AnyMethodArgumentFixup::doInt
std::pair< AnyMethodArgument, int > doInt(int t) const
Definition: AnyMethodArgument.h:56
reco::parser::AnyMethodArgument
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
Definition: AnyMethodArgument.h:45
edm::TypeWithDict::isEnum
bool isEnum() const
Definition: TypeWithDict.cc:412
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::TypeWithDict::dataMemberSize
size_t dataMemberSize() const
Definition: TypeWithDict.cc:528
reco::parser::AnyMethodArgumentFixup
Definition: AnyMethodArgument.h:47
Exhume::I
const std::complex< double > I
Definition: I.h:8
Exception.h
reco::parser::AnyMethodArgumentFixup::dataType_
edm::TypeWithDict dataType_
Definition: AnyMethodArgument.h:49
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::parser::AnyMethodArgumentFixup::operator()
std::pair< AnyMethodArgument, int > operator()(const std::string &t) const
Definition: AnyMethodArgument.h:115
edm::TypeWithDict
Definition: TypeWithDict.h:38
type
type
Definition: SiPixelVCal_PayloadInspector.cc:37
reco::parser::AnyMethodArgument2VoidPtr
Definition: AnyMethodArgument.h:132
reco::parser::AnyMethodArgumentFixup::operator()
std::enable_if< std::is_floating_point< F >::value, std::pair< AnyMethodArgument, int > >::type operator()(const F &t) const
Definition: AnyMethodArgument.h:104
value
Definition: value.py:1
reco::parser::AnyMethodArgument2VoidPtr::operator()
void * operator()(const T &t) const
Definition: AnyMethodArgument.h:135
T
long double T
Definition: Basic3DVectorLD.h:48
relativeConstraints.value
value
Definition: relativeConstraints.py:53
Exception
Definition: hltDiff.cc:246
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
reco::parser::AnyMethodArgumentFixup::retOk_
std::pair< AnyMethodArgument, int > retOk_(const From &f, int cast) const
Definition: AnyMethodArgument.h:51
edm::TypeWithDict::name
std::string name() const
Definition: TypeWithDict.cc:456