CMS 3D CMS Logo

ExpressionVar.cc
Go to the documentation of this file.
3 
8 
9 #include <cassert>
10 #include <map>
11 
12 using namespace reco::parser;
13 using namespace std;
14 
16  objects_.resize(methods_.size());
17  std::vector<edm::ObjectWithDict>::iterator IO = objects_.begin();
18  for (std::vector<MethodInvoker>::const_iterator I = methods_.begin(), E = methods_.end(); I != E; ++IO, ++I) {
19  if (I->isFunction()) {
20  edm::TypeWithDict retType = I->method().finalReturnType();
21  needsDestructor_.push_back(makeStorage(*IO, retType));
22  } else {
23  *IO = edm::ObjectWithDict();
24  needsDestructor_.push_back(false);
25  }
26  }
27 }
28 
29 ExpressionVar::ExpressionVar(const vector<MethodInvoker>& methods, method::TypeCode retType)
30  : methods_(methods), retType_(retType) {
31  initObjects_();
32 }
33 
34 ExpressionVar::ExpressionVar(const ExpressionVar& rhs) : methods_(rhs.methods_), retType_(rhs.retType_) {
35  initObjects_();
36 }
37 
39  for (std::vector<edm::ObjectWithDict>::iterator I = objects_.begin(), E = objects_.end(); I != E; ++I) {
40  delStorage(*I);
41  }
42  objects_.clear();
43 }
44 
46  if (!obj.address()) {
47  return;
48  }
49  if (obj.typeOf().isPointer() || obj.typeOf().isReference()) {
50  // just delete a void*, as that's what it was
51  void** p = static_cast<void**>(obj.address());
52  delete p;
53  } else {
54  //std::cout << "Calling Destruct on a " <<
55  // obj.typeOf().qualifiedName() << std::endl;
56  obj.typeOf().deallocate(obj.address());
57  }
58 }
59 
61  static const edm::TypeWithDict tVoid(edm::TypeWithDict::byName("void"));
62  bool ret = false;
63  if (retType == tVoid) {
65  } else if (retType.isPointer() || retType.isReference()) {
66  // in this case, I have to allocate a void*, not an object!
67  obj = edm::ObjectWithDict(retType, new void*);
68  } else {
69  obj = edm::ObjectWithDict(retType, retType.allocate());
70  ret = retType.isClass();
71  //std::cout << "ExpressionVar: reserved memory at " << obj.address() <<
72  // " for a " << retType.qualifiedName() << " returned by " <<
73  // member.name() << std::endl;
74  }
75  return ret;
76 }
77 
79  using namespace method;
80  bool ret = false;
81  switch (retType) {
82  case (doubleType):
83  ret = true;
84  break;
85  case (floatType):
86  ret = true;
87  break;
88  case (intType):
89  ret = true;
90  break;
91  case (uIntType):
92  ret = true;
93  break;
94  case (shortType):
95  ret = true;
96  break;
97  case (uShortType):
98  ret = true;
99  break;
100  case (longType):
101  ret = true;
102  break;
103  case (uLongType):
104  ret = true;
105  break;
106  case (charType):
107  ret = true;
108  break;
109  case (uCharType):
110  ret = true;
111  break;
112  case (boolType):
113  ret = true;
114  break;
115  case (enumType):
116  ret = true;
117  break;
118  case (invalid):
119  default:
120  break;
121  }
122  return ret;
123 }
124 
127  std::vector<edm::ObjectWithDict>::iterator IO = objects_.begin();
128  for (std::vector<MethodInvoker>::const_iterator I = methods_.begin(), E = methods_.end(); I != E; ++I, ++IO) {
129  val = I->invoke(val, *IO);
130  }
131  double ret = objToDouble(val, retType_);
132  std::vector<bool>::const_reverse_iterator RIB = needsDestructor_.rbegin();
133  for (std::vector<edm::ObjectWithDict>::reverse_iterator RI = objects_.rbegin(), RE = objects_.rend(); RI != RE;
134  ++RIB, ++RI) {
135  if (*RIB) {
136  RI->destruct(false);
137  }
138  }
139  return ret;
140 }
141 
143  using namespace method;
144  void* addr = obj.address();
145  double ret = 0.0;
146  switch (type) {
147  case doubleType:
148  ret = *static_cast<double*>(addr);
149  break;
150  case floatType:
151  ret = *static_cast<float*>(addr);
152  break;
153  case intType:
154  ret = *static_cast<int*>(addr);
155  break;
156  case uIntType:
157  ret = *static_cast<unsigned int*>(addr);
158  break;
159  case shortType:
160  ret = *static_cast<short*>(addr);
161  break;
162  case uShortType:
163  ret = *static_cast<unsigned short*>(addr);
164  break;
165  case longType:
166  ret = *static_cast<long*>(addr);
167  break;
168  case uLongType:
169  ret = *static_cast<unsigned long*>(addr);
170  break;
171  case charType:
172  ret = *static_cast<char*>(addr);
173  break;
174  case uCharType:
175  ret = *static_cast<unsigned char*>(addr);
176  break;
177  case boolType:
178  ret = *static_cast<bool*>(addr);
179  break;
180  case enumType:
181  ret = *static_cast<int*>(addr);
182  break;
183  default:
184  //FIXME: Error not caught in production build!
185  assert(false && "objToDouble: invalid type!");
186  break;
187  };
188  return ret;
189 }
190 
191 ExpressionLazyVar::ExpressionLazyVar(const std::vector<LazyInvoker>& methods) : methods_(methods) {}
192 
194 
197  std::vector<LazyInvoker>::const_iterator I = methods_.begin();
198  std::vector<LazyInvoker>::const_iterator E = methods_.end() - 1;
199  for (; I < E; ++I) {
200  val = I->invoke(val, objects_);
201  }
202  double ret = I->invokeLast(val, objects_);
203  for (std::vector<edm::ObjectWithDict>::reverse_iterator RI = objects_.rbegin(), RE = objects_.rend(); RI != RE;
204  ++RI) {
205  RI->destruct(false);
206  }
207  objects_.clear();
208  return ret;
209 }
static ObjectWithDict byType(TypeWithDict const &)
static bool makeStorage(edm::ObjectWithDict &obj, const edm::TypeWithDict &retType)
ret
prodAgent to be discontinued
std::vector< edm::ObjectWithDict > objects_
Definition: ExpressionVar.h:26
static double objToDouble(const edm::ObjectWithDict &obj, method::TypeCode type)
method::TypeCode retType_
Definition: ExpressionVar.h:28
assert(be >=bs)
void * allocate() const
static TypeWithDict byName(std::string const &name)
Definition: TypeWithDict.cc:74
ExpressionLazyVar(const std::vector< LazyInvoker > &methods)
ExpressionVar(const std::vector< MethodInvoker > &methods, method::TypeCode retType)
static bool isValidReturnType(method::TypeCode)
std::vector< MethodInvoker > methods_
Definition: ExpressionVar.h:25
const std::complex< double > I
Definition: I.h:8
double value(const edm::ObjectWithDict &) const override
static void delStorage(edm::ObjectWithDict &)
double value(const edm::ObjectWithDict &) const override
Evaluate an object&#39;s method or datamember (or chain of them) to get a number.
Definition: ExpressionVar.h:23
bool isClass() const
std::vector< edm::ObjectWithDict > objects_
Definition: ExpressionVar.h:61
bool isPointer() const
bool isReference() const
std::vector< bool > needsDestructor_
Definition: ExpressionVar.h:27
std::vector< LazyInvoker > methods_
Definition: ExpressionVar.h:60