The Gaudi Framework  v30r3 (a5ef0a68)
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 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/StatusCode.h"
14 // ============================================================================
15 // GaudiUtils
16 // ============================================================================
18 #include "GaudiKernel/NamedRange.h"
19 #include "GaudiKernel/Range.h"
20 // ============================================================================
21 // Forward declaration
22 // ============================================================================
23 template <class BASE>
24 class GaudiCommon; // GaudiAlg
25 // ============================================================================
26 namespace Gaudi
27 {
28  namespace Utils
29  {
30  // ========================================================================
37  template <class TYPE>
38  struct _GetType {
39  typedef TYPE* return_type;
40  };
41  // ========================================================================
43  template <class TYPE>
44  struct _GetType<TYPE*> {
45  typedef TYPE* return_type;
46  };
47  // ========================================================================
49  template <class TYPE>
50  struct _GetType<TYPE&> {
51  typedef TYPE* return_type;
52  };
53  // ========================================================================
55  template <class CONTAINER>
56  struct _GetType<Gaudi::Range_<CONTAINER>> {
58  };
59  // ========================================================================
61  template <class CONTAINER>
62  struct _GetType<Gaudi::NamedRange_<CONTAINER>> {
64  };
65  // ========================================================================
70  template <class TYPE, std::enable_if_t<!std::is_constructible<TYPE>::value, void*> = nullptr>
72  {
73  DataObject* obj = nullptr;
74  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
75  StatusCode sc = service->retrieveObject( location, obj );
76  if ( sc.isSuccess() ) {
77  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
78  return tobj;
79  } else {
80  return nullptr;
81  }
82  }
83  // ========================================================================
89  template <class TYPE, std::enable_if_t<std::is_constructible<TYPE>::value, void*> = nullptr>
91  {
92  DataObject* obj = nullptr;
93  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
94  StatusCode sc = service->retrieveObject( location, obj );
95  if ( sc.isSuccess() ) {
96  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
97  if ( !tobj ) {
98  // try with AnyDataWrapper
99  AnyDataWrapper<TYPE>* tobj2 = dynamic_cast<AnyDataWrapper<TYPE>*>( obj );
100  if ( tobj2 ) {
101  tobj = &( tobj2->getData() );
102  }
103  }
104  return tobj;
105  } else {
106  return nullptr;
107  }
108  }
109  // ========================================================================
118  template <class TYPE>
119  struct GetData {
120  public:
121  // ======================================================================
122  typedef TYPE Type;
125  // ======================================================================
126  public:
127  // ======================================================================
135  template <class COMMON>
136  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, 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 '" << System::typeinfoName( typeid( obj ) ) << "' "
147  << ( obj ? "has been" : "could not be" ) << " retrieved from TS at address '" << location
148  << "'" << endmsg;
149  }
150  // return located data
151  return obj;
152  // ======================================================================
153  }
154  };
155  // ========================================================================
157  template <class TYPE>
158  struct GetData<Gaudi::Range_<std::vector<const TYPE*>>> {
159  public:
160  // ======================================================================
164  // ======================================================================
165  public:
166  // ======================================================================
174  template <class COMMON>
175  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
176  const bool checkData = true ) const
177  {
180  DataObject* object = this->getData( service, location );
181  if ( object ) {
183  typedef typename TYPE::Selection Selection_;
184  const Selection_* sel = dynamic_cast<Selection_*>( object );
185  if ( sel ) {
186  if ( common.msgLevel( MSG::DEBUG ) ) {
187  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
188  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
189  }
190  return make_range( sel );
191  }
193  typedef typename TYPE::Container Container_;
194  const Container_* cnt = dynamic_cast<Container_*>( object );
195  if ( cnt ) {
196  if ( common.msgLevel( MSG::DEBUG ) ) {
197  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
198  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
199  }
200  return make_range( cnt );
201  }
202  // no valid data
203  if ( checkData ) common.Assert( false, "get():: No valid data at '" + location + "'" );
204  }
205  // no valid data
206  if ( checkData ) common.Assert( false, "get():: No data at '" + location + "'" );
207  // the fictive return
208  return return_type();
209  }
210  // ======================================================================
211  public:
212  // ======================================================================
213  // create the range from the container
214  return_type make_range( const typename TYPE::Container* cnt ) const
215  {
216  return 0 == cnt ? return_type() : make_range( cnt->begin(), cnt->end() );
217  }
218  // create the range from the selection
219  return_type make_range( const typename TYPE::Selection* cnt ) const
220  {
221  return 0 == cnt ? return_type() : return_type( cnt->begin(), cnt->end() );
222  }
223  // ======================================================================
229  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const
230  {
232  SmartDataObjectPtr getter( SmartDataObjectPtr::ObjectLoader::access(), service, nullptr, location );
233  return getter.accessData();
234  }
235  // ======================================================================
236  private:
237  // ======================================================================
238  template <class ITERATOR>
239  return_type make_range( ITERATOR first, ITERATOR last ) const
240  {
241  auto _begin = reinterpret_cast<typename return_type::const_iterator*>( &first );
242  auto _end = reinterpret_cast<typename return_type::const_iterator*>( &last );
243  return return_type( *_begin, *_end );
244  }
245  // ======================================================================
246  };
247  // ========================================================================
249  template <class TYPE>
250  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*>>> {
251  public:
252  // ======================================================================
256  // ======================================================================
257  public:
258  // ======================================================================
266  template <class COMMON>
267  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
268  const bool checkData = true ) const
269  {
270  return return_type( m_range( common, service, location, checkData ), location );
271  }
272  // ======================================================================
273  public:
274  // ======================================================================
275  // create the range from the container
276  return_type make_range( const typename TYPE::Container* cnt ) const
277  {
278  if ( !cnt ) {
279  return return_type();
280  }
281  static const std::string s_empty = "";
282  const IRegistry* reg = cnt->registry();
283  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
284  }
285  // create the range from the selection
286  return_type make_range( const typename TYPE::Selection* cnt ) const
287  {
288  if ( !cnt ) {
289  return return_type();
290  }
291  static const std::string s_empty = "";
292  const IRegistry* reg = cnt->registry();
293  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
294  }
295  // ======================================================================
301  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const
302  {
303  return m_range.getData( service, location );
304  }
305  // ======================================================================
306  private:
310  // ======================================================================
311  };
312  // ========================================================================
314  template <class TYPE>
315  struct GetData<const TYPE> : public GetData<TYPE> {
316  };
317  // ========================================================================
319  template <class TYPE>
320  struct GetData<TYPE*> : public GetData<TYPE> {
321  };
322  // ========================================================================
324  template <class TYPE>
325  struct GetData<TYPE&> : public GetData<TYPE> {
326  };
327  // ========================================================================
336  template <class TYPE>
337  struct CheckData {
338  public:
339  // ======================================================================
346  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const
347  {
349  return getFromTS<TYPE>( service, location );
350  }
351  // ======================================================================
352  };
353  // ========================================================================
355  template <class TYPE>
356  struct CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {
357  public:
358  // ======================================================================
365  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const
366  {
367  DataObject* object = this->getData( service, location );
368  if ( !object ) {
369  return false;
370  }
371  return dynamic_cast<typename TYPE::Selection*>( object ) || dynamic_cast<typename TYPE::Container*>( object );
372  }
373  // ======================================================================
374  protected:
375  // ======================================================================
381  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const
382  {
384  SmartDataObjectPtr getter( SmartDataObjectPtr::ObjectLoader::access(), service, nullptr, location );
385  return getter.accessData();
386  }
387  // ======================================================================
388  };
389  // ========================================================================
391  template <class TYPE>
392  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*>>>
393  : public CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {
394  };
395  // ========================================================================
397  template <class TYPE>
398  struct CheckData<TYPE*> : public CheckData<TYPE> {
399  };
400  // ========================================================================
402  template <class TYPE>
403  struct CheckData<TYPE&> : public CheckData<TYPE> {
404  };
405  // ========================================================================
407  template <class TYPE>
408  struct CheckData<const TYPE> : public CheckData<TYPE> {
409  };
410  // ========================================================================
419  template <class TYPE, class TYPE2>
421  private:
422  // ======================================================================
424  typedef GetData<TYPE> Getter; // the actual data getter
425  // ======================================================================
426  public:
427  // ======================================================================
428  typedef typename Getter::Type Type;
431  // ======================================================================
432  public:
433  // ======================================================================
440  template <class COMMON>
441  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
442  const std::string& location2 ) const
443  {
444  SmartDataPtr<TYPE> obj( service, location );
445  if ( !obj ) {
446  auto o = std::make_unique<TYPE2>();
447  auto r = o.get();
448  common.put( service, std::move( o ), location2 );
449  if ( common.msgLevel( MSG::DEBUG ) ) {
450  common.debug() << "The object of type '" << System::typeinfoName( typeid( *r ) )
451  << "' has been created from TS at address '" << location2 << "'" << endmsg;
452  }
453  return r;
454  }
455  auto ret = obj.ptr();
457  common.Assert( !( !ret ), "get():: No valid data at '" + location + "'" );
458  if ( common.msgLevel( MSG::DEBUG ) ) {
459  common.debug() << "The object of type '" << System::typeinfoName( typeid( *ret ) )
460  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
461  }
462  // return *VALID* data
463  return ret;
464  // ====================================================================
465  }
466  };
467  // ========================================================================
468  template <class TYPE, class TYPE2>
469  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*>>, TYPE2> {
470  private:
471  // ======================================================================
474  typedef GetData<Range> Getter; // the actual data getter
476  typedef CheckData<Range> Checker; // the actual data checker
477  // ======================================================================
478  public:
479  // ======================================================================
480  typedef typename Getter::Type Type;
483  // ======================================================================
484  public:
485  // ======================================================================
492  template <class COMMON>
493  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
494  const std::string& location2 ) const
495  {
496  DataObject* obj = m_getter.getData( service, location );
497  if ( !obj ) {
498  common.put( service, std::make_unique<TYPE2>(), location2 );
499  if ( common.msgLevel( MSG::DEBUG ) ) {
500  common.debug() << "The object of type '" << System::typeinfoName( typeid( TYPE2 ) )
501  << "' has been created from TS at address '" << location2 << "'" << endmsg;
502  }
503  }
504  return m_getter( common, service, location );
505  // ====================================================================
506  }
507  // ======================================================================
508  private:
509  // ======================================================================
511  Getter m_getter; // the actual data getter
512  // ======================================================================
513  };
514  // ========================================================================
515  template <class TYPE, class TYPE2>
516  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*>>, TYPE2> {
517  private:
518  // ======================================================================
523  typedef GetData<Range> Getter; // the actual data getter
524  // ======================================================================
525  public:
526  // ======================================================================
527  typedef typename Getter::Type Type;
530  // ======================================================================
531  public:
532  // ======================================================================
539  template <class COMMON>
540  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
541  const std::string& location2 ) const
542  {
543  return return_type( m_range( common, service, location, location2 ), location );
544  }
545  // ======================================================================
546  private:
547  // ======================================================================
549  Helper m_range; // the actual data getter
550  // ======================================================================
551  };
552  // ========================================================================
553  template <class TYPE, class TYPE2>
554  struct GetOrCreateData<TYPE, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {
555  };
556  template <class TYPE, class TYPE2>
557  struct GetOrCreateData<TYPE*, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {
558  };
559  template <class TYPE, class TYPE2>
560  struct GetOrCreateData<TYPE*, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {
561  };
562  // ========================================================================
563  template <class TYPE, class TYPE2>
564  struct GetOrCreateData<TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {
565  };
566  template <class TYPE, class TYPE2>
567  struct GetOrCreateData<const TYPE, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {
568  };
569  template <class TYPE, class TYPE2>
570  struct GetOrCreateData<const TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {
571  };
572  // ========================================================================
573  template <class TYPE, class TYPE2>
574  struct GetOrCreateData<TYPE, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {
575  };
576  template <class TYPE, class TYPE2>
577  struct GetOrCreateData<TYPE&, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {
578  };
579  template <class TYPE, class TYPE2>
580  struct GetOrCreateData<TYPE&, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {
581  };
582  // ========================================================================
583  } // end of namespace Gaudi::Utils
584  // ==========================================================================
585 } // end of namespace Gaudi
586 // ============================================================================
587 // The END
588 // ============================================================================
589 #endif // GAUDIUTILS_GETDATA_H
590 // ============================================================================
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:365
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:63
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:38
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:493
Gaudi::NamedRange_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:254
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:332
bool isSuccess() const
Definition: StatusCode.h:287
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:229
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:540
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:219
sel
Definition: IOTest.py:95
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:54
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:346
Data provider interface definition.
const T & getData() const
return_type make_range(ITERATOR first, ITERATOR last) const
Definition: GetData.h:239
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:381
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:286
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:175
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:441
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:214
Range make_range(const DataObject *obj)
virtual const id_type & identifier() const =0
Full identifier (or key)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:337
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:267
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
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:420
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:124
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:276
T move(T...args)
Getter::return_type return_type
the actual return type
Definition: GetData.h:430
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:71
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:301
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor ...
Definition: GetData.h:309
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:162
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:88
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:47
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:93
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:136
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:209
GetData< TYPE > Getter
the actual data getter
Definition: GetData.h:424