DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
OpaqueDataBinder.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
14 // Framework include files
16 #include <DD4hep/Conditions.h>
18 
19 // C/C++ include files
20 #include <set>
21 #include <map>
22 #include <list>
23 #include <vector>
24 
25 namespace {
26 
28  template <typename T, typename Q> bool __bind__(const dd4hep::detail::ValueBinder&, T& object, const std::string& val, const Q*)
29  { object.template bind<Q>(val); return true; }
30 
32  template <typename T, typename Q> bool __bind__(const dd4hep::detail::VectorBinder&, T& object, const std::string& val, const Q*)
33  { object.template bind<std::vector<Q> >(val); return true; }
34 
36  template <typename T, typename Q> bool __bind__(const dd4hep::detail::ListBinder&, T& object, const std::string& val, const Q*)
37  { object.template bind<std::list<Q> >(val); return true; }
38 
40  template <typename T, typename Q> bool __bind__(const dd4hep::detail::SetBinder&, T& object, const std::string& val, const Q*)
41  { object.template bind<std::set<Q> >(val); return true; }
42 
44  template <typename T, typename Q> bool __bind__(const dd4hep::detail::MapBinder&, T& object, const Q*)
45  { object.template bind<Q>(); return true; }
46 
47 }
48 
50 namespace dd4hep {
51 
53  namespace detail {
54 
56  template <typename BINDER, typename T>
57  bool OpaqueDataBinder::bind(const BINDER& b, T& object, const std::string& typ, const std::string& val) {
58 #if defined(DD4HEP_HAVE_ALL_PARSERS)
59  if ( typ.substr(0,4) == "char" )
60  return __bind__(b,object,val,Primitive<char>::null_pointer());
61  else if ( typ.substr(0,13) == "unsigned char" )
62  return __bind__(b,object,val,Primitive<unsigned char>::null_pointer());
63  else if ( typ.substr(0,5) == "short" )
64  return __bind__(b,object,val,Primitive<short>::null_pointer());
65  else if ( typ.substr(0,14) == "unsigned short" )
66  return __bind__(b,object,val,Primitive<unsigned short>::null_pointer());
67  else if ( typ.substr(0,12) == "unsigned int" )
68  return __bind__(b,object,val,Primitive<unsigned int>::null_pointer());
69  else if ( typ.substr(0,13) == "unsigned long" )
70  return __bind__(b,object,val,Primitive<unsigned long>::null_pointer());
71 #else
72  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
73  if ( typ.substr(0,4) == "char" )
74  return __bind__(b,object,val,Primitive<int>::null_pointer());
75  else if ( typ.substr(0,5) == "short" )
76  return __bind__(b,object,val,Primitive<int>::null_pointer());
77 #endif
78  else if ( typ.substr(0,3) == "int" )
79  return __bind__(b,object,val,Primitive<int>::null_pointer());
80  else if ( typ.substr(0,4) == "long" )
81  return __bind__(b,object,val,Primitive<long>::null_pointer());
82  else if ( typ.substr(0,5) == "float" )
83  return __bind__(b,object,val,Primitive<float>::null_pointer());
84  else if ( typ.substr(0,6) == "double" )
85  return __bind__(b,object,val,Primitive<double>::null_pointer());
86  else if ( typ.substr(0,6) == "string" )
87  return __bind__(b,object,val,Primitive<std::string>::null_pointer());
88  else if ( typ == "std::string" )
89  return __bind__(b,object,val,Primitive<std::string>::null_pointer());
90  else if ( typ == "Histo1D" )
91  return __bind__(b,object,val,Primitive<std::string>::null_pointer());
92  else if ( typ == "Histo2D" )
93  return __bind__(b,object,val,Primitive<std::string>::null_pointer());
94  else
95  printout(INFO,"OpaqueDataBinder","++ Unknown conditions parameter type:%s val:%s",typ.c_str(),val.c_str());
96  return __bind__(b,object,val,Primitive<std::string>::null_pointer());
97  }
98  template bool OpaqueDataBinder::bind<ValueBinder,OpaqueDataBlock>( const ValueBinder& b, OpaqueDataBlock& object,
99  const std::string& typ, const std::string& val);
100  template bool OpaqueDataBinder::bind<VectorBinder,OpaqueDataBlock>( const VectorBinder& b, OpaqueDataBlock& object,
101  const std::string& typ, const std::string& val);
102  template bool OpaqueDataBinder::bind<ListBinder,OpaqueDataBlock>( const ListBinder& b, OpaqueDataBlock& object,
103  const std::string& typ, const std::string& val);
104  template bool OpaqueDataBinder::bind<SetBinder,OpaqueDataBlock>( const SetBinder& b, OpaqueDataBlock& object,
105  const std::string& typ, const std::string& val);
106 
107  template bool OpaqueDataBinder::bind<ValueBinder,Condition>( const ValueBinder& b, Condition& object,
108  const std::string& typ, const std::string& val);
109  template bool OpaqueDataBinder::bind<VectorBinder,Condition>( const VectorBinder& b, Condition& object,
110  const std::string& typ, const std::string& val);
111  template bool OpaqueDataBinder::bind<ListBinder,Condition>( const ListBinder& b, Condition& object,
112  const std::string& typ, const std::string& val);
113  template bool OpaqueDataBinder::bind<SetBinder,Condition>( const SetBinder& b, Condition& object,
114  const std::string& typ, const std::string& val);
115 
117  template <typename T>
118  bool OpaqueDataBinder::bind_sequence(T& object, const std::string& typ, const std::string& val)
119  {
120  std::size_t idx = typ.find('[');
121  std::size_t idq = typ.find(']');
122  std::string value_type = typ.substr(idx+1,idq-idx-1);
123  if ( typ.substr(0,6) == "vector" )
124  return bind(VectorBinder(), object, value_type, val);
125  else if ( typ.substr(0,4) == "list" )
126  return bind(ListBinder(), object, value_type, val);
127  else if ( typ.substr(0,3) == "set" )
128  return bind(SetBinder(), object, value_type, val);
129  else if ( idx == std::string::npos && idq == std::string::npos )
130  return bind(ValueBinder(), object, value_type, val);
131  return false;
132  }
133 
134  template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
135  static void emplace_map_item(const BINDER&,
136  OBJECT& object,
137  const KEY& k,
138  const std::string& val,
139  const VAL*)
140  {
141  typedef std::map<KEY,VAL> map_t;
142  map_t& m = object.template get<map_t>();
143  VAL v;
144  if ( !BasicGrammar::instance<VAL>().fromString(&v, val) ) {
145  except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
146  }
147  m.emplace(k,v);
148  }
149 
150  template<typename BINDER, typename OBJECT, typename KEY>
151  static void emplace_map_key(const BINDER& b,
152  OBJECT& object,
153  const std::string& key_val,
154  const std::string& val_type,
155  const std::string& val,
156  const KEY*)
157  {
158  KEY key;
159  BasicGrammar::instance<KEY>().fromString(&key, key_val);
160  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
161  if ( val_type.substr(0,4) == "char" )
162  emplace_map_item(b, object, key, val, (int*)0);
163  else if ( val_type.substr(0,5) == "short" )
164  emplace_map_item(b, object, key, val, (int*)0);
165  else if ( val_type.substr(0,3) == "int" )
166  emplace_map_item(b, object, key, val, (int*)0);
167  else if ( val_type.substr(0,4) == "long" )
168  emplace_map_item(b, object, key, val, (long*)0);
169  else if ( val_type.substr(0,5) == "float" )
170  emplace_map_item(b, object, key, val, (float*)0);
171  else if ( val_type.substr(0,6) == "double" )
172  emplace_map_item(b, object, key, val, (double*)0);
173  else if ( val_type.substr(0,6) == "string" )
174  emplace_map_item(b, object, key, val, (std::string*)0);
175  else if ( val_type == "std::string" )
176  emplace_map_item(b, object, key, val, (std::string*)0);
177  else {
178  printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
179  val_type.c_str(),val.c_str());
180  emplace_map_item(b, object, key, val, (std::string*)0);
181  }
182  }
183 
184  template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
185  static void emplace_map_pair(const BINDER&,
186  OBJECT& object,
187  const std::string& data,
188  const KEY*,
189  const VAL*)
190  {
191  typedef std::map<KEY,VAL> map_t;
192  std::pair<KEY,VAL> entry;
193  map_t& m = object.template get<map_t>();
194  if ( !BasicGrammar::instance<std::pair<KEY,VAL> >().fromString(&entry,data) ) {
195  except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
196  }
197  m.emplace(entry);
198  }
199 
200  template<typename BINDER, typename OBJECT, typename KEY>
201  static void emplace_map_data(const BINDER& b,
202  OBJECT& object,
203  const std::string& val_type,
204  const std::string& pair_data,
205  const KEY*)
206  {
207  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
208  if ( val_type.substr(0,4) == "char" )
209  emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
210  else if ( val_type.substr(0,5) == "short" )
211  emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
212  else if ( val_type.substr(0,3) == "int" )
213  emplace_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
214  else if ( val_type.substr(0,4) == "long" )
215  emplace_map_pair(b, object, pair_data, (KEY*)0, (long*)0);
216  else if ( val_type.substr(0,5) == "float" )
217  emplace_map_pair(b, object, pair_data, (KEY*)0, (float*)0);
218  else if ( val_type.substr(0,6) == "double" )
219  emplace_map_pair(b, object, pair_data, (KEY*)0, (double*)0);
220  else if ( val_type.substr(0,6) == "string" )
221  emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
222  else if ( val_type == "std::string" )
223  emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
224  else {
225  printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
226  val_type.c_str(),pair_data.c_str());
227  emplace_map_pair(b, object, pair_data, (KEY*)0, (std::string*)0);
228  }
229  }
230 
231  template<typename BINDER, typename OBJECT, typename KEY>
232  static void bind_mapping(const BINDER& b, const std::string& val_type, OBJECT& object, const KEY*) {
233  if ( val_type.substr(0,3) == "int" )
234  __bind__(b,object, (std::map<KEY,int>*)0);
235 #if defined(DD4HEP_HAVE_ALL_PARSERS)
236  else if ( val_type.substr(0,12) == "unsigned int" )
237  __bind__(b,object, (std::map<KEY,unsigned int>*)0);
238  else if ( val_type.substr(0,4) == "char" )
239  __bind__(b,object, (std::map<KEY,char>*)0);
240  else if ( val_type.substr(0,13) == "unsigned char" )
241  __bind__(b,object, (std::map<KEY,unsigned char>*)0);
242  else if ( val_type.substr(0,5) == "short" )
243  __bind__(b,object, (std::map<KEY,short>*)0);
244  else if ( val_type.substr(0,14) == "unsigned short" )
245  __bind__(b,object, (std::map<KEY,unsigned short>*)0);
246  else if ( val_type.substr(0,13) == "unsigned long" )
247  __bind__(b,object, (std::map<KEY,unsigned long>*)0);
248 #else
249  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
250  else if ( val_type.substr(0,4) == "char" )
251  __bind__(b,object, (std::map<KEY,int>*)0);
252  else if ( val_type.substr(0,5) == "short" )
253  __bind__(b,object, (std::map<KEY,int>*)0);
254 #endif
255  else if ( val_type.substr(0,4) == "long" )
256  __bind__(b,object, (std::map<KEY,long>*)0);
257  else if ( val_type.substr(0,5) == "float" )
258  __bind__(b,object, (std::map<KEY,float>*)0);
259  else if ( val_type.substr(0,6) == "double" )
260  __bind__(b,object, (std::map<KEY,double>*)0);
261  else if ( val_type.substr(0,6) == "string" )
262  __bind__(b,object, (std::map<KEY,std::string>*)0);
263  else if ( val_type == "std::string" )
264  __bind__(b,object, (std::map<KEY,std::string>*)0);
265  else {
266  __bind__(b,object, (std::map<KEY,std::string>*)0);
267  }
268  }
269 
271  template <typename BINDER, typename OBJECT>
272  bool OpaqueDataBinder::bind_map(const BINDER& b, OBJECT& object,
273  const std::string& key_type, const std::string& val_type) {
274  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
275  if ( key_type.substr(0,3) == "int" )
276  bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
277 #if defined(DD4HEP_HAVE_ALL_PARSERS)
278  else if ( key_type.substr(0,4) == "char" )
279  bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
280  else if ( key_type.substr(0,5) == "short" )
281  bind_mapping(b, val_type, object, Primitive<int>::null_pointer());
282  else if ( key_type.substr(0,4) == "long" )
283  bind_mapping(b, val_type, object, Primitive<long>::null_pointer());
284  else if ( key_type.substr(0,5) == "float" )
285  bind_mapping(b, val_type, object, Primitive<float>::null_pointer());
286  else if ( key_type.substr(0,6) == "double" )
287  bind_mapping(b, val_type, object, Primitive<double>::null_pointer());
288 #endif
289  else if ( key_type.substr(0,6) == "string" )
290  bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
291  else if ( key_type == "std::string" )
292  bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
293  else {
294  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
295  bind_mapping(b, val_type, object, Primitive<std::string>::null_pointer());
296  }
297  return true;
298  }
299 
301  template <typename BINDER, typename OBJECT>
302  bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
303  const std::string& key_type, const std::string& key,
304  const std::string& val_type, const std::string& val)
305  {
306  if ( key_type.substr(0,3) == "int" )
307  emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
308 #if defined(DD4HEP_HAVE_ALL_PARSERS)
309  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
310  else if ( key_type.substr(0,4) == "char" )
311  emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
312  else if ( key_type.substr(0,5) == "short" )
313  emplace_map_key(b, object, key, val_type, val, Primitive<int>::null_pointer());
314  else if ( key_type.substr(0,4) == "long" )
315  emplace_map_key(b, object, key, val_type, val, Primitive<long>::null_pointer());
316  else if ( key_type.substr(0,5) == "float" )
317  emplace_map_key(b, object, key, val_type, val, Primitive<float>::null_pointer());
318  else if ( key_type.substr(0,6) == "double" )
319  emplace_map_key(b, object, key, val_type, val, Primitive<double>::null_pointer());
320 #endif
321  else if ( key_type.substr(0,6) == "string" )
322  emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
323  else if ( key_type == "std::string" )
324  emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
325  else {
326  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
327  emplace_map_key(b, object, key, val_type, val, Primitive<std::string>::null_pointer());
328  }
329  return true;
330  }
331 
333  template <typename BINDER, typename OBJECT>
334  bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
335  const std::string& key_type, const std::string& val_type,
336  const std::string& pair_data)
337  {
338  if ( key_type.substr(0,3) == "int" )
339  emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
340 #if defined(DD4HEP_HAVE_ALL_PARSERS)
341  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
342  else if ( key_type.substr(0,4) == "char" )
343  emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
344  else if ( key_type.substr(0,5) == "short" )
345  emplace_map_data(b, object, val_type, pair_data, Primitive<int>::null_pointer());
346  else if ( key_type.substr(0,4) == "long" )
347  emplace_map_data(b, object, val_type, pair_data, Primitive<long>::null_pointer());
348  else if ( key_type.substr(0,5) == "float" )
349  emplace_map_data(b, object, val_type, pair_data, Primitive<float>::null_pointer());
350  else if ( key_type.substr(0,6) == "double" )
351  emplace_map_data(b, object, val_type, pair_data, Primitive<double>::null_pointer());
352 #endif
353  else if ( key_type.substr(0,6) == "string" )
354  emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
355  else if ( key_type == "std::string" )
356  emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
357  else {
358  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
359  emplace_map_data(b, object, val_type, pair_data, Primitive<std::string>::null_pointer());
360  }
361  return true;
362  }
363 
364  template bool OpaqueDataBinder::bind_sequence<OpaqueDataBlock>(OpaqueDataBlock& object,const std::string& typ,const std::string& val);
365  template bool OpaqueDataBinder::bind_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
366  const std::string& typ,const std::string& val);
367  template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
368  const std::string& key_type, const std::string& key,
369  const std::string& val_type, const std::string& val);
370  template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
371  const std::string& key_type, const std::string& val_type,
372  const std::string& pair_data);
373 
375  template bool OpaqueDataBinder::bind_sequence<Condition>( Condition& object,
376  const std::string& typ,const std::string& val);
378  template <> bool OpaqueDataBinder::bind_map(const MapBinder& b, Condition& object,
379  const std::string& key_type, const std::string& val_type)
380  { return bind_map(b, object->data, key_type, val_type); }
381 
383  template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Condition& object,
384  const std::string& key_type, const std::string& key,
385  const std::string& val_type, const std::string& val)
386  { return insert_map(b, object->data, key_type, key, val_type, val); }
387 
389  template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Condition& object,
390  const std::string& key_type, const std::string& val_type, const std::string& pair_data)
391  { return insert_map(b, object->data, key_type, val_type, pair_data); }
392  }
393 }
dd4hep::detail::ValueBinder
Helper class to bind string values to C++ data objects (primitive or complex)
Definition: OpaqueDataBinder.h:34
dd4hep::detail::OpaqueDataBinder::bind_sequence
static bool bind_sequence(OBJECT &object, const std::string &typ, const std::string &val)
Binding function for sequences (unmapped STL containers)
v
View * v
Definition: MultiView.cpp:28
ConditionsInterna.h
dd4hep::detail::VectorBinder
Helper class to bind string values to a STL vector of data objects (primitive or complex)
Definition: OpaqueDataBinder.h:42
dd4hep::OpaqueDataBlock
Class describing an opaque conditions data block.
Definition: OpaqueData.h:92
dd4hep::BasicGrammar::instance
static const BasicGrammar & instance()
Instance factory.
Definition: GrammarParsed.h:274
dd4hep::Condition
Main condition object handle.
Definition: Conditions.h:51
dd4hep::detail::ListBinder
Helper class to bind string values to a STL list of data objects (primitive or complex)
Definition: OpaqueDataBinder.h:50
Conditions.h
dd4hep::detail::OpaqueDataBinder::bind
static bool bind(const BINDER &b, OBJECT &object, const std::string &typ, const std::string &val)
Binding function for scalar items. See the implementation function for the concrete instantiations.
dd4hep::detail::OpaqueDataBinder::insert_map
static bool insert_map(const BINDER &b, OBJECT &o, const std::string &key_type, const std::string &key, const std::string &val_type, const std::string &val)
Filling function for STL maps.
Definition: OpaqueDataBinder.cpp:302
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::detail::OpaqueDataBinder::bind_map
static bool bind_map(const BINDER &b, OBJECT &o, const std::string &key_type, const std::string &val_type)
Binding function for STL maps. Does not fill data!
Definition: OpaqueDataBinder.cpp:272
dd4hep::detail::MapBinder
Helper class to bind STL map objects.
Definition: OpaqueDataBinder.h:66
OpaqueDataBinder.h
dd4hep::Condition::data
OpaqueDataBlock & data() const
Access the opaque data block.
Definition: Conditions.cpp:129
dd4hep::detail::SetBinder
Helper class to bind string values to a STL set of data objects (primitive or complex)
Definition: OpaqueDataBinder.h:58