19 #include <Evaluator/Evaluator.h>
22 #include <TDataType.h>
30 #if defined(DD4HEP_PARSER_HEADER)
32 #define DD4HEP_NEED_EVALUATOR
35 #include DD4HEP_PARSER_HEADER
40 const dd4hep::tools::Evaluator&
evaluator();
52 typedef std::pair<grammar_create_t, dd4hep::BasicGrammar::specialization_t> grammar_args_t;
55 static std::map<dd4hep::BasicGrammar::key_type, dd4hep::BasicGrammar*>& active_registry() {
56 static std::map<dd4hep::BasicGrammar::key_type, dd4hep::BasicGrammar*> s_registry;
59 static std::map<dd4hep::BasicGrammar::key_type, grammar_args_t>& prenote_registry() {
60 static std::map<dd4hep::BasicGrammar::key_type, grammar_args_t> s_registry;
84 else if ( type ==
"Geant4" || type ==
"G4" )
86 else if ( type ==
"CGS" )
89 except(
"Grammar",
"++ Undefined evaluator type: "+type);
105 if ( j != prenote_registry().end() ) {
108 if ( !active_registry().emplace(
hash_value,
this).second ) {
122 #ifdef DD4HEP_DEBUG_PROPERTIES
123 std::cout <<
"pre_note(1) " << typeName(
info)
124 <<
" " << (
void*)specs.
str
128 if ( !prenote_registry().emplace(
hash, std::make_pair(fcn,specs)).second ) {
129 auto j = prenote_registry().find(
hash);
130 const auto& entry = (*j).second;
131 #ifdef DD4HEP_DEBUG_PROPERTIES
132 const auto& gramm = entry.first();
133 std::cout <<
"pre_note(2) " << typeName(
info)
134 <<
" " << (
void*)gramm.specialization.fromString
135 <<
" " << (
void*)entry.second.fromString
138 if ( !(entry.first == fcn && entry.second == specs) ) {
140 dd4hep::except(
"BasicGrammar",
"FAILED to add existent registry: %s [%016llX]",
145 auto i = active_registry().find(
hash);
146 if ( i != active_registry().end() ) {
147 i->second->specialization = specs;
153 auto i = active_registry().find(
hash);
154 if ( i != active_registry().end() ) {
155 #ifdef DD4HEP_DEBUG_PROPERTIES
156 const auto& entry = (*i).second;
157 const auto& gramm = *entry;
158 std::cout <<
"get(1) " <<
hash
159 <<
" grammar: " << (
void*)&gramm
160 <<
" " << (
void*)gramm.specialization.fromString
161 <<
" " << (
void*)entry->specialization.fromString
166 auto j = prenote_registry().find(
hash);
167 if ( j != prenote_registry().end() ) {
168 #ifdef DD4HEP_DEBUG_PROPERTIES
169 const auto& entry = (*j).second;
170 const auto& gramm = entry.first();
171 std::cout <<
"get(2) " <<
hash
172 <<
" " << (
void*)gramm.specialization.fromString
173 <<
" " << (
void*)entry.second.fromString
176 return (j->second.first)();
178 dd4hep::except(
"BasicGrammar",
"FAILED to look up non existent registry: %016llX",
hash);
185 auto i = active_registry().find(
hash);
186 if ( i != active_registry().end() )
188 auto j = prenote_registry().find(
hash);
189 if ( j != prenote_registry().end() )
190 return (j->second.first)();
191 dd4hep::except(
"BasicGrammar",
"FAILED to look up non existent registry: %016llX [%s]",
198 std::lock_guard<std::mutex> lock(s_mutex);
200 TClass* cl = gROOT->GetClass(type());
206 root_data_type = TDataType::GetType(type());
207 if ( root_data_type == kOther_t ) {
208 except(
"BasicGrammar",
209 "+++ ERROR +++ Cannot initialize gammar object: %s. "
210 "No TClass and no data type information present!",name.c_str());
219 return root_data_type;
230 std::string to_name = typeName(to);
231 throw unrelated_value_error(to,
232 "Data conversion of " + value +
" to type '" +
233 to_name +
"' is not defined.");
238 std::string to_name = typeName(to);
239 std::string from_name = typeName(from);
240 throw unrelated_type_error(from, to,
241 "Data conversion from '" + from_name +
242 "' to '" + to_name +
"' is not implemented.");
253 if ( specialization.cast )
254 return *specialization.cast;
255 except(
"Grammar",
"Cannot serialize object with incomplete grammar: %s",type_name().c_str());
256 return *specialization.cast;
261 if ( specialization.str )
262 return specialization.str(*
this, ptr);
263 except(
"Grammar",
"Cannot serialize object with incomplete grammar: %s", type_name().c_str());
269 if ( specialization.fromString )
270 return specialization.fromString(*
this, ptr, value);
271 except(
"Grammar",
"Cannot deserialize object with incomplete grammar: %s [%s] %p fromString: %s",
272 type_name().c_str(), this->name.c_str(), (
void*)
this, (
void*)specialization.fromString);
278 if ( specialization.eval )
279 return specialization.eval(*
this, ptr, value);
280 except(
"Grammar",
"Cannot evaluate object with incomplete grammar: %s", type_name().c_str());
286 size_t idx = val.find(
"(int)");
287 if (idx != std::string::npos)
289 while (val[0] ==
' ')
291 auto result = s__eval->evaluate(val.c_str());
297 bool ignore_blanks =
true;
298 bool str_open =
false;
299 bool obj_open =
false;
301 std::string res =
"";
304 for(
const char* c = in.c_str(); *c; ++c) {
308 if ( start ) { start =
false; }
309 if ( str_open ) { str_open =
false; }
312 if ( str_open ) { res +=
'\''; }
317 ignore_blanks =
true;
320 if ( !obj_open && str_open ) { res +=
'\''; str_open =
false; }
323 ignore_blanks =
true;
337 ignore_blanks =
true;
348 if ( !ignore_blanks ) res += *c;
352 if ( !obj_open ) res +=
'\'';
356 ignore_blanks =
false;
367 bool ignore_blanks =
true;
368 bool str_open =
false;
370 std::string res =
"";
373 for(
const char* c = in.c_str(); *c; ++c) {
377 if ( start ) { start =
false; }
378 if ( str_open ) { str_open =
false; }
381 if ( str_open ) { res +=
'\''; }
386 ignore_blanks =
true;
389 if ( str_open ) { res +=
'\''; str_open =
false; }
392 ignore_blanks =
true;
399 ignore_blanks =
true;
404 if ( str_open ) { res +=
'\''; str_open =
false; }
408 if ( !ignore_blanks ) res += *c;
416 ignore_blanks =
false;
427 std::string res =
"";
429 for(
const char* c = in.c_str(); *c; ++c) {
432 return "Bad object representation";