All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 // ============================================================================
10 #include "GaudiKernel/IRegistry.h"
11 // ============================================================================
12 // GaudiUtils
13 // ============================================================================
14 #include "GaudiKernel/Range.h"
15 #include "GaudiKernel/NamedRange.h"
16 // ============================================================================
17 // Forward declaration
18 // ============================================================================
19 template <class BASE> class GaudiCommon ; // GaudiAlg
20 // ============================================================================
21 namespace Gaudi
22 {
23  namespace Utils
24  {
25  // ========================================================================
32  template <class TYPE>
33  struct _GetType
34  { typedef TYPE* return_type ; };
35  // ========================================================================
37  template <class TYPE>
38  struct _GetType<TYPE*>
39  { typedef TYPE* return_type ; };
40  // ========================================================================
42  template <class TYPE>
43  struct _GetType<TYPE&>
44  { typedef TYPE* return_type ; };
45  // ========================================================================
47  template <class CONTAINER>
48  struct _GetType<Gaudi::Range_<CONTAINER> >
50  // ========================================================================
52  template <class CONTAINER>
53  struct _GetType<Gaudi::NamedRange_<CONTAINER> >
55  // ========================================================================
59  template <class TYPE>
60  inline
62  const std::string& location ) {
63  DataObject *obj = nullptr;
64  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
65  return service->retrieveObject(location, obj).isSuccess() ?
66  dynamic_cast<typename _GetType<TYPE>::return_type>(obj) :
67  nullptr;
68  }
69  // ========================================================================
78  template <class TYPE>
79  struct GetData
80  {
81  public:
82  // ======================================================================
83  typedef TYPE Type ;
86  // ======================================================================
87  public:
88  // ======================================================================
96  template <class COMMON>
97  inline return_type operator()
98  ( const COMMON& common ,
99  IDataProviderSvc* service ,
100  const std::string& location ,
101  const bool checkData = true) const
102  {
103  // use Data Provider Service
104  return_type obj = getFromTS<Type>(service, location);
105  if (checkData) { // check the data
106  common.Assert(obj, "get():: No valid data at '" + location + "'");
107  }
108  // debug printout
109  if ( common.msgLevel ( MSG::DEBUG ) ) {
110  common.debug() << "The object of type '"
111  << System::typeinfoName(typeid(obj))
112  << "' "
113  << (obj ? "has been" : "could not be")
114  << " retrieved from TS at address '"
115  << location << "'" << endmsg ;
116  }
117  // return located data
118  return obj ;
119  // ======================================================================
120  }
121  };
122  // ========================================================================
124  template <class TYPE>
125  struct GetData<Gaudi::Range_<std::vector<const TYPE*> > >
126  {
127  public:
128  // ======================================================================
132  // ======================================================================
133  public:
134  // ======================================================================
142  template <class COMMON>
143  inline return_type operator()
144  ( const COMMON& common ,
145  IDataProviderSvc* service ,
146  const std::string& location ,
147  const bool checkData = true) const
148  {
151  DataObject* object = this -> getData ( service , location ) ;
152  if ( object )
153  {
155  typedef typename TYPE::Selection Selection_;
156  const Selection_* sel = dynamic_cast<Selection_*> ( object ) ;
157  if ( sel )
158  {
159  if ( common.msgLevel ( MSG::DEBUG ) )
160  { common.debug() << "The object of type '"
161  << System::typeinfoName(typeid(*object))
162  << "' has been retrieved from TS at address '"
163  << location << "'" << endmsg ; }
164  return make_range ( sel ) ;
165  }
167  typedef typename TYPE::Container Container_ ;
168  const Container_* cnt = dynamic_cast<Container_*> ( object ) ;
169  if ( cnt )
170  {
171  if ( common.msgLevel ( MSG::DEBUG ) )
172  { common.debug() << "The object of type '"
173  << System::typeinfoName(typeid(*object))
174  << "' has been retrieved from TS at address '"
175  << location << "'" << endmsg ; }
176  return make_range ( cnt ) ;
177  }
178  // no valid data
179  if (checkData)
180  common.Assert ( false , "get():: No valid data at '" + location + "'" ) ;
181  }
182  // no valid data
183  if (checkData)
184  common.Assert ( false , "get():: No data at '" + location + "'" ) ;
185  // the fictive return
186  return return_type () ;
187  }
188  // ======================================================================
189  public:
190  // ======================================================================
191  // create the range from the container
192  return_type make_range ( const typename TYPE::Container* cnt ) const
193  { return 0 == cnt ? return_type() : make_range ( cnt->begin() , cnt->end() ) ; }
194  // create the range from the selection
195  return_type make_range ( const typename TYPE::Selection* cnt ) const
196  { return 0 == cnt ? return_type() : return_type ( cnt->begin() , cnt->end() ) ; }
197  // ======================================================================
204  const std::string& location ) const
205  {
207  SmartDataObjectPtr getter
209  service , nullptr , location ) ;
210  return getter.accessData () ;
211  }
212  // ======================================================================
213  private:
214  // ======================================================================
215  template <class ITERATOR>
216  return_type make_range
217  ( ITERATOR first ,
218  ITERATOR last ) const
219  {
220  auto _begin = reinterpret_cast<typename return_type::const_iterator*>(&first);
221  auto _end = reinterpret_cast<typename return_type::const_iterator*>(&last);
222  return return_type(*_begin, *_end);
223  }
224  // ======================================================================
225  } ;
226  // ========================================================================
228  template <class TYPE>
229  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
230  {
231  public:
232  // ======================================================================
236  // ======================================================================
237  public:
238  // ======================================================================
246  template <class COMMON>
247  inline return_type operator()
248  ( const COMMON& common ,
249  IDataProviderSvc* service ,
250  const std::string& location ,
251  const bool checkData = true) const
252  {
253  return return_type ( m_range( common , service , location, checkData ) , location ) ;
254  }
255  // ======================================================================
256  public:
257  // ======================================================================
258  // create the range from the container
259  return_type make_range ( const typename TYPE::Container* cnt ) const
260  {
261  if ( !cnt ) { return return_type() ; }
262  static const std::string s_empty = "" ;
263  const IRegistry* reg = cnt->registry() ;
264  return return_type
265  ( m_range.make_range ( cnt ) , reg ? reg->identifier() : s_empty ) ;
266  }
267  // create the range from the selection
268  return_type make_range ( const typename TYPE::Selection* cnt ) const
269  {
270  if ( !cnt ) { return return_type() ; }
271  static const std::string s_empty = "" ;
272  const IRegistry* reg = cnt->registry() ;
273  return return_type
274  ( m_range.make_range ( cnt ) , reg ? reg->identifier() : s_empty ) ;
275  }
276  // ======================================================================
283  const std::string& location ) const
284  { return m_range.getData ( service , location ) ; }
285  // ======================================================================
286  private:
290  // ======================================================================
291  } ;
292  // ========================================================================
294  template <class TYPE>
295  struct GetData<const TYPE> : public GetData<TYPE> {} ;
296  // ========================================================================
298  template <class TYPE>
299  struct GetData<TYPE*> : public GetData<TYPE> {} ;
300  // ========================================================================
302  template <class TYPE>
303  struct GetData<TYPE&> : public GetData<TYPE> {} ;
304  // ========================================================================
313  template <class TYPE>
314  struct CheckData
315  {
316  public:
317  // ======================================================================
324  inline bool operator()
325  ( IDataProviderSvc* service ,
326  const std::string& location ) const
327  {
329  return getFromTS<TYPE>(service, location);
330  }
331  // ======================================================================
332  };
333  // ========================================================================
335  template <class TYPE>
336  struct CheckData<Gaudi::Range_<std::vector<const TYPE*> > >
337  {
338  public:
339  // ======================================================================
346  inline bool operator()
347  ( IDataProviderSvc* service ,
348  const std::string& location ) const
349  {
350  DataObject* object = this->getData( service , location ) ;
351  if ( !object ) { return false ; }
352  return
353  dynamic_cast<typename TYPE::Selection*> ( object ) ||
354  dynamic_cast<typename TYPE::Container*> ( object ) ;
355  }
356  // ======================================================================
357  protected:
358  // ======================================================================
365  const std::string& location ) const
366  {
368  SmartDataObjectPtr getter
370  service , nullptr , location ) ;
371  return getter.accessData () ;
372  }
373  // ======================================================================
374  } ;
375  // ========================================================================
377  template <class TYPE>
378  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
379  : public CheckData<Gaudi::Range_<std::vector<const TYPE*> > > {} ;
380  // ========================================================================
382  template <class TYPE>
383  struct CheckData<TYPE*> : public CheckData<TYPE> {} ;
384  // ========================================================================
386  template <class TYPE>
387  struct CheckData<TYPE&> : public CheckData<TYPE> {} ;
388  // ========================================================================
390  template <class TYPE>
391  struct CheckData<const TYPE> : public CheckData<TYPE> {} ;
392  // ========================================================================
401  template <class TYPE, class TYPE2>
403  {
404  private:
405  // ======================================================================
407  typedef GetData<TYPE> Getter ; // the actual data getter
408  // ======================================================================
409  public:
410  // ======================================================================
411  typedef typename Getter::Type Type ;
413  typedef typename Getter::return_type return_type ;
414  // ======================================================================
415  public:
416  // ======================================================================
423  template <class COMMON>
424  inline return_type operator()
425  ( const COMMON& common ,
426  IDataProviderSvc* service ,
427  const std::string& location ,
428  const std::string& location2 ) const
429  {
430  SmartDataPtr<TYPE> obj ( service , location ) ;
431  if ( !obj )
432  {
433  TYPE2* o = new TYPE2() ;
434  common.put ( service , o , location2 ) ;
435  if ( common.msgLevel ( MSG::DEBUG ) )
436  { common.debug() << "The object of type '"
437  << System::typeinfoName(typeid(*o))
438  << "' has been created from TS at address '"
439  << location2 << "'" << endmsg ; }
440  return o ;
441  }
442  auto ret = obj.ptr() ;
444  common.Assert ( !(!ret) , "get():: No valid data at '" + location + "'" ) ;
445  if ( common.msgLevel ( MSG::DEBUG ) )
446  { common.debug() << "The object of type '"
447  << System::typeinfoName(typeid(*ret))
448  << "' has been retrieved from TS at address '"
449  << location << "'" << endmsg ; }
450  // return *VALID* data
451  return ret ;
452  // ====================================================================
453  }
454  };
455  // ========================================================================
456  template <class TYPE, class TYPE2>
457  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*> >,TYPE2>
458  {
459  private:
460  // ======================================================================
463  typedef GetData<Range> Getter ; // the actual data getter
465  typedef CheckData<Range> Checker ; // the actual data checker
466  // ======================================================================
467  public:
468  // ======================================================================
469  typedef typename Getter::Type Type ;
471  typedef typename Getter::return_type return_type ;
472  // ======================================================================
473  public:
474  // ======================================================================
481  template <class COMMON>
482  inline return_type operator()
483  ( const COMMON& common ,
484  IDataProviderSvc* service ,
485  const std::string& location ,
486  const std::string& location2 ) const
487  {
488  DataObject* obj = m_getter.getData ( service , location ) ;
489  if ( !obj )
490  {
491  common.put ( service , new TYPE2() , location2 ) ;
492  if ( common.msgLevel ( MSG::DEBUG ) )
493  { common.debug() << "The object of type '"
494  << System::typeinfoName(typeid(TYPE2))
495  << "' has been created from TS at address '"
496  << location2 << "'" << endmsg ; }
497  }
498  return m_getter ( common , service , location ) ;
499  // ====================================================================
500  }
501  // ======================================================================
502  private:
503  // ======================================================================
505  Getter m_getter ; // the actual data getter
506  // ======================================================================
507  };
508  // ========================================================================
509  template <class TYPE, class TYPE2>
510  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*> >,TYPE2>
511  {
512  private:
513  // ======================================================================
518  typedef GetData<Range> Getter ; // the actual data getter
519  // ======================================================================
520  public:
521  // ======================================================================
522  typedef typename Getter::Type Type ;
524  typedef typename Getter::return_type return_type ;
525  // ======================================================================
526  public:
527  // ======================================================================
534  template <class COMMON>
535  inline return_type operator()
536  ( const COMMON& common ,
537  IDataProviderSvc* service ,
538  const std::string& location ,
539  const std::string& location2 ) const
540  {
541  return return_type ( m_range ( common ,
542  service ,
543  location ,
544  location2 ) , location ) ;
545  }
546  // ======================================================================
547  private:
548  // ======================================================================
550  Helper m_range ; // the actual data getter
551  // ======================================================================
552  };
553  // ========================================================================
554  template <class TYPE, class TYPE2>
555  struct GetOrCreateData<TYPE,TYPE2*>
556  : public GetOrCreateData<TYPE,TYPE2> {} ;
557  template <class TYPE, class TYPE2>
558  struct GetOrCreateData<TYPE*,TYPE2>
559  : public GetOrCreateData<TYPE,TYPE2> {} ;
560  template <class TYPE, class TYPE2>
561  struct GetOrCreateData<TYPE*,TYPE2*>
562  : public GetOrCreateData<TYPE,TYPE2> {} ;
563  // ========================================================================
564  template <class TYPE, class TYPE2>
565  struct GetOrCreateData< TYPE,const TYPE2>
566  : public GetOrCreateData<TYPE,TYPE2> {} ;
567  template <class TYPE, class TYPE2>
568  struct GetOrCreateData<const TYPE, TYPE2>
569  : public GetOrCreateData<TYPE,TYPE2> {} ;
570  template <class TYPE, class TYPE2>
571  struct GetOrCreateData<const TYPE,const TYPE2>
572  : public GetOrCreateData<TYPE,TYPE2> {} ;
573  // ========================================================================
574  template <class TYPE, class TYPE2>
575  struct GetOrCreateData<TYPE,TYPE2&>
576  : public GetOrCreateData<TYPE,TYPE2> {} ;
577  template <class TYPE, class TYPE2>
578  struct GetOrCreateData<TYPE&,TYPE2>
579  : public GetOrCreateData<TYPE,TYPE2> {} ;
580  template <class TYPE, class TYPE2>
581  struct GetOrCreateData<TYPE&,TYPE2&>
582  : public GetOrCreateData<TYPE,TYPE2> {} ;
583  // ========================================================================
584  } // end of namespace Gaudi::Utils
585  // ==========================================================================
586 } // end of namespace Gaudi
587 // ============================================================================
588 // The END
589 // ============================================================================
590 #endif // GAUDIUTILS_GETDATA_H
591 // ============================================================================
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:54
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:33
Gaudi::NamedRange_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:234
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:203
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:195
sel
Definition: IOTest.py:84
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:41
Data provider interface definition.
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:364
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:268
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:192
virtual const id_type & identifier() const =0
Full identifier (or key)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:314
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:402
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:85
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:259
Getter::return_type return_type
the actual return type
Definition: GetData.h:413
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:61
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:282
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor ...
Definition: GetData.h:289
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:130
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:407
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.