19 #include <Evaluator/Evaluator.h>
22 #include <TDataType.h>
31 #if defined(DD4HEP_PARSER_HEADER)
33 #define DD4HEP_NEED_EVALUATOR
36 #include DD4HEP_PARSER_HEADER
41 const dd4hep::tools::Evaluator&
evaluator();
53 typedef std::pair<grammar_create_t, dd4hep::BasicGrammar::specialization_t> grammar_args_t;
56 static std::map<dd4hep::BasicGrammar::key_type, dd4hep::BasicGrammar*>& active_registry() {
57 static std::map<dd4hep::BasicGrammar::key_type, dd4hep::BasicGrammar*> s_registry;
60 static std::map<dd4hep::BasicGrammar::key_type, grammar_args_t>& prenote_registry() {
61 static std::map<dd4hep::BasicGrammar::key_type, grammar_args_t> s_registry;
85 else if ( type ==
"Geant4" || type ==
"G4" )
87 else if ( type ==
"CGS" )
90 except(
"Grammar",
"++ Undefined evaluator type: "+type);
106 if ( j != prenote_registry().end() ) {
109 if ( !active_registry().emplace(
hash_value,
this).second ) {
123 #ifdef DD4HEP_DEBUG_PROPERTIES
124 std::cout <<
"pre_note(1) " << typeName(
info)
125 <<
" " << (
void*)specs.
str
129 if ( !prenote_registry().emplace(
hash, std::make_pair(fcn,specs)).second ) {
130 auto j = prenote_registry().find(
hash);
131 const auto& entry = (*j).second;
132 #ifdef DD4HEP_DEBUG_PROPERTIES
133 const auto& gramm = entry.first();
134 std::cout <<
"pre_note(2) " << typeName(
info)
135 <<
" " << (
void*)gramm.specialization.fromString
136 <<
" " << (
void*)entry.second.fromString
139 if ( !(entry.first == fcn && entry.second == specs) ) {
141 dd4hep::except(
"BasicGrammar",
"FAILED to add existent registry: %s [%016llX]",
146 auto i = active_registry().find(
hash);
147 if ( i != active_registry().end() ) {
148 i->second->specialization = specs;
154 auto i = active_registry().find(
hash);
155 if ( i != active_registry().end() ) {
156 #ifdef DD4HEP_DEBUG_PROPERTIES
157 const auto& entry = (*i).second;
158 const auto& gramm = *entry;
159 std::cout <<
"get(1) " <<
hash
160 <<
" grammar: " << (
void*)&gramm
161 <<
" " << (
void*)gramm.specialization.fromString
162 <<
" " << (
void*)entry->specialization.fromString
167 auto j = prenote_registry().find(
hash);
168 if ( j != prenote_registry().end() ) {
169 #ifdef DD4HEP_DEBUG_PROPERTIES
170 const auto& entry = (*j).second;
171 const auto& gramm = entry.first();
172 std::cout <<
"get(2) " <<
hash
173 <<
" " << (
void*)gramm.specialization.fromString
174 <<
" " << (
void*)entry.second.fromString
177 return (j->second.first)();
179 dd4hep::except(
"BasicGrammar",
"FAILED to look up non existent registry: %016llX",
hash);
186 auto i = active_registry().find(
hash);
187 if ( i != active_registry().end() )
189 auto j = prenote_registry().find(
hash);
190 if ( j != prenote_registry().end() )
191 return (j->second.first)();
192 dd4hep::except(
"BasicGrammar",
"FAILED to look up non existent registry: %016llX [%s]",
199 std::lock_guard<std::mutex> lock(s_mutex);
201 TClass* cl = gROOT->GetClass(type());
207 root_data_type = TDataType::GetType(type());
208 if ( root_data_type == kOther_t ) {
209 except(
"BasicGrammar",
210 "+++ ERROR +++ Cannot initialize gammar object: %s. "
211 "No TClass and no data type information present!",name.c_str());
220 return root_data_type;
231 std::string to_name = typeName(to);
232 throw unrelated_value_error(to,
233 "Data conversion of " + value +
" to type '" +
234 to_name +
"' is not defined.");
239 std::string to_name = typeName(to);
240 std::string from_name = typeName(from);
241 throw unrelated_type_error(from, to,
242 "Data conversion from '" + from_name +
243 "' to '" + to_name +
"' is not implemented.");
254 if ( specialization.cast )
255 return *specialization.cast;
256 except(
"Grammar",
"Cannot serialize object with incomplete grammar: %s",type_name().c_str());
257 return *specialization.cast;
262 if ( specialization.str )
263 return specialization.str(*
this, ptr);
264 except(
"Grammar",
"Cannot serialize object with incomplete grammar: %s", type_name().c_str());
270 if ( specialization.fromString )
271 return specialization.fromString(*
this, ptr, value);
272 except(
"Grammar",
"Cannot deserialize object with incomplete grammar: %s [%s] %p fromString: %s",
273 type_name().c_str(), this->name.c_str(), (
void*)
this, (
void*)specialization.fromString);
279 if ( specialization.eval )
280 return specialization.eval(*
this, ptr, value);
281 except(
"Grammar",
"Cannot evaluate object with incomplete grammar: %s", type_name().c_str());
287 size_t idx = val.find(
"(int)");
288 if (idx != std::string::npos)
290 while (val[0] ==
' ')
292 auto result = s__eval->evaluate(val.c_str());
298 bool ignore_blanks =
true;
299 bool str_open =
false;
300 bool obj_open =
false;
302 std::string res =
"";
305 for(
const char* c = in.c_str(); *c; ++c) {
309 if ( start ) { start =
false; }
310 if ( str_open ) { str_open =
false; }
313 if ( str_open ) { res +=
'\''; }
318 ignore_blanks =
true;
321 if ( !obj_open && str_open ) { res +=
'\''; str_open =
false; }
324 ignore_blanks =
true;
338 ignore_blanks =
true;
349 if ( !ignore_blanks ) res += *c;
353 if ( !obj_open ) res +=
'\'';
357 ignore_blanks =
false;
368 bool ignore_blanks =
true;
369 bool str_open =
false;
371 std::string res =
"";
374 for(
const char* c = in.c_str(); *c; ++c) {
378 if ( start ) { start =
false; }
379 if ( str_open ) { str_open =
false; }
382 if ( str_open ) { res +=
'\''; }
387 ignore_blanks =
true;
390 if ( str_open ) { res +=
'\''; str_open =
false; }
393 ignore_blanks =
true;
400 ignore_blanks =
true;
405 if ( str_open ) { res +=
'\''; str_open =
false; }
409 if ( !ignore_blanks ) res += *c;
417 ignore_blanks =
false;
428 std::string res =
"";
430 for(
const char* c = in.c_str(); *c; ++c) {
433 return "Bad object representation";