The Gaudi Framework  v33r0 (d5ea422b)
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>
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  inline typename _GetType<TYPE>::return_type getFromTS( IDataProviderSvc* service, const std::string& 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  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& 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, "get():: No valid data at '" + location + "'" );
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  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
179  const bool checkData = true ) const {
182  DataObject* object = this->getData( service, 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 ) common.Assert( false, "get():: No valid data at '" + location + "'" );
206  }
207  // no valid data
208  if ( checkData ) common.Assert( false, "get():: No data at '" + location + "'" );
209  // the fictive return
210  return return_type();
211  }
212  // ======================================================================
213  public:
214  // ======================================================================
215  // create the range from the container
216  return_type make_range( const typename TYPE::Container* cnt ) const {
217  return 0 == cnt ? return_type() : make_range( cnt->begin(), cnt->end() );
218  }
219  // create the range from the selection
220  return_type make_range( const typename TYPE::Selection* cnt ) const {
221  return 0 == cnt ? return_type() : return_type( cnt->begin(), cnt->end() );
222  }
223  // ======================================================================
232  return getter.accessData();
233  }
234  // ======================================================================
235  private:
236  // ======================================================================
237  template <class ITERATOR>
238  return_type make_range( ITERATOR first, ITERATOR last ) const {
239  auto _begin = reinterpret_cast<typename return_type::const_iterator*>( &first );
240  auto _end = reinterpret_cast<typename return_type::const_iterator*>( &last );
241  return return_type( *_begin, *_end );
242  }
243  // ======================================================================
244  };
245  // ========================================================================
247  template <class TYPE>
248  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*>>> {
249  public:
250  // ======================================================================
254  // ======================================================================
255  public:
256  // ======================================================================
264  template <class COMMON>
265  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
266  const bool checkData = true ) const {
267  return return_type( m_range( common, service, location, checkData ), location );
268  }
269  // ======================================================================
270  public:
271  // ======================================================================
272  // create the range from the container
273  return_type make_range( const typename TYPE::Container* cnt ) const {
274  if ( !cnt ) { return return_type(); }
275  static const std::string s_empty = "";
276  const IRegistry* reg = cnt->registry();
277  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
278  }
279  // create the range from the selection
280  return_type make_range( const typename TYPE::Selection* cnt ) const {
281  if ( !cnt ) { return return_type(); }
282  static const std::string s_empty = "";
283  const IRegistry* reg = cnt->registry();
284  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
285  }
286  // ======================================================================
293  return m_range.getData( service, location );
294  }
295  // ======================================================================
296  private:
300  // ======================================================================
301  };
302  // ========================================================================
304  template <class TYPE>
305  struct GetData<const TYPE> : public GetData<TYPE> {};
306  // ========================================================================
308  template <class TYPE>
309  struct GetData<TYPE*> : public GetData<TYPE> {};
310  // ========================================================================
312  template <class TYPE>
313  struct GetData<TYPE&> : public GetData<TYPE> {};
314  // ========================================================================
323  template <class TYPE>
324  struct CheckData {
325  public:
326  // ======================================================================
333  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const {
335  return getFromTS<TYPE>( service, location );
336  }
337  // ======================================================================
338  };
339  // ========================================================================
341  template <class TYPE>
342  struct CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {
343  public:
344  // ======================================================================
351  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const {
352  DataObject* object = this->getData( service, location );
353  if ( !object ) { return false; }
354  return dynamic_cast<typename TYPE::Selection*>( object ) || dynamic_cast<typename TYPE::Container*>( object );
355  }
356  // ======================================================================
357  protected:
358  // ======================================================================
367  return getter.accessData();
368  }
369  // ======================================================================
370  };
371  // ========================================================================
373  template <class TYPE>
374  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*>>>
375  : public CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {};
376  // ========================================================================
378  template <class TYPE>
379  struct CheckData<TYPE*> : public CheckData<TYPE> {};
380  // ========================================================================
382  template <class TYPE>
383  struct CheckData<TYPE&> : public CheckData<TYPE> {};
384  // ========================================================================
386  template <class TYPE>
387  struct CheckData<const TYPE> : public CheckData<TYPE> {};
388  // ========================================================================
397  template <class TYPE, class TYPE2>
399  private:
400  // ======================================================================
402  typedef GetData<TYPE> Getter; // the actual data getter
403  // ======================================================================
404  public:
405  // ======================================================================
406  typedef typename Getter::Type Type;
409  // ======================================================================
410  public:
411  // ======================================================================
418  template <class COMMON>
419  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
420  const std::string& location2 ) const {
421  SmartDataPtr<TYPE> obj( service, location );
422  if ( !obj ) {
423  auto o = std::make_unique<TYPE2>();
424  auto r = o.get();
425  common.put( service, std::move( o ), location2 );
426  if ( common.msgLevel( MSG::DEBUG ) ) {
427  common.debug() << "The object of type '" << System::typeinfoName( typeid( *r ) )
428  << "' has been created from TS at address '" << location2 << "'" << endmsg;
429  }
430  return r;
431  }
432  auto ret = obj.ptr();
434  common.Assert( !( !ret ), "get():: No valid data at '" + location + "'" );
435  if ( common.msgLevel( MSG::DEBUG ) ) {
436  common.debug() << "The object of type '" << System::typeinfoName( typeid( *ret ) )
437  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
438  }
439  // return *VALID* data
440  return ret;
441  // ====================================================================
442  }
443  };
444  // ========================================================================
445  template <class TYPE, class TYPE2>
446  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*>>, TYPE2> {
447  private:
448  // ======================================================================
451  typedef GetData<Range> Getter; // the actual data getter
453  typedef CheckData<Range> Checker; // the actual data checker
454  // ======================================================================
455  public:
456  // ======================================================================
457  typedef typename Getter::Type Type;
460  // ======================================================================
461  public:
462  // ======================================================================
469  template <class COMMON>
470  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
471  const std::string& location2 ) const {
472  DataObject* obj = m_getter.getData( service, location );
473  if ( !obj ) {
474  common.put( service, std::make_unique<TYPE2>(), location2 );
475  if ( common.msgLevel( MSG::DEBUG ) ) {
476  common.debug() << "The object of type '" << System::typeinfoName( typeid( TYPE2 ) )
477  << "' has been created from TS at address '" << location2 << "'" << endmsg;
478  }
479  }
480  return m_getter( common, service, location );
481  // ====================================================================
482  }
483  // ======================================================================
484  private:
485  // ======================================================================
487  Getter m_getter; // the actual data getter
488  // ======================================================================
489  };
490  // ========================================================================
491  template <class TYPE, class TYPE2>
492  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*>>, TYPE2> {
493  private:
494  // ======================================================================
499  typedef GetData<Range> Getter; // the actual data getter
500  // ======================================================================
501  public:
502  // ======================================================================
503  typedef typename Getter::Type Type;
506  // ======================================================================
507  public:
508  // ======================================================================
515  template <class COMMON>
516  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
517  const std::string& location2 ) const {
518  return return_type( m_range( common, service, location, location2 ), location );
519  }
520  // ======================================================================
521  private:
522  // ======================================================================
524  Helper m_range; // the actual data getter
525  // ======================================================================
526  };
527  // ========================================================================
528  template <class TYPE, class TYPE2>
529  struct GetOrCreateData<TYPE, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
530  template <class TYPE, class TYPE2>
531  struct GetOrCreateData<TYPE*, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
532  template <class TYPE, class TYPE2>
533  struct GetOrCreateData<TYPE*, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
534  // ========================================================================
535  template <class TYPE, class TYPE2>
536  struct GetOrCreateData<TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
537  template <class TYPE, class TYPE2>
538  struct GetOrCreateData<const TYPE, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
539  template <class TYPE, class TYPE2>
540  struct GetOrCreateData<const TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
541  // ========================================================================
542  template <class TYPE, class TYPE2>
543  struct GetOrCreateData<TYPE, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
544  template <class TYPE, class TYPE2>
545  struct GetOrCreateData<TYPE&, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
546  template <class TYPE, class TYPE2>
547  struct GetOrCreateData<TYPE&, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
548  // ========================================================================
549  } // namespace Utils
550  // ==========================================================================
551 } // end of namespace Gaudi
552 // ============================================================================
553 // The END
554 // ============================================================================
555 #endif // GAUDIUTILS_GETDATA_H
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:364
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:265
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:252
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
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:178
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:63
Data provider interface definition.
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:229
const T & getData() const
STL class.
TYPE * ptr()
Automatic conversion to data type.
return_type make_range(ITERATOR first, ITERATOR last) const
Definition: GetData.h:238
Range make_range(const DataObject *obj)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:324
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:140
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:292
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"
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:333
DataObject * accessData()
Static Object retrieval method: must call specific function.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE>
Definition: GetData.h:398
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:220
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:32
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
bool isSuccess() const
Definition: StatusCode.h:361
T move(T... args)
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:470
Getter::return_type return_type
the actual return type
Definition: GetData.h:408
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.
_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:79
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor
Definition: GetData.h:299
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:351
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:419
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:216
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:101
virtual const id_type & identifier() const =0
Full identifier (or key)
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:273
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 operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:516
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:280
GetData< TYPE > Getter
the actual data getter
Definition: GetData.h:402