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 objects(methods_.size(), {edm::ObjectWithDict(), false});
17  assert(objects.size() == methods_.size());
18  auto IO = objects.begin();
19  for (auto const& method : methods_) {
20  if (method.isFunction()) {
21  edm::TypeWithDict retType = method.method().finalReturnType();
22  IO->second = makeStorage(IO->first, retType);
23  } else {
24  *IO = {edm::ObjectWithDict(), false};
25  }
26  ++IO;
27  }
28  return objects;
29 }
30 
31 ExpressionVar::ExpressionVar(const vector<MethodInvoker>& methods, method::TypeCode retType)
32  : methods_(methods), retType_(retType) {
34 }
35 
36 ExpressionVar::ExpressionVar(const ExpressionVar& rhs) : methods_(rhs.methods_), retType_(rhs.retType_) {
38 }
39 
42  if (objectsCache_.try_pop(objects)) {
43  return objects;
44  }
45  return initObjects_();
46 }
47 
49 
52  while (objectsCache_.try_pop(objects)) {
53  for (auto& o : objects) {
54  delStorage(o.first);
55  }
56  }
57 }
58 
60  if (!obj.address()) {
61  return;
62  }
63  if (obj.typeOf().isPointer() || obj.typeOf().isReference()) {
64  // just delete a void*, as that's what it was
65  void** p = static_cast<void**>(obj.address());
66  delete p;
67  } else {
68  //std::cout << "Calling Destruct on a " <<
69  // obj.typeOf().qualifiedName() << std::endl;
70  obj.typeOf().deallocate(obj.address());
71  }
72 }
73 
75  static const edm::TypeWithDict tVoid(edm::TypeWithDict::byName("void"));
76  bool ret = false;
77  if (retType == tVoid) {
79  } else if (retType.isPointer() || retType.isReference()) {
80  // in this case, I have to allocate a void*, not an object!
81  obj = edm::ObjectWithDict(retType, new void*);
82  } else {
83  obj = edm::ObjectWithDict(retType, retType.allocate());
84  ret = retType.isClass();
85  //std::cout << "ExpressionVar: reserved memory at " << obj.address() <<
86  // " for a " << retType.qualifiedName() << " returned by " <<
87  // member.name() << std::endl;
88  }
89  return ret;
90 }
91 
93  using namespace method;
94  bool ret = false;
95  switch (retType) {
96  case (doubleType):
97  ret = true;
98  break;
99  case (floatType):
100  ret = true;
101  break;
102  case (intType):
103  ret = true;
104  break;
105  case (uIntType):
106  ret = true;
107  break;
108  case (shortType):
109  ret = true;
110  break;
111  case (uShortType):
112  ret = true;
113  break;
114  case (longType):
115  ret = true;
116  break;
117  case (uLongType):
118  ret = true;
119  break;
120  case (charType):
121  ret = true;
122  break;
123  case (uCharType):
124  ret = true;
125  break;
126  case (boolType):
127  ret = true;
128  break;
129  case (enumType):
130  ret = true;
131  break;
132  case (invalid):
133  default:
134  break;
135  }
136  return ret;
137 }
138 
141  auto objects = borrowObjects();
142  auto IO = objects.begin();
143  for (auto& m : methods_) {
144  val = m.invoke(val, IO->first);
145  ++IO;
146  }
147  double ret = objToDouble(val, retType_);
148  for (auto RI = objects.rbegin(), RE = objects.rend(); RI != RE; ++RI) {
149  if (RI->second) {
150  RI->first.destruct(false);
151  }
152  }
154  return ret;
155 }
156 
158  using namespace method;
159  void* addr = obj.address();
160  double ret = 0.0;
161  switch (type) {
162  case doubleType:
163  ret = *static_cast<double*>(addr);
164  break;
165  case floatType:
166  ret = *static_cast<float*>(addr);
167  break;
168  case intType:
169  ret = *static_cast<int*>(addr);
170  break;
171  case uIntType:
172  ret = *static_cast<unsigned int*>(addr);
173  break;
174  case shortType:
175  ret = *static_cast<short*>(addr);
176  break;
177  case uShortType:
178  ret = *static_cast<unsigned short*>(addr);
179  break;
180  case longType:
181  ret = *static_cast<long*>(addr);
182  break;
183  case uLongType:
184  ret = *static_cast<unsigned long*>(addr);
185  break;
186  case charType:
187  ret = *static_cast<char*>(addr);
188  break;
189  case uCharType:
190  ret = *static_cast<unsigned char*>(addr);
191  break;
192  case boolType:
193  ret = *static_cast<bool*>(addr);
194  break;
195  case enumType:
196  ret = *static_cast<int*>(addr);
197  break;
198  default:
199  //FIXME: Error not caught in production build!
200  assert(false && "objToDouble: invalid type!");
201  break;
202  };
203  return ret;
204 }
205 
206 ExpressionLazyVar::ExpressionLazyVar(const std::vector<LazyInvoker>& methods) : methods_(methods) {}
207 
209 
212  std::vector<StorageManager> storage;
213  storage.reserve(methods_.size());
214 
215  std::vector<LazyInvoker>::const_iterator I = methods_.begin();
216  std::vector<LazyInvoker>::const_iterator E = methods_.end() - 1;
217  for (; I < E; ++I) {
218  val = I->invoke(val, storage);
219  }
220  double ret = I->invokeLast(val, storage);
221  while (not storage.empty()) {
222  storage.pop_back();
223  }
224  return ret;
225 }
static ObjectWithDict byType(TypeWithDict const &)
static bool makeStorage(edm::ObjectWithDict &obj, const edm::TypeWithDict &retType)
ret
prodAgent to be discontinued
static double objToDouble(const edm::ObjectWithDict &obj, method::TypeCode type)
method::TypeCode retType_
Definition: ExpressionVar.h:29
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)
std::vector< std::pair< edm::ObjectWithDict, bool > > Objects
Definition: ExpressionVar.h:27
static bool isValidReturnType(method::TypeCode)
Objects borrowObjects() const
std::vector< MethodInvoker > methods_
Definition: ExpressionVar.h:26
void returnObjects(Objects &&) const
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:24
bool isClass() const
oneapi::tbb::concurrent_queue< Objects > objectsCache_
Definition: ExpressionVar.h:28
bool isPointer() const
Objects initObjects_() const
bool isReference() const
def move(src, dest)
Definition: eostools.py:511
std::vector< LazyInvoker > methods_
Definition: ExpressionVar.h:64