21 #ifndef DD4HEP_GRAMMARPARSED_H
22 #define DD4HEP_GRAMMARPARSED_H
24 #if defined(DD4HEP_GRAMMARUNPARSED_H)
25 #error "The header files GrammarParsed.h and GrammarUnparsed.h may not be included in the same compilation unit!"
31 #include <Parsers/Parsers.h>
32 #include <Evaluator/Evaluator.h>
61 #ifdef DD4HEP_DEBUG_PROPERTIES
62 std::cout <<
"Parsing " << val << std::endl;
66 *(TYPE*)ptr = std::move(temp);
72 #ifdef DD4HEP_DEBUG_PROPERTIES
73 std::cout <<
"Parsing " << val <<
"FAILED" << std::endl;
75 if ( !sc ) sc = gr.
evaluate(&temp,val);
76 #ifdef DD4HEP_DEBUG_PROPERTIES
77 std::cout <<
"Sc=" << sc <<
" Converting value: " << val
78 <<
" to type " <<
typeid(TYPE).name()
82 *(TYPE*)ptr = std::move(temp);
91 std::stringstream string_rep;
93 return string_rep.str();
97 template <
typename T>
inline int eval_item(T* ptr,
const std::string& val) {
111 if (result.first == tools::Evaluator::OK) {
112 *ptr = (T)result.second;
120 template <>
inline int eval_item<std::string>(std::string* ptr,
const std::string& val) {
126 template <
typename TYPE>
static int fill_data(std::vector<TYPE>* p,
const std::vector<std::string>& temp) {
128 const BasicGrammar& grammar = BasicGrammar::instance<TYPE>();
129 for(
auto i=std::begin(temp); i != std::end(temp); ++i) {
132 p->emplace_back(val);
138 template <
typename TYPE>
static int fill_data(std::list<TYPE>* p,
const std::vector<std::string>& temp) {
140 const BasicGrammar& grammar = BasicGrammar::instance<TYPE>();
141 for(
auto i=std::begin(temp); i != std::end(temp); ++i) {
142 if ( !grammar.fromString(&val,*i) )
144 p->emplace_back(val);
150 template <
typename TYPE>
static int fill_data(std::set<TYPE>* p,
const std::vector<std::string>& temp) {
152 const BasicGrammar& grammar = BasicGrammar::instance<TYPE>();
153 for(
auto i=std::begin(temp); i != std::end(temp); ++i) {
154 if ( !grammar.fromString(&val,*i) )
162 template <
typename TYPE>
static int fill_data(std::deque<TYPE>* p,
const std::vector<std::string>& temp) {
164 const BasicGrammar& grammar = BasicGrammar::instance<TYPE>();
165 for(
auto i=std::begin(temp); i != std::end(temp); ++i) {
166 if ( !grammar.fromString(&val,*i) )
168 p->emplace_back(val);
174 template <
typename KEY,
typename TYPE>
static int fill_data(std::map<KEY,TYPE>* p,
const std::vector<std::string>& temp) {
175 std::pair<KEY,TYPE> val;
176 const BasicGrammar& grammar = BasicGrammar::instance<std::pair<KEY,TYPE> >();
177 for(
auto i=std::begin(temp); i != std::end(temp); ++i) {
178 if ( !grammar.fromString(&val,*i) )
186 template <
typename TYPE>
static int eval_map(TYPE* p,
const std::string& str) {
187 std::vector<std::string> buff;
191 return fill_data(p,buff);
195 std::map<std::string,std::string> map_buff;
213 for(
const auto& _o : map_buff ) {
214 typename TYPE::key_type _k {};
215 typename TYPE::mapped_type _v {};
227 template <
typename TYPE>
static int eval_container(TYPE* p,
const std::string& str) {
228 std::vector<std::string> buff;
231 return fill_data(p,buff);
239 *p = std::move(temp);
246 *p = std::move(temp);
252 return fill_data(p,buff);
259 template <
typename T,
typename Q>
inline int eval_pair(std::pair<T,Q>* ptr, std::string str) {
260 const BasicGrammar& grammar = BasicGrammar::instance<std::pair<T,Q> >();
266 template<
typename T>
inline int eval_obj(T* ptr,
const std::string& str) {
292 #define DD4HEP_PARSER_GRAMMAR_CNAME(serial,name) namespace_dd4hep__grammar_##serial##_##name
294 #define DD4HEP_DEFINE_PARSER_GRAMMAR_EVAL(xx,func) \
295 namespace dd4hep { namespace detail { \
296 template<> int grammar_eval<xx>(const BasicGrammar&, void* _p, const std::string& _v) { \
297 return func ((xx*)_p,_v); \
301 #define DD4HEP_DEFINE_PARSER_GRAMMAR_INSTANCE(serial,xx) \
303 template class Grammar< xx >; \
304 template BasicGrammar const& BasicGrammar::instance< xx >(); \
305 template const GrammarRegistry& GrammarRegistry::pre_note<xx>(int); \
307 namespace DD4HEP_PARSER_GRAMMAR_CNAME(serial,0) { \
308 static auto s_reg = ::dd4hep::GrammarRegistry::pre_note< xx >(1); \
311 #define DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,ctxt,xx,func) \
312 DD4HEP_DEFINE_PARSER_GRAMMAR_EVAL(xx,func) \
313 DD4HEP_DEFINE_PARSER_GRAMMAR_INSTANCE(DD4HEP_PARSER_GRAMMAR_CNAME(serial,ctxt),xx)
315 #if defined(DD4HEP_HAVE_ALL_PARSERS)
316 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(serial,xx,eval_func) \
317 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,1,x,eval_func) \
318 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,2,std::vector<xx>, eval_container) \
319 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,3,std::list<xx>, eval_container) \
320 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,4,std::set<xx>, eval_container) \
321 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,5,std::deque<xx>, eval_container) \
322 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,6,dd4hep::detail::Primitive<xx>::int_map_t, eval_container) \
323 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,7,dd4hep::detail::Primitive<xx>::ulong_map_t, eval_container) \
324 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,8,dd4hep::detail::Primitive<xx>::string_map_t, eval_container) \
325 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,9,dd4hep::detail::Primitive<xx>::int_pair_t, eval_pair) \
326 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,10,dd4hep::detail::Primitive<xx>::ulong_pair_t, eval_pair) \
327 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,11,dd4hep::detail::Primitive<xx>::string_pair_t, eval_pair)
329 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_VL_SERIAL(serial,xx,eval_func) \
330 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,12,xx,eval_func) \
331 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,13,std::vector<xx>,eval_container) \
332 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,14,std::list<xx>,eval_container)
334 #define DD4HEP_DEFINE_PARSER_GRAMMAR_U_CONT_SERIAL(serial,xx) \
335 DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(serial,xx,eval_item) \
336 DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(serial,unsigned xx,eval_item)
340 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(serial,xx,eval_func) \
341 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,1,xx,eval_func) \
342 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,2,std::vector<xx>, eval_container) \
343 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,3,std::list<xx>, eval_container) \
344 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,4,std::set<xx>, eval_container) \
345 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,5,dd4hep::detail::Primitive<xx>::int_map_t, eval_map) \
346 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,6,dd4hep::detail::Primitive<xx>::string_map_t, eval_map) \
347 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,7,dd4hep::detail::Primitive<xx>::int_pair_t, eval_pair) \
348 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,8,dd4hep::detail::Primitive<xx>::string_pair_t, eval_pair)
350 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_ROOTMATH(serial,xx,eval_func) \
351 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,1,xx,eval_func) \
352 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,2,std::vector<xx>, eval_container) \
353 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,3,std::list<xx>, eval_container) \
354 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,4,std::set<xx>, eval_container) \
355 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,5,dd4hep::detail::Primitive<xx>::int_map_t, eval_container) \
356 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,6,dd4hep::detail::Primitive<xx>::string_map_t, eval_container) \
357 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,7,dd4hep::detail::Primitive<xx>::int_pair_t, eval_pair) \
358 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,8,dd4hep::detail::Primitive<xx>::string_pair_t, eval_pair)
360 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_VL_SERIAL(serial,xx,eval_func) \
361 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,9,xx,eval_func) \
362 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,10,std::vector<xx>,eval_container) \
363 DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(serial,11,std::list<xx>,eval_container)
365 #define DD4HEP_DEFINE_PARSER_GRAMMAR_U_CONT_SERIAL(serial,xx) \
366 DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(serial,xx,eval_item)
370 #define DD4HEP_DEFINE_PARSER_GRAMMAR(xx,func) DD4HEP_DEFINE_PARSER_GRAMMAR_SERIAL(__LINE__,__LINE__,xx,func)
371 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT(xx,eval_func) DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_SERIAL(__LINE__,xx,eval_func)
372 #define DD4HEP_DEFINE_PARSER_GRAMMAR_ROOTMATH(xx,eval_func) DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_ROOTMATH(__LINE__,xx,eval_func)
373 #define DD4HEP_DEFINE_PARSER_GRAMMAR_U_CONT(xx) DD4HEP_DEFINE_PARSER_GRAMMAR_U_CONT_SERIAL(__LINE__,xx)
374 #define DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_VL(xx,eval_func) DD4HEP_DEFINE_PARSER_GRAMMAR_CONT_VL_SERIAL(__LINE__,xx,eval_func)
375 #define DD4HEP_DEFINE_PARSER_GRAMMAR_DUMMY(xx,func) DD4HEP_DEFINE_PARSER_GRAMMAR_DUMMY_SERIAL(__LINE__,xx,func)
377 #endif // DD4HEP_GRAMMARPARSED_H