CMS 3D CMS Logo

MethodInvoker.cc
Go to the documentation of this file.
2 
8 
9 #include <algorithm>
10 using namespace reco::parser;
11 using namespace std;
12 
13 MethodInvoker::MethodInvoker(const edm::FunctionWithDict& method, const vector<AnyMethodArgument>& ints)
14  : method_(method), member_(), ints_(ints), isFunction_(true) {
15  setArgs();
16  if (isFunction_) {
18  }
19  //std::cout <<
20  // "Booking " <<
21  // methodName() <<
22  // " from " <<
23  // method_.declaringType().name() <<
24  // " with " <<
25  // args_.size() <<
26  // " arguments" <<
27  // " (were " <<
28  // ints.size() <<
29  // ")" <<
30  // std::endl;
31 }
32 
34  : method_(), member_(member), ints_(), isFunction_(false) {
35  setArgs();
36  //std::cout <<
37  // "Booking " <<
38  // methodName() <<
39  // " from " <<
40  // member_.declaringType().name() <<
41  // " with " <<
42  // args_.size() <<
43  // " arguments" <<
44  // " (were " <<
45  // ints.size() <<
46  // ")" <<
47  // std::endl;
48 }
49 
51  : method_(rhs.method_),
52  member_(rhs.member_),
53  ints_(rhs.ints_),
54  isFunction_(rhs.isFunction_),
55  retTypeFinal_(rhs.retTypeFinal_) {
56  setArgs();
57 }
58 
60  if (this != &rhs) {
61  method_ = rhs.method_;
62  member_ = rhs.member_;
63  ints_ = rhs.ints_;
66 
67  setArgs();
68  }
69  return *this;
70 }
71 
73  for (size_t i = 0; i < ints_.size(); ++i) {
74  args_.push_back(boost::apply_visitor(AnyMethodArgument2VoidPtr(), ints_[i]));
75  }
76 }
77 
79  if (isFunction_) {
80  return method_.name();
81  }
82  return member_.name();
83 }
84 
86  if (isFunction_) {
87  return method_.typeName();
88  }
89  return member_.typeOf().qualifiedName();
90 }
91 
93  edm::ObjectWithDict ret = retstore;
94  edm::TypeWithDict retType;
95  if (isFunction_) {
96  //std::cout << "Invoking " << methodName()
97  // << " from " << method_.declaringType().qualifiedName()
98  // << " on an instance of " << o.dynamicType().qualifiedName()
99  // << " at " << o.address()
100  // << " with " << args_.size() << " arguments"
101  // << std::endl;
102  method_.invoke(o, &ret, args_);
103  // this is correct, it takes pointers and refs into account
104  retType = retTypeFinal_;
105  } else {
106  //std::cout << "Invoking " << methodName()
107  // << " from " << member_.declaringType().qualifiedName()
108  // << " on an instance of " << o.dynamicType().qualifiedName()
109  // << " at " << o.address()
110  // << " with " << args_.size() << " arguments"
111  // << std::endl;
112  ret = member_.get(o);
113  retType = member_.typeOf();
114  }
115  void* addr = ret.address();
116  //std::cout << "Stored result of " << methodName() << " (type " <<
117  // returnTypeName() << ") at " << addr << std::endl;
118  if (addr == nullptr) {
120  << "method \"" << methodName() << "\" called with " << args_.size() << " arguments returned a null pointer ";
121  }
122  //std::cout << "Return type is " << retType.qualifiedName() << std::endl;
123  if (retType.isPointer() || retType.isReference()) {
124  // both need void** -> void* conversion
125  if (retType.isPointer()) {
126  retType = retType.toType();
127  } else {
128  // strip cv & ref flags
129  // FIXME: This is only true if the propery passed to the constructor
130  // overrides the const and reference flags.
131  retType = retType.stripConstRef();
132  }
133  ret = edm::ObjectWithDict(retType, *static_cast<void**>(addr));
134  //std::cout << "Now type is " << retType.qualifiedName() << std::endl;
135  }
136  if (!bool(ret)) {
138  << "method \"" << methodName() << "\" returned void invoked on object of type \"" << o.typeOf().qualifiedName()
139  << "\"\n";
140  }
141  return ret;
142 }
143 
144 LazyInvoker::LazyInvoker(const std::string& name, const std::vector<AnyMethodArgument>& args)
145  : name_(name), argsBeforeFixups_(args) {}
146 
148 
150  //std::cout << "LazyInvoker for " << name_ << " called on type " <<
151  // type.qualifiedName() << std::endl;
152  const edm::TypeID thetype(type.typeInfo());
153  auto found = invokers_.find(thetype);
154  if (found != invokers_.cend()) {
155  return *(found->second);
156  }
157  auto to_add = std::make_shared<SingleInvoker>(type, name_, argsBeforeFixups_);
158  auto emplace_result = invokers_.insert(std::make_pair(thetype, to_add));
159  return *(emplace_result.first->second);
160 }
161 
162 edm::ObjectWithDict LazyInvoker::invoke(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const {
163  pair<edm::ObjectWithDict, bool> ret(o, false);
164  do {
165  edm::TypeWithDict type = ret.first.typeOf();
166  if (type.isClass()) {
167  type = ret.first.dynamicType();
168  }
169  ret = invoker(type).invoke(edm::ObjectWithDict(type, ret.first.address()), v);
170  } while (ret.second == false);
171  return ret.first;
172 }
173 
174 double LazyInvoker::invokeLast(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const {
175  pair<edm::ObjectWithDict, bool> ret(o, false);
176  const SingleInvoker* i = nullptr;
177  do {
178  edm::TypeWithDict type = ret.first.typeOf();
179  if (type.isClass()) {
180  type = ret.first.dynamicType();
181  }
182  i = &invoker(type);
183  ret = i->invoke(edm::ObjectWithDict(type, ret.first.address()), v);
184  } while (ret.second == false);
185  return i->retToDouble(ret.first);
186 }
187 
189  const std::string& name,
190  const std::vector<AnyMethodArgument>& args) {
191  TypeStack typeStack(1, type);
193  MethodArgumentStack dummy2;
194  MethodSetter setter(invokers_, dummy, typeStack, dummy2, false);
195  isRefGet_ = !setter.push(name, args, "LazyInvoker dynamic resolution", false);
196  //std::cerr << "SingleInvoker on type " << type.qualifiedName() <<
197  // ", name " << name << (isRefGet_ ? " is just a ref.get " : " is real") <<
198  // std::endl;
199  if (invokers_.front().isFunction()) {
200  edm::TypeWithDict retType = invokers_.front().method().finalReturnType();
202  } else {
204  storageNeedsDestructor_ = false;
205  }
206  // typeStack[0] = type of self
207  // typeStack[1] = type of ret
208  retType_ = reco::typeCode(typeStack[1]);
209 }
210 
212 
213 pair<edm::ObjectWithDict, bool> SingleInvoker::invoke(const edm::ObjectWithDict& o,
214  std::vector<edm::ObjectWithDict>& v) const {
215  // std::cerr << "[SingleInvoker::invoke] member " <<
216  // invokers_.front().method().qualifiedName() <<
217  // " of type " <<
218  // o.typeOf().qualifiedName() <<
219  // (!isRefGet_ ? " is one shot" : " needs another round") <<
220  // std::endl;
221  pair<edm::ObjectWithDict, bool> ret(invokers_.front().invoke(o, storage_), !isRefGet_);
223  //std::cerr << "Storage type: " << storage_.typeOf().qualifiedName() <<
224  // ", I have to call the destructor." << std::endl;
225  v.push_back(storage_);
226  }
227  return ret;
228 }
229 
233  }
235 }
236 
239  << "member \"" << invokers_.back().methodName() << "\" return type is \"" << invokers_.back().returnTypeName()
240  << "\" retured a \"" << o.typeOf().qualifiedName() << "\" which is not convertible to double.";
241 }
runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:355
reco::parser::SingleInvoker::invoke
std::pair< edm::ObjectWithDict, bool > invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
Definition: MethodInvoker.cc:213
writedatasetfile.args
args
Definition: writedatasetfile.py:18
mps_fire.i
i
Definition: mps_fire.py:355
reco::parser::MethodInvoker::retTypeFinal_
edm::TypeWithDict retTypeFinal_
Definition: MethodInvoker.h:35
funct::false
false
Definition: Factorize.h:34
reco::parser::MethodInvoker::methodName
std::string methodName() const
Definition: MethodInvoker.cc:78
edm::errors::InvalidReference
Definition: EDMException.h:39
reco::parser::LazyInvoker::LazyInvoker
LazyInvoker(const std::string &name, const std::vector< AnyMethodArgument > &args)
Definition: MethodInvoker.cc:144
reco::parser::SingleInvoker
Definition: MethodInvoker.h:65
edm::ObjectWithDict
Definition: ObjectWithDict.h:17
AlcaSiPixelAliHarvester0T_cff.method
method
Definition: AlcaSiPixelAliHarvester0T_cff.py:41
edm::FunctionWithDict::invoke
void invoke(ObjectWithDict const &obj, ObjectWithDict *ret=nullptr, std::vector< void * > const &values=std::vector< void * >()) const
Call a member function.
Definition: FunctionWithDict.cc:74
reco::parser::MethodInvoker::method_
edm::FunctionWithDict method_
Definition: MethodInvoker.h:29
reco::parser::MethodInvoker
Definition: MethodInvoker.h:27
reco::parser::SingleInvoker::storage_
edm::ObjectWithDict storage_
Definition: MethodInvoker.h:69
reco::parser::SingleInvoker::storageNeedsDestructor_
bool storageNeedsDestructor_
Definition: MethodInvoker.h:70
edm::TypeWithDict::stripConstRef
TypeWithDict & stripConstRef()
Definition: TypeWithDict.cc:254
findQualityFiles.v
v
Definition: findQualityFiles.py:179
newFWLiteAna.found
found
Definition: newFWLiteAna.py:118
reco::parser::MethodInvoker::operator=
MethodInvoker & operator=(const MethodInvoker &)
Definition: MethodInvoker.cc:59
EcalTangentSkim_cfg.o
o
Definition: EcalTangentSkim_cfg.py:36
edm::TypeWithDict::qualifiedName
std::string qualifiedName() const
Definition: TypeWithDict.cc:441
reco::parser::LazyInvoker::invokeLast
double invokeLast(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
invoke and coerce result to double
Definition: MethodInvoker.cc:174
EDMException.h
reco::parser::MethodInvoker::returnTypeName
std::string returnTypeName() const
Definition: MethodInvoker.cc:85
reco::parser::LazyInvoker::argsBeforeFixups_
std::vector< AnyMethodArgument > argsBeforeFixups_
Definition: MethodInvoker.h:101
reco::parser::MethodInvoker::isFunction_
bool isFunction_
Definition: MethodInvoker.h:34
generateTowerEtThresholdLUT.addr
addr
Definition: generateTowerEtThresholdLUT.py:57
reco::parser::SingleInvoker::invokers_
std::vector< MethodInvoker > invokers_
Definition: MethodInvoker.h:68
edm::TypeWithDict::isPointer
bool isPointer() const
Definition: TypeWithDict.cc:416
edm::TypeWithDict::isReference
bool isReference() const
Definition: TypeWithDict.cc:418
edm::MemberWithDict::get
ObjectWithDict get() const
Definition: MemberWithDict.cc:46
reco::parser::SingleInvoker::isRefGet_
bool isRefGet_
true if this invoker just pops out a ref and returns (ref.get(), false)
Definition: MethodInvoker.h:72
edm::MemberWithDict::typeOf
TypeWithDict typeOf() const
Definition: MemberWithDict.cc:18
edm::FunctionWithDict::typeName
std::string typeName() const
Definition: FunctionWithDict.cc:38
reco::parser::MethodInvoker::args_
std::vector< void * > args_
Definition: MethodInvoker.h:32
reco::parser::LazyInvoker::invoker
const SingleInvoker & invoker(const edm::TypeWithDict &) const
Definition: MethodInvoker.cc:149
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
reco::typeCode
TypeCode typeCode(const edm::TypeWithDict &t)
Definition: returnType.cc:39
reco::parser::SingleInvoker::SingleInvoker
SingleInvoker(const edm::TypeWithDict &, const std::string &name, const std::vector< AnyMethodArgument > &args)
Definition: MethodInvoker.cc:188
reco::parser::LazyInvoker::name_
std::string name_
Definition: MethodInvoker.h:100
funct::true
true
Definition: Factorize.h:173
edm::TypeWithDict
Definition: TypeWithDict.h:38
reco::parser::SingleInvoker::retToDouble
double retToDouble(const edm::ObjectWithDict &) const
convert the output of invoke to a double, if possible
Definition: MethodInvoker.cc:230
reco::parser::SingleInvoker::throwFailedConversion
void throwFailedConversion(const edm::ObjectWithDict &) const
Definition: MethodInvoker.cc:237
reco::parser::MethodInvoker::invoke
edm::ObjectWithDict invoke(const edm::ObjectWithDict &obj, edm::ObjectWithDict &retstore) const
Definition: MethodInvoker.cc:92
reco::parser::LazyInvoker::invokers_
InvokerMap invokers_
Definition: MethodInvoker.h:105
edm::TypeWithDict::toType
TypeWithDict toType() const
Definition: TypeWithDict.cc:633
reco::parser::AnyMethodArgument2VoidPtr
Definition: AnyMethodArgument.h:136
reco::parser::MethodInvoker::setArgs
void setArgs()
Definition: MethodInvoker.cc:72
edm::MemberWithDict
Definition: MemberWithDict.h:19
MethodInvoker.h
reco::parser::TypeStack
std::vector< edm::TypeWithDict > TypeStack
Definition: TypeStack.h:17
edm::MemberWithDict::name
std::string name() const
Definition: MemberWithDict.cc:16
reco::parser::MethodInvoker::MethodInvoker
MethodInvoker(const edm::FunctionWithDict &method, const std::vector< AnyMethodArgument > &ints=std::vector< AnyMethodArgument >())
Definition: MethodInvoker.cc:13
reco::parser::LazyInvoker::invoke
edm::ObjectWithDict invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
Definition: MethodInvoker.cc:162
reco::parser::MethodInvoker::ints_
std::vector< AnyMethodArgument > ints_
Definition: MethodInvoker.h:31
returnType.h
reco::parser::ExpressionVar::objToDouble
static double objToDouble(const edm::ObjectWithDict &obj, method::TypeCode type)
Definition: ExpressionVar.cc:142
reco::parser
Definition: cutParser.h:9
reco::parser::ExpressionVar::delStorage
static void delStorage(edm::ObjectWithDict &)
Definition: ExpressionVar.cc:45
edm::TypeID
Definition: TypeID.h:22
reco::parser::SingleInvoker::retType_
method::TypeCode retType_
Definition: MethodInvoker.h:67
type
type
Definition: HCALResponse.h:21
std
Definition: JetResolutionObject.h:76
reco::parser::SingleInvoker::~SingleInvoker
~SingleInvoker()
Definition: MethodInvoker.cc:211
reco::parser::ExpressionVar::makeStorage
static bool makeStorage(edm::ObjectWithDict &obj, const edm::TypeWithDict &retType)
Definition: ExpressionVar.cc:60
edm::FunctionWithDict
Definition: FunctionWithDict.h:27
reco::parser::MethodSetter
Definition: MethodSetter.h:17
reco::parser::LazyMethodStack
std::vector< LazyInvoker > LazyMethodStack
Definition: MethodStack.h:18
ExpressionVar.h
Exception
Definition: hltDiff.cc:246
reco::parser::MethodInvoker::member_
edm::MemberWithDict member_
Definition: MethodInvoker.h:30
edm::FunctionWithDict::name
std::string name() const
Definition: FunctionWithDict.cc:36
reco::parser::MethodSetter::push
bool push(const std::string &, const std::vector< AnyMethodArgument > &, const char *, bool deep=true) const
Definition: MethodSetter.cc:57
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
reco::parser::LazyInvoker::~LazyInvoker
~LazyInvoker()
Definition: MethodInvoker.cc:147
reco::parser::MethodArgumentStack
std::vector< AnyMethodArgument > MethodArgumentStack
Definition: MethodArgumentStack.h:17
dummy
Definition: DummySelector.h:38
findMethod.h
reco::parser::ExpressionVar::isValidReturnType
static bool isValidReturnType(method::TypeCode)
Definition: ExpressionVar.cc:78
edm::FunctionWithDict::finalReturnType
TypeWithDict finalReturnType() const
Definition: FunctionWithDict.cc:40
edm::errors::Configuration
Definition: EDMException.h:36
MethodSetter.h