CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ExpressionVar.cc
Go to the documentation of this file.
6 #include <map>
7 #include <assert.h>
8 using namespace reco::parser;
9 using namespace std;
10 
11 ExpressionVar::ExpressionVar(const vector<MethodInvoker>& methods, method::TypeCode retType) :
12  methods_(methods), retType_(retType) {
13  initObjects_();
14 }
15 
17  methods_(other.methods_), retType_(other.retType_) {
18  initObjects_();
19 }
20 
22  for(std::vector<edm::ObjectWithDict>::iterator it = objects_.begin(); it != objects_.end(); ++it) {
23  delStorage(*it);
24  }
25  objects_.clear();
26 }
27 
28 void
30  if (obj.address() != 0) {
31  if (obj.typeOf().isPointer() || obj.typeOf().isReference()) {
32  // just delete a void *, as that's what it was
33  void **p = static_cast<void **>(obj.address());
34  delete p;
35  } else {
36  //std::cout << "Calling Destruct on a " << obj.typeOf().qualifiedName() << std::endl;
37  obj.typeOf().deallocate(obj.address());
38  }
39  }
40 }
41 
43  objects_.resize(methods_.size());
44  std::vector<MethodInvoker>::const_iterator it = methods_.begin(), ed = methods_.end();
45  std::vector<edm::ObjectWithDict>::iterator itobj = objects_.begin();
46  for (; it != ed; ++it, ++itobj) {
47  if(it->isFunction()) {
48  edm::TypeWithDict retType = it->method().finalReturnType();
49  needsDestructor_.push_back(makeStorage(*itobj, retType));
50  } else {
51  *itobj = edm::ObjectWithDict();
52  needsDestructor_.push_back(false);
53  }
54  }
55 }
56 
57 bool
59  bool ret = false;
60  static edm::TypeWithDict tVoid(edm::TypeWithDict::byName("void"));
61  if (retType == tVoid) {
62  obj = edm::ObjectWithDict::byType(tVoid);
63  } else if (retType.isPointer() || retType.isReference()) {
64  // in this case, I have to allocate a void *, not an object!
65  obj = edm::ObjectWithDict(retType, new void *);
66  } else {
67  obj = edm::ObjectWithDict(retType, retType.allocate());
68  ret = retType.isClass();
69  //std::cout << "ExpressionVar: reserved memory at " << obj.address() << " for a " << retType.qualifiedName() << " returned by " << member.name() << std::endl;
70  }
71  return ret;
72 }
73 
75 {
76  using namespace method;
77  bool ret = false;
78  switch(retType) {
79  case(doubleType) : ret = true; break;
80  case(floatType ) : ret = true; break;
81  case(intType ) : ret = true; break;
82  case(uIntType ) : ret = true; break;
83  case(shortType ) : ret = true; break;
84  case(uShortType) : ret = true; break;
85  case(longType ) : ret = true; break;
86  case(uLongType ) : ret = true; break;
87  case(charType ) : ret = true; break;
88  case(uCharType ) : ret = true; break;
89  case(boolType ) : ret = true; break;
90  case(enumType ) : ret = true; break;
91  case(invalid):
92  default:
93  break;
94  }
95  return ret;
96 }
97 
100  std::vector<MethodInvoker>::const_iterator itm, end = methods_.end();
101  std::vector<edm::ObjectWithDict>::iterator ito;
102  for(itm = methods_.begin(), ito = objects_.begin(); itm != end; ++itm, ++ito) {
103  ro = itm->invoke(ro, *ito);
104  }
105  double ret = objToDouble(ro, retType_);
106  std::vector<edm::ObjectWithDict>::reverse_iterator rito, rend = objects_.rend();;
107  std::vector<bool>::const_reverse_iterator ritb;
108  for(rito = objects_.rbegin(), ritb = needsDestructor_.rbegin(); rito != rend; ++rito, ++ritb) {
109  if (*ritb) rito->typeOf().destruct(rito->address(), false);
110  }
111  return ret;
112 }
113 
114 double
116  using namespace method;
117  void * addr = obj.address();
118  double ret = 0;
119  switch(type) {
120  case(doubleType) : ret = * static_cast<double *>(addr); break;
121  case(floatType ) : ret = * static_cast<float *>(addr); break;
122  case(intType ) : ret = * static_cast<int *>(addr); break;
123  case(uIntType ) : ret = * static_cast<unsigned int *>(addr); break;
124  case(shortType ) : ret = * static_cast<short *>(addr); break;
125  case(uShortType) : ret = * static_cast<unsigned short *>(addr); break;
126  case(longType ) : ret = * static_cast<long *>(addr); break;
127  case(uLongType ) : ret = * static_cast<unsigned long *>(addr); break;
128  case(charType ) : ret = * static_cast<char *>(addr); break;
129  case(uCharType ) : ret = * static_cast<unsigned char *>(addr); break;
130  case(boolType ) : ret = * static_cast<bool *>(addr); break;
131  case(enumType ) : ret = * static_cast<int *>(addr); break;
132  default:
133  assert(false);
134  };
135  return ret;
136 }
137 
138 ExpressionLazyVar::ExpressionLazyVar(const std::vector<LazyInvoker> & methods) :
139  methods_(methods)
140 {
141 }
142 
144 {
145 }
146 
147 double
149  std::vector<LazyInvoker>::const_iterator it, ed = methods_.end()-1;
150  edm::ObjectWithDict ro = o;
151  for (it = methods_.begin(); it < ed; ++it) {
152  ro = it->invoke(ro, objects_);
153  }
154  double ret = it->invokeLast(ro, objects_);
155  std::vector<edm::ObjectWithDict>::reverse_iterator rit, red = objects_.rend();
156  for (rit = objects_.rbegin(); rit != red; ++rit) {
157  rit->typeOf().destruct(rit->address(), false);
158  }
159  objects_.clear();
160  return ret;
161 }
162 
static ObjectWithDict byType(TypeWithDict const &type)
type
Definition: HCALResponse.h:21
virtual double value(const edm::ObjectWithDict &o) const
static TypeWithDict byName(std::string const &className)
ExpressionLazyVar(const std::vector< LazyInvoker > &methods)
void * address() const
static bool makeStorage(edm::ObjectWithDict &obj, const edm::TypeWithDict &retType)
static void delStorage(edm::ObjectWithDict &obj)
std::vector< LazyInvoker > methods_
Definition: ExpressionVar.h:59
ExpressionVar(const std::vector< MethodInvoker > &methods, method::TypeCode retType)
std::vector< bool > needsDestructor_
Definition: ExpressionVar.h:45
static bool isValidReturnType(method::TypeCode)
static double objToDouble(const edm::ObjectWithDict &obj, method::TypeCode type)
bool isClass() const
#define end
Definition: vmac.h:38
TypeWithDict const & typeOf() const
void * allocate() const
Definition: TypeWithDict.h:151
virtual double value(const edm::ObjectWithDict &o) const
bool isReference() const
std::vector< edm::ObjectWithDict > objects_
Definition: ExpressionVar.h:60
std::vector< MethodInvoker > methods_
Definition: ExpressionVar.h:43
bool isPointer() const
std::vector< edm::ObjectWithDict > objects_
Definition: ExpressionVar.h:44
void deallocate(void *obj) const
Definition: TypeWithDict.h:155
Evaluate an object&#39;s method or datamember (or chain of them) to get a number.
Definition: ExpressionVar.h:21
method::TypeCode retType_
Definition: ExpressionVar.h:46