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 
15  const vector<AnyMethodArgument>& ints)
16  : method_(method)
17  , member_()
18  , ints_(ints)
19  , isFunction_(true)
20 {
21  setArgs();
22  if (isFunction_) {
24  }
25  //std::cout <<
26  // "Booking " <<
27  // methodName() <<
28  // " from " <<
29  // method_.declaringType().name() <<
30  // " with " <<
31  // args_.size() <<
32  // " arguments" <<
33  // " (were " <<
34  // ints.size() <<
35  // ")" <<
36  // std::endl;
37 }
38 
41  : method_()
42  , member_(member)
43  , ints_()
45 {
46  setArgs();
47  //std::cout <<
48  // "Booking " <<
49  // methodName() <<
50  // " from " <<
51  // member_.declaringType().name() <<
52  // " with " <<
53  // args_.size() <<
54  // " arguments" <<
55  // " (were " <<
56  // ints.size() <<
57  // ")" <<
58  // std::endl;
59 }
60 
63  : method_(rhs.method_)
64  , member_(rhs.member_)
65  , ints_(rhs.ints_)
68 {
69  setArgs();
70 }
71 
75 {
76  if (this != &rhs) {
77  method_ = rhs.method_;
78  member_ = rhs.member_;
79  ints_ = rhs.ints_;
82 
83  setArgs();
84  }
85  return *this;
86 }
87 
88 void
91 {
92  for (size_t i = 0; i < ints_.size(); ++i) {
93  args_.push_back(boost::apply_visitor(AnyMethodArgument2VoidPtr(), ints_[i]));
94  }
95 }
96 
99 methodName() const
100 {
101  if (isFunction_) {
102  return method_.name();
103  }
104  return member_.name();
105 }
106 
110 {
111  if (isFunction_) {
112  return method_.typeName();
113  }
114  return member_.typeOf().qualifiedName();
115 }
116 
120 {
121  edm::ObjectWithDict ret = retstore;
122  edm::TypeWithDict retType;
123  if (isFunction_) {
124  //std::cout << "Invoking " << methodName()
125  // << " from " << method_.declaringType().qualifiedName()
126  // << " on an instance of " << o.dynamicType().qualifiedName()
127  // << " at " << o.address()
128  // << " with " << args_.size() << " arguments"
129  // << std::endl;
130  method_.invoke(o, &ret, args_);
131  // this is correct, it takes pointers and refs into account
132  retType = retTypeFinal_;
133  }
134  else {
135  //std::cout << "Invoking " << methodName()
136  // << " from " << member_.declaringType().qualifiedName()
137  // << " on an instance of " << o.dynamicType().qualifiedName()
138  // << " at " << o.address()
139  // << " with " << args_.size() << " arguments"
140  // << std::endl;
141  ret = member_.get(o);
142  retType = member_.typeOf();
143  }
144  void* addr = ret.address();
145  //std::cout << "Stored result of " << methodName() << " (type " <<
146  // returnTypeName() << ") at " << addr << std::endl;
147  if (addr == nullptr) {
149  << "method \"" << methodName() << "\" called with " << args_.size()
150  << " arguments returned a null pointer ";
151  }
152  //std::cout << "Return type is " << retType.qualifiedName() << std::endl;
153  if (retType.isPointer() || retType.isReference()) {
154  // both need void** -> void* conversion
155  if (retType.isPointer()) {
156  retType = retType.toType();
157  }
158  else {
159  // strip cv & ref flags
160  // FIXME: This is only true if the propery passed to the constructor
161  // overrides the const and reference flags.
162  retType = retType.stripConstRef();
163  }
164  ret = edm::ObjectWithDict(retType, *static_cast<void**>(addr));
165  //std::cout << "Now type is " << retType.qualifiedName() << std::endl;
166  }
167  if (!bool(ret)) {
169  << "method \"" << methodName()
170  << "\" returned void invoked on object of type \""
171  << o.typeOf().qualifiedName() << "\"\n";
172  }
173  return ret;
174 }
175 
178  const std::vector<AnyMethodArgument>& args)
179  : name_(name)
180  , argsBeforeFixups_(args)
181 {
182 }
183 
185 {
186 }
187 
188 const SingleInvoker&
191 {
192  //std::cout << "LazyInvoker for " << name_ << " called on type " <<
193  // type.qualifiedName() << std::endl;
194  const edm::TypeID thetype(type.typeInfo());
195  auto found = invokers_.find(thetype);
196  if( found != invokers_.cend() ) {
197  return *(found->second);
198  }
199  auto to_add = std::make_shared<SingleInvoker>(type, name_, argsBeforeFixups_);
200  auto emplace_result = invokers_.insert(std::make_pair(thetype,to_add) );
201  return *(emplace_result.first->second);
202 }
203 
206 invoke(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const
207 {
208  pair<edm::ObjectWithDict, bool> ret(o, false);
209  do {
210  edm::TypeWithDict type = ret.first.typeOf();
211  if (type.isClass()) {
212  type = ret.first.dynamicType();
213  }
214  ret = invoker(type).invoke(edm::ObjectWithDict(type, ret.first.address()), v);
215  }
216  while (ret.second == false);
217  return ret.first;
218 }
219 
220 double
223  std::vector<edm::ObjectWithDict>& v) const
224 {
225  pair<edm::ObjectWithDict, bool> ret(o, false);
226  const SingleInvoker* i = nullptr;
227  do {
228  edm::TypeWithDict type = ret.first.typeOf();
229  if (type.isClass()) {
230  type = ret.first.dynamicType();
231  }
232  i = &invoker(type);
233  ret = i->invoke(edm::ObjectWithDict(type, ret.first.address()), v);
234  }
235  while (ret.second == false);
236  return i->retToDouble(ret.first);
237 }
238 
241  const std::vector<AnyMethodArgument>& args)
242 {
243  TypeStack typeStack(1, type);
245  MethodArgumentStack dummy2;
246  MethodSetter setter(invokers_, dummy, typeStack, dummy2, false);
247  isRefGet_ = !setter.push(name, args, "LazyInvoker dynamic resolution", false);
248  //std::cerr << "SingleInvoker on type " << type.qualifiedName() <<
249  // ", name " << name << (isRefGet_ ? " is just a ref.get " : " is real") <<
250  // std::endl;
251  if (invokers_.front().isFunction()) {
252  edm::TypeWithDict retType = invokers_.front().method().finalReturnType();
253  storageNeedsDestructor_ = ExpressionVar::makeStorage(storage_, retType);
254  }
255  else {
256  storage_ = edm::ObjectWithDict();
257  storageNeedsDestructor_ = false;
258  }
259  // typeStack[0] = type of self
260  // typeStack[1] = type of ret
261  retType_ = reco::typeCode(typeStack[1]);
262 }
263 
266 {
267  ExpressionVar::delStorage(storage_);
268 }
269 
270 pair<edm::ObjectWithDict, bool>
272 invoke(const edm::ObjectWithDict& o, std::vector<edm::ObjectWithDict>& v) const
273 {
274  // std::cerr << "[SingleInvoker::invoke] member " <<
275  // invokers_.front().method().qualifiedName() <<
276  // " of type " <<
277  // o.typeOf().qualifiedName() <<
278  // (!isRefGet_ ? " is one shot" : " needs another round") <<
279  // std::endl;
280  pair<edm::ObjectWithDict, bool>
281  ret(invokers_.front().invoke(o, storage_), !isRefGet_);
282  if (storageNeedsDestructor_) {
283  //std::cerr << "Storage type: " << storage_.typeOf().qualifiedName() <<
284  // ", I have to call the destructor." << std::endl;
285  v.push_back(storage_);
286  }
287  return ret;
288 }
289 
290 double
293 {
294  if (!ExpressionVar::isValidReturnType(retType_)) {
295  throwFailedConversion(o);
296  }
297  return ExpressionVar::objToDouble(o, retType_);
298 }
299 
300 void
303 {
305  << "member \"" << invokers_.back().methodName()
306  << "\" return type is \"" << invokers_.back().returnTypeName()
307  << "\" retured a \"" << o.typeOf().qualifiedName()
308  << "\" which is not convertible to double.";
309 }
310 
type
Definition: HCALResponse.h:21
edm::ObjectWithDict invoke(const edm::ObjectWithDict &obj, edm::ObjectWithDict &retstore) const
static bool makeStorage(edm::ObjectWithDict &obj, const edm::TypeWithDict &retType)
void * address() const
SingleInvoker(const edm::TypeWithDict &, const std::string &name, const std::vector< AnyMethodArgument > &args)
std::pair< edm::ObjectWithDict, bool > invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
MethodInvoker(const edm::FunctionWithDict &method, const std::vector< AnyMethodArgument > &ints=std::vector< AnyMethodArgument >())
std::string returnTypeName() const
std::vector< edm::TypeWithDict > TypeStack
Definition: TypeStack.h:17
static double objToDouble(const edm::ObjectWithDict &obj, method::TypeCode type)
std::string qualifiedName() const
std::string name() const
double invokeLast(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
invoke and coerce result to double
edm::MemberWithDict member_
Definition: MethodInvoker.h:34
TypeWithDict toType() const
TypeWithDict typeOf() const
std::vector< AnyMethodArgument > ints_
Definition: MethodInvoker.h:35
std::vector< AnyMethodArgument > MethodArgumentStack
static bool isValidReturnType(method::TypeCode)
std::string typeName() const
edm::MemberWithDict const member() const
Definition: MethodInvoker.h:51
bool isClass() const
edm::FunctionWithDict method_
Definition: MethodInvoker.h:33
std::string name() const
TypeCode typeCode(const edm::TypeWithDict &t)
Definition: returnType.cc:44
ObjectWithDict get() const
bool push(const std::string &, const std::vector< AnyMethodArgument > &, const char *, bool deep=true) const
Definition: MethodSetter.cc:65
TypeWithDict typeOf() const
std::type_info const & typeInfo() const
edm::TypeWithDict retTypeFinal_
Definition: MethodInvoker.h:39
void invoke(ObjectWithDict const &obj, ObjectWithDict *ret=nullptr, std::vector< void * > const &values=std::vector< void * >()) const
Call a member function.
edm::ObjectWithDict invoke(const edm::ObjectWithDict &o, std::vector< edm::ObjectWithDict > &v) const
TypeWithDict finalReturnType() const
std::vector< LazyInvoker > LazyMethodStack
Definition: MethodStack.h:18
MethodInvoker & operator=(const MethodInvoker &)
void throwFailedConversion(const edm::ObjectWithDict &) const
static void delStorage(edm::ObjectWithDict &)
bool isReference() const
const SingleInvoker & invoker(const edm::TypeWithDict &) const
TypeWithDict & stripConstRef()
std::string methodName() const
std::vector< void * > args_
Definition: MethodInvoker.h:36
std::vector< AnyMethodArgument > argsBeforeFixups_
double retToDouble(const edm::ObjectWithDict &) const
convert the output of invoke to a double, if possible
bool isPointer() const
LazyInvoker(const std::string &name, const std::vector< AnyMethodArgument > &args)