DD4hep - The AIDA detector description toolkit for high energy physics experiments
DD4hep  Rev:Unversioneddirectory
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpaqueDataBinder.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation for LCD
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
18 #include "DD4hep/Conditions.h"
19 
20 // C/C++ include files
21 #include <set>
22 #include <map>
23 #include <list>
24 #include <vector>
25 
26 using namespace std;
27 
28 namespace {
29  using namespace DD4hep;
30 
31 #if defined(DD4HEP_HAVE_ALL_PARSERS)
32  const char* _char() { return 0; }
33  const unsigned char* _uchar() { return 0; }
34  const short* _short() { return 0; }
35  const unsigned short* _ushort() { return 0; }
36  const unsigned int* _uint() { return 0; }
37  const unsigned long* _ulong() { return 0; }
38 #endif
39  const int* _int() { return 0; }
40  const long* _long() { return 0; }
41  const float* _float() { return 0; }
42  const double* _double() { return 0; }
43  const std::string* _string() { return 0; }
44 
46  template <typename T, typename Q> bool __bind__(const ValueBinder&, T& object, const string& val, const Q*)
47  { object.template bind<Q>(val); return true; }
48 
50  template <typename T, typename Q> bool __bind__(const VectorBinder&, T& object, const string& val, const Q*)
51  { object.template bind<vector<Q> >(val); return true; }
52 
54  template <typename T, typename Q> bool __bind__(const ListBinder&, T& object, const string& val, const Q*)
55  { object.template bind<list<Q> >(val); return true; }
56 
58  template <typename T, typename Q> bool __bind__(const SetBinder&, T& object, const string& val, const Q*)
59  { object.template bind<set<Q> >(val); return true; }
60 
62  template <typename T, typename Q> bool __bind__(const MapBinder&, T& object, const Q*)
63  { object.template bind<Q>(); return true; }
64 
65 }
66 
68 namespace DD4hep {
69 
71  template <typename BINDER, typename T>
72  bool OpaqueDataBinder::bind(const BINDER& b, T& object, const string& typ, const string& val) {
73 #if defined(DD4HEP_HAVE_ALL_PARSERS)
74  if ( typ.substr(0,4) == "char" )
75  return __bind__(b,object,val,_char());
76  else if ( typ.substr(0,13) == "unsigned char" )
77  return __bind__(b,object,val,_uchar());
78  else if ( typ.substr(0,5) == "short" )
79  return __bind__(b,object,val,_short());
80  else if ( typ.substr(0,14) == "unsigned short" )
81  return __bind__(b,object,val,_ushort());
82  else if ( typ.substr(0,12) == "unsigned int" )
83  return __bind__(b,object,val,_uint());
84  else if ( typ.substr(0,13) == "unsigned long" )
85  return __bind__(b,object,val,_ulong());
86 #else
87  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
88  if ( typ.substr(0,4) == "char" )
89  return __bind__(b,object,val,_int());
90  else if ( typ.substr(0,5) == "short" )
91  return __bind__(b,object,val,_int());
92 #endif
93  else if ( typ.substr(0,3) == "int" )
94  return __bind__(b,object,val,_int());
95  else if ( typ.substr(0,4) == "long" )
96  return __bind__(b,object,val,_long());
97  else if ( typ.substr(0,5) == "float" )
98  return __bind__(b,object,val,_float());
99  else if ( typ.substr(0,6) == "double" )
100  return __bind__(b,object,val,_double());
101  else if ( typ.substr(0,6) == "string" )
102  return __bind__(b,object,val,_string());
103  else if ( typ == "std::string" )
104  return __bind__(b,object,val,_string());
105  else if ( typ == "Histo1D" )
106  return __bind__(b,object,val,_string());
107  else if ( typ == "Histo2D" )
108  return __bind__(b,object,val,_string());
109  else
110  printout(INFO,"OpaqueDataBinder","++ Unknown conditions parameter type:%s val:%s",typ.c_str(),val.c_str());
111  return __bind__(b,object,val,_string());
112  }
113  template bool OpaqueDataBinder::bind<ValueBinder,OpaqueDataBlock>( const ValueBinder& b, OpaqueDataBlock& object,
114  const string& typ, const string& val);
115  template bool OpaqueDataBinder::bind<VectorBinder,OpaqueDataBlock>( const VectorBinder& b, OpaqueDataBlock& object,
116  const string& typ, const string& val);
117  template bool OpaqueDataBinder::bind<ListBinder,OpaqueDataBlock>( const ListBinder& b, OpaqueDataBlock& object,
118  const string& typ, const string& val);
119  template bool OpaqueDataBinder::bind<SetBinder,OpaqueDataBlock>( const SetBinder& b, OpaqueDataBlock& object,
120  const string& typ, const string& val);
121 
122  template bool OpaqueDataBinder::bind<ValueBinder,Conditions::Condition>( const ValueBinder& b, Conditions::Condition& object,
123  const string& typ, const string& val);
124  template bool OpaqueDataBinder::bind<VectorBinder,Conditions::Condition>( const VectorBinder& b, Conditions::Condition& object,
125  const string& typ, const string& val);
126  template bool OpaqueDataBinder::bind<ListBinder,Conditions::Condition>( const ListBinder& b, Conditions::Condition& object,
127  const string& typ, const string& val);
128  template bool OpaqueDataBinder::bind<SetBinder,Conditions::Condition>( const SetBinder& b, Conditions::Condition& object,
129  const string& typ, const string& val);
130 
132  template <typename T>
133  bool OpaqueDataBinder::bind_sequence(T& object, const string& typ, const string& val)
134  {
135  size_t idx = typ.find('[');
136  size_t idq = typ.find(']');
137  string value_type = typ.substr(idx+1,idq-idx-1);
138  if ( typ.substr(0,6) == "vector" )
139  return bind(VectorBinder(), object, value_type, val);
140  else if ( typ.substr(0,6) == "list" )
141  return bind(ListBinder(), object, value_type, val);
142  else if ( typ.substr(0,6) == "set" )
143  return bind(SetBinder(), object, value_type, val);
144  else if ( idx == string::npos && idq == string::npos )
145  return bind(ValueBinder(), object, value_type, val);
146  return false;
147  }
148 
149  template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
150  static void insert_map_item(const BINDER&,
151  OBJECT& object,
152  const KEY& k,
153  const string& val,
154  const VAL*)
155  {
156  typedef map<KEY,VAL> map_t;
157  map_t& m = object.template get<map_t>();
158  VAL v;
159  if ( !BasicGrammar::instance<VAL>().fromString(&v, val) ) {
160  except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
161  }
162  m.insert(make_pair(k,v));
163  }
164 
165  template<typename BINDER, typename OBJECT, typename KEY>
166  static void insert_map_key(const BINDER& b,
167  OBJECT& object,
168  const string& key_val,
169  const string& val_type,
170  const string& val,
171  const KEY*)
172  {
173  KEY key;
174  BasicGrammar::instance<KEY>().fromString(&key, key_val);
175  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
176  if ( val_type.substr(0,4) == "char" )
177  insert_map_item(b, object, key, val, (int*)0);
178  else if ( val_type.substr(0,5) == "short" )
179  insert_map_item(b, object, key, val, (int*)0);
180  else if ( val_type.substr(0,3) == "int" )
181  insert_map_item(b, object, key, val, (int*)0);
182  else if ( val_type.substr(0,4) == "long" )
183  insert_map_item(b, object, key, val, (long*)0);
184  else if ( val_type.substr(0,5) == "float" )
185  insert_map_item(b, object, key, val, (float*)0);
186  else if ( val_type.substr(0,6) == "double" )
187  insert_map_item(b, object, key, val, (double*)0);
188  else if ( val_type.substr(0,6) == "string" )
189  insert_map_item(b, object, key, val, (string*)0);
190  else if ( val_type == "std::string" )
191  insert_map_item(b, object, key, val, (string*)0);
192  else {
193  printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
194  val_type.c_str(),val.c_str());
195  insert_map_item(b, object, key, val, (string*)0);
196  }
197  }
198 
199  template<typename BINDER, typename OBJECT, typename KEY, typename VAL>
200  static void insert_map_pair(const BINDER&,
201  OBJECT& object,
202  const string& data,
203  const KEY*,
204  const VAL*)
205  {
206  typedef map<KEY,VAL> map_t;
207  pair<KEY,VAL> entry;
208  map_t& m = object.template get<map_t>();
209  if ( !BasicGrammar::instance<pair<KEY,VAL> >().fromString(&entry,data) ) {
210  except("OpaqueDataBinder","++ Failed to convert conditions map entry.");
211  }
212  m.insert(entry);
213  }
214 
215  template<typename BINDER, typename OBJECT, typename KEY>
216  static void insert_map_data(const BINDER& b,
217  OBJECT& object,
218  const string& val_type,
219  const string& pair_data,
220  const KEY*)
221  {
222  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
223  if ( val_type.substr(0,4) == "char" )
224  insert_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
225  else if ( val_type.substr(0,5) == "short" )
226  insert_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
227  else if ( val_type.substr(0,3) == "int" )
228  insert_map_pair(b, object, pair_data, (KEY*)0, (int*)0);
229  else if ( val_type.substr(0,4) == "long" )
230  insert_map_pair(b, object, pair_data, (KEY*)0, (long*)0);
231  else if ( val_type.substr(0,5) == "float" )
232  insert_map_pair(b, object, pair_data, (KEY*)0, (float*)0);
233  else if ( val_type.substr(0,6) == "double" )
234  insert_map_pair(b, object, pair_data, (KEY*)0, (double*)0);
235  else if ( val_type.substr(0,6) == "string" )
236  insert_map_pair(b, object, pair_data, (KEY*)0, (string*)0);
237  else if ( val_type == "std::string" )
238  insert_map_pair(b, object, pair_data, (KEY*)0, (string*)0);
239  else {
240  printout(INFO,"Param","++ Unknown conditions parameter type:%s data:%s",
241  val_type.c_str(),pair_data.c_str());
242  insert_map_pair(b, object, pair_data, (KEY*)0, (string*)0);
243  }
244  }
245 
246  template<typename BINDER, typename OBJECT, typename KEY>
247  static void bind_mapping(const BINDER& b, const string& val_type, OBJECT& object, const KEY*) {
248  if ( val_type.substr(0,3) == "int" )
249  __bind__(b,object, (map<KEY,int>*)0);
250 #if defined(DD4HEP_HAVE_ALL_PARSERS)
251  else if ( val_type.substr(0,12) == "unsigned int" )
252  __bind__(b,object, (map<KEY,unsigned int>*)0);
253  else if ( val_type.substr(0,4) == "char" )
254  __bind__(b,object, (map<KEY,char>*)0);
255  else if ( val_type.substr(0,13) == "unsigned char" )
256  __bind__(b,object, (map<KEY,unsigned char>*)0);
257  else if ( val_type.substr(0,5) == "short" )
258  __bind__(b,object, (map<KEY,short>*)0);
259  else if ( val_type.substr(0,14) == "unsigned short" )
260  __bind__(b,object, (map<KEY,unsigned short>*)0);
261  else if ( val_type.substr(0,13) == "unsigned long" )
262  __bind__(b,object, (map<KEY,unsigned long>*)0);
263 #else
264  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
265  else if ( val_type.substr(0,4) == "char" )
266  __bind__(b,object, (map<KEY,int>*)0);
267  else if ( val_type.substr(0,5) == "short" )
268  __bind__(b,object, (map<KEY,int>*)0);
269 #endif
270  else if ( val_type.substr(0,4) == "long" )
271  __bind__(b,object, (map<KEY,long>*)0);
272  else if ( val_type.substr(0,5) == "float" )
273  __bind__(b,object, (map<KEY,float>*)0);
274  else if ( val_type.substr(0,6) == "double" )
275  __bind__(b,object, (map<KEY,double>*)0);
276  else if ( val_type.substr(0,6) == "string" )
277  __bind__(b,object, (map<KEY,string>*)0);
278  else if ( val_type == "std::string" )
279  __bind__(b,object, (map<KEY,string>*)0);
280  else {
281  __bind__(b,object, (map<KEY,string>*)0);
282  }
283  }
284 
286  template <typename BINDER, typename OBJECT>
287  bool OpaqueDataBinder::bind_map(const BINDER& b, OBJECT& object,
288  const string& key_type, const string& val_type) {
289  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
290  if ( key_type.substr(0,3) == "int" )
291  bind_mapping(b, val_type, object, _int());
292 #if defined(DD4HEP_HAVE_ALL_PARSERS)
293  else if ( key_type.substr(0,4) == "char" )
294  bind_mapping(b, val_type, object, _int());
295  else if ( key_type.substr(0,5) == "short" )
296  bind_mapping(b, val_type, object, _int());
297  else if ( key_type.substr(0,4) == "long" )
298  bind_mapping(b, val_type, object, _long());
299  else if ( key_type.substr(0,5) == "float" )
300  bind_mapping(b, val_type, object, _float());
301  else if ( key_type.substr(0,6) == "double" )
302  bind_mapping(b, val_type, object, _double());
303 #endif
304  else if ( key_type.substr(0,6) == "string" )
305  bind_mapping(b, val_type, object, _string());
306  else if ( key_type == "std::string" )
307  bind_mapping(b, val_type, object, _string());
308  else {
309  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
310  bind_mapping(b, val_type, object, _string());
311  }
312  return true;
313  }
314 
316  template <typename BINDER, typename OBJECT>
317  bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
318  const string& key_type, const string& key,
319  const string& val_type, const string& val)
320  {
321  if ( key_type.substr(0,3) == "int" )
322  insert_map_key(b, object, key, val_type, val, _int());
323 #if defined(DD4HEP_HAVE_ALL_PARSERS)
324  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
325  else if ( key_type.substr(0,4) == "char" )
326  insert_map_key(b, object, key, val_type, val, _int());
327  else if ( key_type.substr(0,5) == "short" )
328  insert_map_key(b, object, key, val_type, val, _int());
329  else if ( key_type.substr(0,4) == "long" )
330  insert_map_key(b, object, key, val_type, val, _long());
331  else if ( key_type.substr(0,5) == "float" )
332  insert_map_key(b, object, key, val_type, val, _float());
333  else if ( key_type.substr(0,6) == "double" )
334  insert_map_key(b, object, key, val_type, val, _double());
335 #endif
336  else if ( key_type.substr(0,6) == "string" )
337  insert_map_key(b, object, key, val_type, val, _string());
338  else if ( key_type == "std::string" )
339  insert_map_key(b, object, key, val_type, val, _string());
340  else {
341  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
342  insert_map_key(b, object, key, val_type, val, _string());
343  }
344  return true;
345  }
346 
348  template <typename BINDER, typename OBJECT>
349  bool OpaqueDataBinder::insert_map(const BINDER& b, OBJECT& object,
350  const std::string& key_type, const std::string& val_type,
351  const std::string& pair_data)
352  {
353  if ( key_type.substr(0,3) == "int" )
354  insert_map_data(b, object, val_type, pair_data, _int());
355 #if defined(DD4HEP_HAVE_ALL_PARSERS)
356  // Short and char is not part of the standard dictionaries. Fall back to 'int'.
357  else if ( key_type.substr(0,4) == "char" )
358  insert_map_data(b, object, val_type, pair_data, _int());
359  else if ( key_type.substr(0,5) == "short" )
360  insert_map_data(b, object, val_type, pair_data, _int());
361  else if ( key_type.substr(0,4) == "long" )
362  insert_map_data(b, object, val_type, pair_data, _long());
363  else if ( key_type.substr(0,5) == "float" )
364  insert_map_data(b, object, val_type, pair_data, _float());
365  else if ( key_type.substr(0,6) == "double" )
366  insert_map_data(b, object, val_type, pair_data, _double());
367 #endif
368  else if ( key_type.substr(0,6) == "string" )
369  insert_map_data(b, object, val_type, pair_data, _string());
370  else if ( key_type == "std::string" )
371  insert_map_data(b, object, val_type, pair_data, _string());
372  else {
373  printout(INFO,"OpaqueDataBinder","++ Unknown MAP-conditions key-type:%s",key_type.c_str());
374  insert_map_data(b, object, val_type, pair_data, _string());
375  }
376  return true;
377  }
378 
379  template bool OpaqueDataBinder::bind_sequence<OpaqueDataBlock>(OpaqueDataBlock& object,const string& typ,const string& val);
380  template bool OpaqueDataBinder::bind_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
381  const string& typ,const string& val);
382  template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
383  const string& key_type, const string& key,
384  const string& val_type, const string& val);
385  template bool OpaqueDataBinder::insert_map<MapBinder,OpaqueDataBlock>( const MapBinder& b, OpaqueDataBlock& object,
386  const string& key_type, const string& val_type,
387  const string& pair_data);
388 
390  template bool OpaqueDataBinder::bind_sequence<Conditions::Condition>( Conditions::Condition& object,
391  const string& typ,const string& val);
393  template <> bool OpaqueDataBinder::bind_map(const MapBinder& b, Conditions::Condition& object,
394  const string& key_type, const string& val_type)
395  { return bind_map(b, object->data, key_type, val_type); }
396 
398  template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Conditions::Condition& object,
399  const string& key_type, const string& key,
400  const string& val_type, const string& val)
401  { return insert_map(b, object->data, key_type, key, val_type, val); }
402 
404  template <> bool OpaqueDataBinder::insert_map(const MapBinder& b, Conditions::Condition& object,
405  const string& key_type, const string& val_type, const string& pair_data)
406  { return insert_map(b, object->data, key_type, val_type, pair_data); }
407 }
static void insert_map_item(const BINDER &, OBJECT &object, const KEY &k, const string &val, const VAL *)
Class describing an opaque conditions data block.
Definition: OpaqueData.h:81
OpaqueData & data() const
Access the IOV block.
Definition: Conditions.cpp:92
static void bind_mapping(const BINDER &b, const string &val_type, OBJECT &object, const KEY *)
Helper class to bind string values to a STL list of data objects (primitive or complex) ...
Main condition object handle.
Definition: Conditions.h:70
int except(const std::string &src, const std::string &fmt,...)
Calls the display action with ERROR and throws an std::runtime_error exception.
Definition: Printout.cpp:217
static void insert_map_data(const BINDER &b, OBJECT &object, const string &val_type, const string &pair_data, const KEY *)
Helper class to bind string values to a STL set of data objects (primitive or complex) ...
static void insert_map_pair(const BINDER &, OBJECT &object, const string &data, const KEY *, const VAL *)
Helper class to bind string values to C++ data objects (primitive or complex)
Helper class to bind string values to a STL vector of data objects (primitive or complex) ...
static void insert_map_key(const BINDER &b, OBJECT &object, const string &key_val, const string &val_type, const string &val, const KEY *)
View * v
Definition: MultiView.cpp:30
int printout(PrintLevel severity, const char *src, const char *fmt,...)
Calls the display action with a given severity level.
Definition: Printout.cpp:111
Helper class to bind STL map objects.
TGeoShape TGeoMedium * m
Definition: Volumes.cpp:294