The Gaudi Framework  v33r1 (b1225454)
GetData.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIUTILS_GETDATA_H
12 #define GAUDIUTILS_GETDATA_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // GaudiKernel
17 // ============================================================================
20 #include "GaudiKernel/IRegistry.h"
21 #include "GaudiKernel/MsgStream.h"
23 #include "GaudiKernel/StatusCode.h"
24 // ============================================================================
25 // GaudiUtils
26 // ============================================================================
28 #include "GaudiKernel/NamedRange.h"
29 #include "GaudiKernel/Range.h"
30 // ============================================================================
31 // Forward declaration
32 // ============================================================================
33 template <class BASE>
34 class GaudiCommon; // GaudiAlg
35 // ============================================================================
36 namespace Gaudi {
37  namespace Utils {
38  // ========================================================================
45  template <class TYPE>
46  struct _GetType {
47  typedef TYPE* return_type;
48  };
49  // ========================================================================
51  template <class TYPE>
52  struct _GetType<TYPE*> {
53  typedef TYPE* return_type;
54  };
55  // ========================================================================
57  template <class TYPE>
58  struct _GetType<TYPE&> {
59  typedef TYPE* return_type;
60  };
61  // ========================================================================
63  template <class CONTAINER>
64  struct _GetType<Gaudi::Range_<CONTAINER>> {
66  };
67  // ========================================================================
69  template <class CONTAINER>
70  struct _GetType<Gaudi::NamedRange_<CONTAINER>> {
72  };
73  // ========================================================================
78  template <class TYPE, std::enable_if_t<!std::is_constructible_v<TYPE>, void*> = nullptr>
79  typename _GetType<TYPE>::return_type getFromTS( IDataProviderSvc* service, std::string_view location ) {
80  DataObject* obj = nullptr;
81  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
82  StatusCode sc = service->retrieveObject( location, obj );
83  if ( sc.isSuccess() ) {
84  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
85  return tobj;
86  } else {
87  return nullptr;
88  }
89  }
90  // ========================================================================
96  template <class TYPE, std::enable_if_t<std::is_constructible_v<TYPE>, void*> = nullptr>
97  typename _GetType<TYPE>::return_type getFromTS( IDataProviderSvc* service, std::string_view location ) {
98  DataObject* obj = nullptr;
99  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
100  StatusCode sc = service->retrieveObject( location, obj );
101  if ( sc.isSuccess() ) {
102  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
103  if ( !tobj ) {
104  // try with AnyDataWrapper
105  AnyDataWrapper<TYPE>* tobj2 = dynamic_cast<AnyDataWrapper<TYPE>*>( obj );
106  if ( tobj2 ) { tobj = &( tobj2->getData() ); }
107  }
108  return tobj;
109  } else {
110  return nullptr;
111  }
112  }
113  // ========================================================================
122  template <class TYPE>
123  struct GetData {
124  public:
125  // ======================================================================
126  typedef TYPE Type;
129  // ======================================================================
130  public:
131  // ======================================================================
139  template <class COMMON>
140  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string_view location,
141  const bool checkData = true ) const {
142  // use Data Provider Service
143  return_type obj = getFromTS<Type>( service, location );
144  if ( checkData ) { // check the data
145  common.Assert( obj, std::string{"get():: No valid data at '"}.append( location ).append( "'" ) );
146  }
147  // debug printout
148  if ( common.msgLevel( MSG::DEBUG ) ) {
149  common.debug() << "The object of type '" << System::typeinfoName( typeid( obj ) ) << "' "
150  << ( obj ? "has been" : "could not be" ) << " retrieved from TS at address '" << location
151  << "'" << 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  public:
163  // ======================================================================
167  // ======================================================================
168  public:
169  // ======================================================================
177  template <class COMMON>
178  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string_view location,
179  const bool checkData = true ) const {
182  DataObject* object = this->getData( service, std::string{location} );
183  if ( object ) {
185  typedef typename TYPE::Selection Selection_;
186  const Selection_* sel = dynamic_cast<Selection_*>( object );
187  if ( sel ) {
188  if ( common.msgLevel( MSG::DEBUG ) ) {
189  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
190  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
191  }
192  return make_range( sel );
193  }
195  typedef typename TYPE::Container Container_;
196  const Container_* cnt = dynamic_cast<Container_*>( object );
197  if ( cnt ) {
198  if ( common.msgLevel( MSG::DEBUG ) ) {
199  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
200  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
201  }
202  return make_range( cnt );
203  }
204  // no valid data
205  if ( checkData )
206  common.Assert( false, std::string{"get():: No valid data at '"}.append( location ).append( "'" ) );
207  }
208  // no valid data
209  if ( checkData ) common.Assert( false, std::string{"get():: No data at '"}.append( location ).append( "'" ) );
210  // the fictive return
211  return return_type();
212  }
213  // ======================================================================
214  public:
215  // ======================================================================
216  // create the range from the container
217  return_type make_range( const typename TYPE::Container* cnt ) const {
218  return 0 == cnt ? return_type() : make_range( cnt->begin(), cnt->end() );
219  }
220  // create the range from the selection
221  return_type make_range( const typename TYPE::Selection* cnt ) const {
222  return 0 == cnt ? return_type() : return_type( cnt->begin(), cnt->end() );
223  }
224  // ======================================================================
233  std::move( location ) );
234  return getter.accessData();
235  }
236  // ======================================================================
237  private:
238  // ======================================================================
239  template <class ITERATOR>
240  return_type make_range( ITERATOR first, ITERATOR last ) const {
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  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string_view location,
268  const bool checkData = true ) const {
269  return return_type( m_range( common, service, location, checkData ), std::string{location} );
270  }
271  // ======================================================================
272  public:
273  // ======================================================================
274  // create the range from the container
275  return_type make_range( const typename TYPE::Container* cnt ) const {
276  if ( !cnt ) { return return_type(); }
277  static const std::string s_empty = "";
278  const IRegistry* reg = cnt->registry();
279  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
280  }
281  // create the range from the selection
282  return_type make_range( const typename TYPE::Selection* cnt ) const {
283  if ( !cnt ) { return return_type(); }
284  static const std::string s_empty = "";
285  const IRegistry* reg = cnt->registry();
286  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
287  }
288  // ======================================================================
295  return m_range.getData( service, std::move( location ) );
296  }
297  // ======================================================================
298  private:
302  // ======================================================================
303  };
304  // ========================================================================
306  template <class TYPE>
307  struct GetData<const TYPE> : public GetData<TYPE> {};
308  // ========================================================================
310  template <class TYPE>
311  struct GetData<TYPE*> : public GetData<TYPE> {};
312  // ========================================================================
314  template <class TYPE>
315  struct GetData<TYPE&> : public GetData<TYPE> {};
316  // ========================================================================
325  template <class TYPE>
326  struct CheckData {
327  public:
328  // ======================================================================
335  bool operator()( IDataProviderSvc* service, std::string_view location ) const {
337  return getFromTS<TYPE>( service, location );
338  }
339  // ======================================================================
340  };
341  // ========================================================================
343  template <class TYPE>
344  struct CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {
345  public:
346  // ======================================================================
354  DataObject* object = this->getData( service, std::move( location ) );
355  return object && ( dynamic_cast<typename TYPE::Selection*>( object ) ||
356  dynamic_cast<typename TYPE::Container*>( object ) );
357  }
358  // ======================================================================
359  protected:
360  // ======================================================================
369  std::move( location ) );
370  return getter.accessData();
371  }
372  // ======================================================================
373  };
374  // ========================================================================
376  template <class TYPE>
377  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*>>>
378  : public CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {};
379  // ========================================================================
381  template <class TYPE>
382  struct CheckData<TYPE*> : public CheckData<TYPE> {};
383  // ========================================================================
385  template <class TYPE>
386  struct CheckData<TYPE&> : public CheckData<TYPE> {};
387  // ========================================================================
389  template <class TYPE>
390  struct CheckData<const TYPE> : public CheckData<TYPE> {};
391  // ========================================================================
400  template <class TYPE, class TYPE2>
402  private:
403  // ======================================================================
405  typedef GetData<TYPE> Getter; // the actual data getter
406  // ======================================================================
407  public:
408  // ======================================================================
409  typedef typename Getter::Type Type;
412  // ======================================================================
413  public:
414  // ======================================================================
421  template <class COMMON>
422  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string_view location,
423  std::string_view location2 ) const {
424  SmartDataPtr<TYPE> obj( service, std::string{location} );
425  if ( !obj ) {
426  auto o = std::make_unique<TYPE2>();
427  auto r = o.get();
428  common.put( service, std::move( o ), location2 );
429  if ( common.msgLevel( MSG::DEBUG ) ) {
430  common.debug() << "The object of type '" << System::typeinfoName( typeid( *r ) )
431  << "' has been created from TS at address '" << location2 << "'" << endmsg;
432  }
433  return r;
434  }
435  auto ret = obj.ptr();
437  common.Assert( !( !ret ), std::string{"get():: No valid data at '"}.append( location ).append( "\'" ) );
438  if ( common.msgLevel( MSG::DEBUG ) ) {
439  common.debug() << "The object of type '" << System::typeinfoName( typeid( *ret ) )
440  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
441  }
442  // return *VALID* data
443  return ret;
444  // ====================================================================
445  }
446  };
447  // ========================================================================
448  template <class TYPE, class TYPE2>
449  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*>>, TYPE2> {
450  private:
451  // ======================================================================
454  typedef GetData<Range> Getter; // the actual data getter
456  typedef CheckData<Range> Checker; // the actual data checker
457  // ======================================================================
458  public:
459  // ======================================================================
460  typedef typename Getter::Type Type;
463  // ======================================================================
464  public:
465  // ======================================================================
472  template <class COMMON>
473  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string_view location,
474  std::string_view location2 ) const {
475  DataObject* obj = m_getter.getData( service, std::string{location} );
476  if ( !obj ) {
477  common.put( service, std::make_unique<TYPE2>(), location2 );
478  if ( common.msgLevel( MSG::DEBUG ) ) {
479  common.debug() << "The object of type '" << System::typeinfoName( typeid( TYPE2 ) )
480  << "' has been created from TS at address '" << location2 << "'" << endmsg;
481  }
482  }
483  return m_getter( common, service, location );
484  // ====================================================================
485  }
486  // ======================================================================
487  private:
488  // ======================================================================
490  Getter m_getter; // the actual data getter
491  // ======================================================================
492  };
493  // ========================================================================
494  template <class TYPE, class TYPE2>
495  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*>>, TYPE2> {
496  private:
497  // ======================================================================
502  typedef GetData<Range> Getter; // the actual data getter
503  // ======================================================================
504  public:
505  // ======================================================================
506  typedef typename Getter::Type Type;
509  // ======================================================================
510  public:
511  // ======================================================================
518  template <class COMMON>
519  return_type operator()( const COMMON& common, IDataProviderSvc* service, std::string location,
520  std::string_view location2 ) const {
521  auto range = m_range( common, service, location, location2 );
522  return return_type( std::move( range ), std::move( location ) );
523  }
524  // ======================================================================
525  private:
526  // ======================================================================
528  Helper m_range; // the actual data getter
529  // ======================================================================
530  };
531  // ========================================================================
532  template <class TYPE, class TYPE2>
533  struct GetOrCreateData<TYPE, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
534  template <class TYPE, class TYPE2>
535  struct GetOrCreateData<TYPE*, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
536  template <class TYPE, class TYPE2>
537  struct GetOrCreateData<TYPE*, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
538  // ========================================================================
539  template <class TYPE, class TYPE2>
540  struct GetOrCreateData<TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
541  template <class TYPE, class TYPE2>
542  struct GetOrCreateData<const TYPE, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
543  template <class TYPE, class TYPE2>
544  struct GetOrCreateData<const TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
545  // ========================================================================
546  template <class TYPE, class TYPE2>
547  struct GetOrCreateData<TYPE, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
548  template <class TYPE, class TYPE2>
549  struct GetOrCreateData<TYPE&, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
550  template <class TYPE, class TYPE2>
551  struct GetOrCreateData<TYPE&, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
552  // ========================================================================
553  } // namespace Utils
554  // ==========================================================================
555 } // end of namespace Gaudi
556 // ============================================================================
557 // The END
558 // ============================================================================
559 #endif // GAUDIUTILS_GETDATA_H
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:71
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:46
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:308
DataObject * getData(IDataProviderSvc *service, std::string location) const
get the data form transient store
Definition: GetData.h:294
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:57
Data provider interface definition.
const T & getData() const
STL class.
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string location, std::string_view location2) const
the only one essential method
Definition: GetData.h:519
bool operator()(IDataProviderSvc *service, std::string location) const
the only one essential method
Definition: GetData.h:353
_GetType< TYPE >::return_type getFromTS(IDataProviderSvc *service, std::string_view location)
Helper function to provide the minimal lookup and cast functionality of SmartDataPtr used in the help...
Definition: GetData.h:79
return_type make_range(ITERATOR first, ITERATOR last) const
Definition: GetData.h:240
Range make_range(const DataObject *obj)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:326
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range from arbitrary container
Definition: NamedRange.h:126
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis"
T append(T... args)
DataObject * accessData()
Static Object retrieval method: must call specific function.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE>
Definition: GetData.h:401
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:221
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:32
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string_view location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:267
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:128
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string_view location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:178
bool isSuccess() const
Definition: StatusCode.h:365
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string_view location, std::string_view location2) const
the only one essential method
Definition: GetData.h:473
T move(T... args)
Getter::return_type return_type
the actual return type
Definition: GetData.h:411
A small class used to access easily (and efficiently) data items residing in data stores.
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
bool operator()(IDataProviderSvc *service, std::string_view location) const
the only one essential method
Definition: GetData.h:335
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor
Definition: GetData.h:301
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string_view location, std::string_view location2) const
the only one essential method
Definition: GetData.h:422
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:165
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:217
Useful class for representation of "sequence" of the objects through the range of valid iterators.
Definition: Range.h:95
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:95
DataObject * getData(IDataProviderSvc *service, std::string location) const
get the data form transient store
Definition: GetData.h:366
return_type operator()(const COMMON &common, IDataProviderSvc *service, std::string_view location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:140
virtual const id_type & identifier() const =0
Full identifier (or key)
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:275
DataObject * getData(IDataProviderSvc *service, std::string location) const
get the data form transient store
Definition: GetData.h:230
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:282
GetData< TYPE > Getter
the actual data getter
Definition: GetData.h:405