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 <string>
10 #include <cstdint>
11 
12 #include <boost/variant.hpp>
13 #include <boost/type_traits/is_same.hpp>
14 #include <boost/mpl/if.hpp>
15 #include <type_traits>
16 
17 namespace reco {
18  namespace parser {
19 
20  // true if T matches one of the integral types in the default
21  // AnyMethodArgument variant
22  template <typename T>
28  };
29 
30  // size_t on 32-bit Os X is type unsigned long, which doesn't match uint32_t,
31  // so add unsigned long if it doesn't match any of the other integral types.
32  // Use "unsigned long" rather than size_t as PtrVector has unsigned long as
33  // size_type
34  typedef boost::mpl::if_<
36  boost::
37  variant<int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, double, float, std::string>,
38  boost::variant<int8_t,
39  uint8_t,
40  int16_t,
41  uint16_t,
42  int32_t,
43  uint32_t,
44  int64_t,
45  uint64_t,
46  unsigned long,
47  double,
48  float,
50 
51  class AnyMethodArgumentFixup : public boost::static_visitor<std::pair<AnyMethodArgument, int> > {
52  private:
54  template <typename From, typename To>
55  std::pair<AnyMethodArgument, int> retOk_(const From &f, int cast) const {
56  return std::pair<AnyMethodArgument, int>(AnyMethodArgument(static_cast<To>(f)), cast);
57  }
58 
59  // correct return for each int output type
60  std::pair<AnyMethodArgument, int> doInt(int t) const {
61  if (dataType_ == typeid(int8_t)) {
62  return retOk_<int, int8_t>(t, 0);
63  }
64  if (dataType_ == typeid(uint8_t)) {
65  return retOk_<int, uint8_t>(t, 0);
66  }
67  if (dataType_ == typeid(int16_t)) {
68  return retOk_<int, int16_t>(t, 0);
69  }
70  if (dataType_ == typeid(uint16_t)) {
71  return retOk_<int, uint16_t>(t, 0);
72  }
73  if (dataType_ == typeid(int32_t)) {
74  return retOk_<int, int32_t>(t, 0);
75  }
76  if (dataType_ == typeid(uint32_t)) {
77  return retOk_<int, uint32_t>(t, 0);
78  }
79  if (dataType_ == typeid(int64_t)) {
80  return retOk_<int, int64_t>(t, 0);
81  }
82  if (dataType_ == typeid(uint64_t)) {
83  return retOk_<int, uint64_t>(t, 0);
84  }
85  if (dataType_ == typeid(unsigned long)) {
86  return retOk_<int, unsigned long>(t, 0);
87  } // harmless if unsigned long matches another type
88  if (dataType_ == typeid(double)) {
89  return retOk_<int, double>(t, 1);
90  }
91  if (dataType_ == typeid(float)) {
92  return retOk_<int, float>(t, 1);
93  }
94  return std::pair<AnyMethodArgument, int>(t, -1);
95  }
96 
97  public:
99 
100  // we handle all integer types through 'int', as that's the way they are parsed by boost::spirit
101  template <typename I>
102  typename std::enable_if<std::is_integral<I>::value, std::pair<AnyMethodArgument, int> >::type operator()(
103  const I &t) const {
104  return doInt(t);
105  }
106 
107  template <typename F>
108  typename std::enable_if<std::is_floating_point<F>::value, std::pair<AnyMethodArgument, int> >::type operator()(
109  const F &t) const {
110  if (dataType_ == typeid(double)) {
111  return retOk_<F, double>(t, 0);
112  }
113  if (dataType_ == typeid(float)) {
114  return retOk_<F, float>(t, 0);
115  }
116  return std::pair<AnyMethodArgument, int>(t, -1);
117  }
118 
119  std::pair<AnyMethodArgument, int> operator()(const std::string &t) const {
120  if (dataType_.isEnum()) {
121  if (dataType_.dataMemberSize() == 0) {
122  throw parser::Exception(t.c_str())
123  << "Enumerator '" << dataType_.name() << "' has no keys.\nPerhaps the dictionary is missing?\n";
124  }
125  int ival = dataType_.stringToEnumValue(t);
126  // std::cerr << " value is = " << dataType_.stringToEnumValue(t) << std::endl;
127  return std::pair<AnyMethodArgument, int>(ival, 1);
128  }
129  if (dataType_ == typeid(std::string)) {
130  return std::pair<AnyMethodArgument, int>(t, 0);
131  }
132  return std::pair<AnyMethodArgument, int>(t, -1);
133  }
134  };
135 
136  class AnyMethodArgument2VoidPtr : public boost::static_visitor<void *> {
137  public:
138  template <typename T>
139  void *operator()(const T &t) const {
140  return const_cast<void *>(static_cast<const void *>(&t));
141  }
142  };
143  } // namespace parser
144 } // namespace reco
145 
146 #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:23
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:102
reco::parser::AnyMethodArgumentFixup::AnyMethodArgumentFixup
AnyMethodArgumentFixup(const edm::TypeWithDict &type)
Definition: AnyMethodArgument.h:98
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:60
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:51
Exhume::I
const std::complex< double > I
Definition: I.h:8
Exception.h
reco::parser::AnyMethodArgumentFixup::dataType_
edm::TypeWithDict dataType_
Definition: AnyMethodArgument.h:53
OrderedSet.t
t
Definition: OrderedSet.py:90
reco::parser::AnyMethodArgument
boost::mpl::if_< matches_another_integral_type< unsigned long >, boost::variant< int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, double, float, std::string >, boost::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: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:119
edm::TypeWithDict
Definition: TypeWithDict.h:38
reco::parser::AnyMethodArgument2VoidPtr
Definition: AnyMethodArgument.h:136
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:108
value
Definition: value.py:1
reco::parser::AnyMethodArgument2VoidPtr::operator()
void * operator()(const T &t) const
Definition: AnyMethodArgument.h:139
type
type
Definition: HCALResponse.h:21
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
reco::parser::AnyMethodArgumentFixup::retOk_
std::pair< AnyMethodArgument, int > retOk_(const From &f, int cast) const
Definition: AnyMethodArgument.h:55
edm::TypeWithDict::name
std::string name() const
Definition: TypeWithDict.cc:456