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