The Gaudi Framework  v28r3 (cc1cf868)
GetData.h
Go to the documentation of this file.
1 #ifndef GAUDIUTILS_GETDATA_H
2 #define GAUDIUTILS_GETDATA_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // GaudiKernel
7 // ============================================================================
11 #include "GaudiKernel/MsgStream.h"
12 #include "GaudiKernel/IRegistry.h"
13 #include "GaudiKernel/StatusCode.h"
14 // ============================================================================
15 // GaudiUtils
16 // ============================================================================
17 #include "GaudiKernel/Range.h"
18 #include "GaudiKernel/NamedRange.h"
20 // ============================================================================
21 // Forward declaration
22 // ============================================================================
23 template <class BASE> class GaudiCommon ; // GaudiAlg
24 // ============================================================================
25 namespace Gaudi
26 {
27  namespace Utils
28  {
29  // ========================================================================
36  template <class TYPE>
37  struct _GetType
38  { typedef TYPE* return_type ; };
39  // ========================================================================
41  template <class TYPE>
42  struct _GetType<TYPE*>
43  { typedef TYPE* return_type ; };
44  // ========================================================================
46  template <class TYPE>
47  struct _GetType<TYPE&>
48  { typedef TYPE* return_type ; };
49  // ========================================================================
51  template <class CONTAINER>
52  struct _GetType<Gaudi::Range_<CONTAINER> >
54  // ========================================================================
56  template <class CONTAINER>
57  struct _GetType<Gaudi::NamedRange_<CONTAINER> >
59  // ========================================================================
64  template <class TYPE, typename std::enable_if<!std::is_constructible<TYPE>::value,void*>::type=nullptr>
65  inline
67  const std::string& location ) {
68  DataObject *obj = nullptr;
69  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
70  StatusCode sc = service->retrieveObject(location, obj);
71  if (sc.isSuccess()) {
72  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>(obj);
73  return tobj;
74  } else {
75  return nullptr;
76  }
77  }
78  // ========================================================================
84  template <class TYPE, typename std::enable_if<std::is_constructible<TYPE>::value,void*>::type=nullptr>
85  inline
87  const std::string& location ) {
88  DataObject *obj = nullptr;
89  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
90  StatusCode sc = service->retrieveObject(location, obj);
91  if (sc.isSuccess()) {
92  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>(obj);
93  if (!tobj) {
94  // try with AnyDataWrapper
95  AnyDataWrapper<TYPE>* tobj2 = dynamic_cast<AnyDataWrapper<TYPE>*>(obj);
96  if (tobj2) {
97  tobj = &(tobj2->getData());
98  }
99  }
100  return tobj;
101  } else {
102  return nullptr;
103  }
104  }
105  // ========================================================================
114  template <class TYPE>
115  struct GetData
116  {
117  public:
118  // ======================================================================
119  typedef TYPE Type ;
122  // ======================================================================
123  public:
124  // ======================================================================
132  template <class COMMON>
133  inline return_type operator()
134  ( const COMMON& common ,
135  IDataProviderSvc* service ,
136  const std::string& location ,
137  const bool checkData = true) const
138  {
139  // use Data Provider Service
140  return_type obj = getFromTS<Type>(service, location);
141  if (checkData) { // check the data
142  common.Assert(obj, "get():: No valid data at '" + location + "'");
143  }
144  // debug printout
145  if ( common.msgLevel ( MSG::DEBUG ) ) {
146  common.debug() << "The object of type '"
147  << System::typeinfoName(typeid(obj))
148  << "' "
149  << (obj ? "has been" : "could not be")
150  << " retrieved from TS at address '"
151  << location << "'" << endmsg ;
152  }
153  // return located data
154  return obj ;
155  // ======================================================================
156  }
157  };
158  // ========================================================================
160  template <class TYPE>
161  struct GetData<Gaudi::Range_<std::vector<const TYPE*> > >
162  {
163  public:
164  // ======================================================================
168  // ======================================================================
169  public:
170  // ======================================================================
178  template <class COMMON>
179  inline return_type operator()
180  ( const COMMON& common ,
181  IDataProviderSvc* service ,
182  const std::string& location ,
183  const bool checkData = true) const
184  {
187  DataObject* object = this -> getData ( service , location ) ;
188  if ( object )
189  {
191  typedef typename TYPE::Selection Selection_;
192  const Selection_* sel = dynamic_cast<Selection_*> ( object ) ;
193  if ( sel )
194  {
195  if ( common.msgLevel ( MSG::DEBUG ) )
196  { common.debug() << "The object of type '"
197  << System::typeinfoName(typeid(*object))
198  << "' has been retrieved from TS at address '"
199  << location << "'" << endmsg ; }
200  return make_range ( sel ) ;
201  }
203  typedef typename TYPE::Container Container_ ;
204  const Container_* cnt = dynamic_cast<Container_*> ( object ) ;
205  if ( cnt )
206  {
207  if ( common.msgLevel ( MSG::DEBUG ) )
208  { common.debug() << "The object of type '"
209  << System::typeinfoName(typeid(*object))
210  << "' has been retrieved from TS at address '"
211  << location << "'" << endmsg ; }
212  return make_range ( cnt ) ;
213  }
214  // no valid data
215  if (checkData)
216  common.Assert ( false , "get():: No valid data at '" + location + "'" ) ;
217  }
218  // no valid data
219  if (checkData)
220  common.Assert ( false , "get():: No data at '" + location + "'" ) ;
221  // the fictive return
222  return return_type () ;
223  }
224  // ======================================================================
225  public:
226  // ======================================================================
227  // create the range from the container
228  return_type make_range ( const typename TYPE::Container* cnt ) const
229  { return 0 == cnt ? return_type() : make_range ( cnt->begin() , cnt->end() ) ; }
230  // create the range from the selection
231  return_type make_range ( const typename TYPE::Selection* cnt ) const
232  { return 0 == cnt ? return_type() : return_type ( cnt->begin() , cnt->end() ) ; }
233  // ======================================================================
240  const std::string& location ) const
241  {
243  SmartDataObjectPtr getter
245  service , nullptr , location ) ;
246  return getter.accessData () ;
247  }
248  // ======================================================================
249  private:
250  // ======================================================================
251  template <class ITERATOR>
252  return_type make_range
253  ( ITERATOR first ,
254  ITERATOR last ) const
255  {
256  auto _begin = reinterpret_cast<typename return_type::const_iterator*>(&first);
257  auto _end = reinterpret_cast<typename return_type::const_iterator*>(&last);
258  return return_type(*_begin, *_end);
259  }
260  // ======================================================================
261  } ;
262  // ========================================================================
264  template <class TYPE>
265  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
266  {
267  public:
268  // ======================================================================
272  // ======================================================================
273  public:
274  // ======================================================================
282  template <class COMMON>
283  inline return_type operator()
284  ( const COMMON& common ,
285  IDataProviderSvc* service ,
286  const std::string& location ,
287  const bool checkData = true) const
288  {
289  return return_type ( m_range( common , service , location, checkData ) , location ) ;
290  }
291  // ======================================================================
292  public:
293  // ======================================================================
294  // create the range from the container
295  return_type make_range ( const typename TYPE::Container* cnt ) const
296  {
297  if ( !cnt ) { return return_type() ; }
298  static const std::string s_empty = "" ;
299  const IRegistry* reg = cnt->registry() ;
300  return return_type
301  ( m_range.make_range ( cnt ) , reg ? reg->identifier() : s_empty ) ;
302  }
303  // create the range from the selection
304  return_type make_range ( const typename TYPE::Selection* cnt ) const
305  {
306  if ( !cnt ) { return return_type() ; }
307  static const std::string s_empty = "" ;
308  const IRegistry* reg = cnt->registry() ;
309  return return_type
310  ( m_range.make_range ( cnt ) , reg ? reg->identifier() : s_empty ) ;
311  }
312  // ======================================================================
319  const std::string& location ) const
320  { return m_range.getData ( service , location ) ; }
321  // ======================================================================
322  private:
326  // ======================================================================
327  } ;
328  // ========================================================================
330  template <class TYPE>
331  struct GetData<const TYPE> : public GetData<TYPE> {} ;
332  // ========================================================================
334  template <class TYPE>
335  struct GetData<TYPE*> : public GetData<TYPE> {} ;
336  // ========================================================================
338  template <class TYPE>
339  struct GetData<TYPE&> : public GetData<TYPE> {} ;
340  // ========================================================================
349  template <class TYPE>
350  struct CheckData
351  {
352  public:
353  // ======================================================================
360  inline bool operator()
361  ( IDataProviderSvc* service ,
362  const std::string& location ) const
363  {
365  return getFromTS<TYPE>(service, location);
366  }
367  // ======================================================================
368  };
369  // ========================================================================
371  template <class TYPE>
372  struct CheckData<Gaudi::Range_<std::vector<const TYPE*> > >
373  {
374  public:
375  // ======================================================================
382  inline bool operator()
383  ( IDataProviderSvc* service ,
384  const std::string& location ) const
385  {
386  DataObject* object = this->getData( service , location ) ;
387  if ( !object ) { return false ; }
388  return
389  dynamic_cast<typename TYPE::Selection*> ( object ) ||
390  dynamic_cast<typename TYPE::Container*> ( object ) ;
391  }
392  // ======================================================================
393  protected:
394  // ======================================================================
401  const std::string& location ) const
402  {
404  SmartDataObjectPtr getter
406  service , nullptr , location ) ;
407  return getter.accessData () ;
408  }
409  // ======================================================================
410  } ;
411  // ========================================================================
413  template <class TYPE>
414  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
415  : public CheckData<Gaudi::Range_<std::vector<const TYPE*> > > {} ;
416  // ========================================================================
418  template <class TYPE>
419  struct CheckData<TYPE*> : public CheckData<TYPE> {} ;
420  // ========================================================================
422  template <class TYPE>
423  struct CheckData<TYPE&> : public CheckData<TYPE> {} ;
424  // ========================================================================
426  template <class TYPE>
427  struct CheckData<const TYPE> : public CheckData<TYPE> {} ;
428  // ========================================================================
437  template <class TYPE, class TYPE2>
439  {
440  private:
441  // ======================================================================
443  typedef GetData<TYPE> Getter ; // the actual data getter
444  // ======================================================================
445  public:
446  // ======================================================================
447  typedef typename Getter::Type Type ;
449  typedef typename Getter::return_type return_type ;
450  // ======================================================================
451  public:
452  // ======================================================================
459  template <class COMMON>
460  inline return_type operator()
461  ( const COMMON& common ,
462  IDataProviderSvc* service ,
463  const std::string& location ,
464  const std::string& location2 ) const
465  {
466  SmartDataPtr<TYPE> obj ( service , location ) ;
467  if ( !obj )
468  {
469  TYPE2* o = new TYPE2() ;
470  common.put ( service , o , location2 ) ;
471  if ( common.msgLevel ( MSG::DEBUG ) )
472  { common.debug() << "The object of type '"
473  << System::typeinfoName(typeid(*o))
474  << "' has been created from TS at address '"
475  << location2 << "'" << endmsg ; }
476  return o ;
477  }
478  auto ret = obj.ptr() ;
480  common.Assert ( !(!ret) , "get():: No valid data at '" + location + "'" ) ;
481  if ( common.msgLevel ( MSG::DEBUG ) )
482  { common.debug() << "The object of type '"
483  << System::typeinfoName(typeid(*ret))
484  << "' has been retrieved from TS at address '"
485  << location << "'" << endmsg ; }
486  // return *VALID* data
487  return ret ;
488  // ====================================================================
489  }
490  };
491  // ========================================================================
492  template <class TYPE, class TYPE2>
493  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*> >,TYPE2>
494  {
495  private:
496  // ======================================================================
499  typedef GetData<Range> Getter ; // the actual data getter
501  typedef CheckData<Range> Checker ; // the actual data checker
502  // ======================================================================
503  public:
504  // ======================================================================
505  typedef typename Getter::Type Type ;
507  typedef typename Getter::return_type return_type ;
508  // ======================================================================
509  public:
510  // ======================================================================
517  template <class COMMON>
518  inline return_type operator()
519  ( const COMMON& common ,
520  IDataProviderSvc* service ,
521  const std::string& location ,
522  const std::string& location2 ) const
523  {
524  DataObject* obj = m_getter.getData ( service , location ) ;
525  if ( !obj )
526  {
527  common.put ( service , new TYPE2() , location2 ) ;
528  if ( common.msgLevel ( MSG::DEBUG ) )
529  { common.debug() << "The object of type '"
530  << System::typeinfoName(typeid(TYPE2))
531  << "' has been created from TS at address '"
532  << location2 << "'" << endmsg ; }
533  }
534  return m_getter ( common , service , location ) ;
535  // ====================================================================
536  }
537  // ======================================================================
538  private:
539  // ======================================================================
541  Getter m_getter ; // the actual data getter
542  // ======================================================================
543  };
544  // ========================================================================
545  template <class TYPE, class TYPE2>
546  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*> >,TYPE2>
547  {
548  private:
549  // ======================================================================
554  typedef GetData<Range> Getter ; // the actual data getter
555  // ======================================================================
556  public:
557  // ======================================================================
558  typedef typename Getter::Type Type ;
560  typedef typename Getter::return_type return_type ;
561  // ======================================================================
562  public:
563  // ======================================================================
570  template <class COMMON>
571  inline return_type operator()
572  ( const COMMON& common ,
573  IDataProviderSvc* service ,
574  const std::string& location ,
575  const std::string& location2 ) const
576  {
577  return return_type ( m_range ( common ,
578  service ,
579  location ,
580  location2 ) , location ) ;
581  }
582  // ======================================================================
583  private:
584  // ======================================================================
586  Helper m_range ; // the actual data getter
587  // ======================================================================
588  };
589  // ========================================================================
590  template <class TYPE, class TYPE2>
591  struct GetOrCreateData<TYPE,TYPE2*>
592  : public GetOrCreateData<TYPE,TYPE2> {} ;
593  template <class TYPE, class TYPE2>
594  struct GetOrCreateData<TYPE*,TYPE2>
595  : public GetOrCreateData<TYPE,TYPE2> {} ;
596  template <class TYPE, class TYPE2>
597  struct GetOrCreateData<TYPE*,TYPE2*>
598  : public GetOrCreateData<TYPE,TYPE2> {} ;
599  // ========================================================================
600  template <class TYPE, class TYPE2>
601  struct GetOrCreateData< TYPE,const TYPE2>
602  : public GetOrCreateData<TYPE,TYPE2> {} ;
603  template <class TYPE, class TYPE2>
604  struct GetOrCreateData<const TYPE, TYPE2>
605  : public GetOrCreateData<TYPE,TYPE2> {} ;
606  template <class TYPE, class TYPE2>
607  struct GetOrCreateData<const TYPE,const TYPE2>
608  : public GetOrCreateData<TYPE,TYPE2> {} ;
609  // ========================================================================
610  template <class TYPE, class TYPE2>
611  struct GetOrCreateData<TYPE,TYPE2&>
612  : public GetOrCreateData<TYPE,TYPE2> {} ;
613  template <class TYPE, class TYPE2>
614  struct GetOrCreateData<TYPE&,TYPE2>
615  : public GetOrCreateData<TYPE,TYPE2> {} ;
616  template <class TYPE, class TYPE2>
617  struct GetOrCreateData<TYPE&,TYPE2&>
618  : public GetOrCreateData<TYPE,TYPE2> {} ;
619  // ========================================================================
620  } // end of namespace Gaudi::Utils
621  // ==========================================================================
622 } // end of namespace Gaudi
623 // ============================================================================
624 // The END
625 // ============================================================================
626 #endif // GAUDIUTILS_GETDATA_H
627 // ============================================================================
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:58
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:37
Gaudi::NamedRange_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:270
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:299
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:74
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:239
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:231
sel
Definition: IOTest.py:84
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:41
Data provider interface definition.
const T & getData() const
STL class.
TYPE * ptr()
Automatic conversion to data type.
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:400
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:304
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:228
virtual const id_type & identifier() const =0
Full identifier (or key)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:350
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
DataObject * accessData()
Static Object retrieval method: must call specific function.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE> ...
Definition: GetData.h:438
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
_GetType< Type >::return_type return_type
the actual return type
Definition: GetData.h:121
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:295
Getter::return_type return_type
the actual return type
Definition: GetData.h:449
A small class used to access easily (and efficiently) data items residing in data stores...
_GetType< TYPE >::return_type getFromTS(IDataProviderSvc *service, const std::string &location)
Helper function to provide the minimal lookup and cast functionality of SmartDataPtr used in the help...
Definition: GetData.h:66
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:318
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor ...
Definition: GetData.h:325
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:166
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:102
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:46
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:80
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
GetData< TYPE > Getter
the actual data getter
Definition: GetData.h:443
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.