All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GetData.h
Go to the documentation of this file.
1 // $Id: GetData.h,v 1.1 2008/10/10 08:06:33 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDIUTILS_GETDATA_H
4 #define GAUDIUTILS_GETDATA_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // GaudiKernel
9 // ============================================================================
12 #include "GaudiKernel/IRegistry.h"
13 // ============================================================================
14 // GaudiUtils
15 // ============================================================================
16 #include "GaudiKernel/Range.h"
17 #include "GaudiKernel/NamedRange.h"
18 // ============================================================================
19 // Forward declaration
20 // ============================================================================
21 template <class BASE> class GaudiCommon ; // GaudiAlg
22 // ============================================================================
23 namespace Gaudi
24 {
25  namespace Utils
26  {
27  // ========================================================================
34  template <class TYPE>
35  struct _GetType
36  { typedef TYPE* return_type ; };
37  // ========================================================================
39  template <class TYPE>
40  struct _GetType<TYPE*>
41  { typedef TYPE* return_type ; };
42  // ========================================================================
44  template <class TYPE>
45  struct _GetType<TYPE&>
46  { typedef TYPE* return_type ; };
47  // ========================================================================
49  template <class CONTAINER>
50  struct _GetType<Gaudi::Range_<CONTAINER> >
52  // ========================================================================
54  template <class CONTAINER>
55  struct _GetType<Gaudi::NamedRange_<CONTAINER> >
57  // ========================================================================
61  template <class TYPE>
62  inline
64  const std::string& location ) {
65  DataObject *obj = NULL;
66  // Return the casted pointer if the retrieve was successful or NULL otherwise.
67  return service->retrieveObject(location, obj).isSuccess() ?
68  dynamic_cast<typename _GetType<TYPE>::return_type>(obj) :
69  NULL;
70  }
71  // ========================================================================
80  template <class TYPE>
81  struct GetData
82  {
83  public:
84  // ======================================================================
85  typedef TYPE Type ;
88  // ======================================================================
89  public:
90  // ======================================================================
98  template <class COMMON>
99  inline return_type operator()
100  ( const COMMON& common ,
101  IDataProviderSvc* service ,
102  const std::string& location ,
103  const bool checkData = true) const
104  {
105  // use Data Provider Service
106  return_type obj = getFromTS<Type>(service, location);
107  if (checkData) { // check the data
108  common.Assert(obj, "get():: No valid data at '" + location + "'");
109  }
110  // debug printout
111  if ( common.msgLevel ( MSG::DEBUG ) ) {
112  common.debug() << "The object of type '"
113  << System::typeinfoName(typeid(obj))
114  << "' "
115  << (obj ? "has been" : "could not be")
116  << " retrieved from TS at address '"
117  << location << "'" << endmsg ;
118  }
119  // return located data
120  return obj ;
121  // ======================================================================
122  }
123  };
124  // ========================================================================
126  template <class TYPE>
127  struct GetData<Gaudi::Range_<std::vector<const TYPE*> > >
128  {
129  public:
130  // ======================================================================
134  // ======================================================================
135  public:
136  // ======================================================================
144  template <class COMMON>
145  inline return_type operator()
146  ( const COMMON& common ,
147  IDataProviderSvc* service ,
148  const std::string& location ,
149  const bool checkData = true) const
150  {
153  DataObject* object = this -> getData ( service , location ) ;
154  if ( 0 != object )
155  {
157  typedef typename TYPE::Selection Selection_;
158  const Selection_* sel = dynamic_cast<Selection_*> ( object ) ;
159  if ( 0 != sel )
160  {
161  if ( common.msgLevel ( MSG::DEBUG ) )
162  { common.debug() << "The object of type '"
163  << System::typeinfoName(typeid(*object))
164  << "' has been retrieved from TS at address '"
165  << location << "'" << endmsg ; }
166  return make_range ( sel ) ;
167  }
169  typedef typename TYPE::Container Container_ ;
170  const Container_* cnt = dynamic_cast<Container_*> ( object ) ;
171  if ( 0 != cnt )
172  {
173  if ( common.msgLevel ( MSG::DEBUG ) )
174  { common.debug() << "The object of type '"
175  << System::typeinfoName(typeid(*object))
176  << "' has been retrieved from TS at address '"
177  << location << "'" << endmsg ; }
178  return make_range ( cnt ) ;
179  }
180  // no valid data
181  if (checkData)
182  common.Assert ( false , "get():: No valid data at '" + location + "'" ) ;
183  }
184  // no valid data
185  if (checkData)
186  common.Assert ( false , "get():: No data at '" + location + "'" ) ;
187  // the fictive return
188  return return_type () ;
189  }
190  // ======================================================================
191  public:
192  // ======================================================================
193  // create the range from the container
194  return_type make_range ( const typename TYPE::Container* cnt ) const
195  { return 0 == cnt ? return_type() : make_range ( cnt->begin() , cnt->end() ) ; }
196  // create the range from the selection
197  return_type make_range ( const typename TYPE::Selection* cnt ) const
198  { return 0 == cnt ? return_type() : return_type ( cnt->begin() , cnt->end() ) ; }
199  // ======================================================================
206  const std::string& location ) const
207  {
209  SmartDataObjectPtr getter
211  service , 0 , location ) ;
212  return getter.accessData () ;
213  }
214  // ======================================================================
215  private:
216  // ======================================================================
217  template <class ITERATOR>
218  return_type make_range
219  ( ITERATOR first ,
220  ITERATOR last ) const
221  {
222  typename return_type::const_iterator* _begin = reinterpret_cast<typename return_type::const_iterator*>(&first);
223  typename return_type::const_iterator* _end = reinterpret_cast<typename return_type::const_iterator*>(&last);
224  return return_type(*_begin, *_end);
225  }
226  // ======================================================================
227  } ;
228  // ========================================================================
230  template <class TYPE>
231  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
232  {
233  public:
234  // ======================================================================
238  // ======================================================================
239  public:
240  // ======================================================================
248  template <class COMMON>
249  inline return_type operator()
250  ( const COMMON& common ,
251  IDataProviderSvc* service ,
252  const std::string& location ,
253  const bool checkData = true) const
254  {
255  return return_type ( m_range( common , service , location, checkData ) , location ) ;
256  }
257  // ======================================================================
258  public:
259  // ======================================================================
260  // create the range from the container
261  return_type make_range ( const typename TYPE::Container* cnt ) const
262  {
263  if ( 0 == cnt ) { return return_type() ; }
264  static const std::string s_empty = "" ;
265  const IRegistry* reg = cnt->registry() ;
266  return return_type
267  ( m_range.make_range ( cnt ) , 0 != reg ? reg->identifier() : s_empty ) ;
268  }
269  // create the range from the selection
270  return_type make_range ( const typename TYPE::Selection* cnt ) const
271  {
272  if ( 0 == cnt ) { return return_type() ; }
273  static const std::string s_empty = "" ;
274  const IRegistry* reg = cnt->registry() ;
275  return return_type
276  ( m_range.make_range ( cnt ) , 0 != reg ? reg->identifier() : s_empty ) ;
277  }
278  // ======================================================================
285  const std::string& location ) const
286  { return m_range.getData ( service , location ) ; }
287  // ======================================================================
288  private:
292  // ======================================================================
293  } ;
294  // ========================================================================
296  template <class TYPE>
297  struct GetData<const TYPE> : public GetData<TYPE> {} ;
298  // ========================================================================
300  template <class TYPE>
301  struct GetData<TYPE*> : public GetData<TYPE> {} ;
302  // ========================================================================
304  template <class TYPE>
305  struct GetData<TYPE&> : public GetData<TYPE> {} ;
306  // ========================================================================
315  template <class TYPE>
316  struct CheckData
317  {
318  public:
319  // ======================================================================
326  inline bool operator()
327  ( IDataProviderSvc* service ,
328  const std::string& location ) const
329  {
331  return getFromTS<TYPE>(service, location);
332  }
333  // ======================================================================
334  };
335  // ========================================================================
337  template <class TYPE>
338  struct CheckData<Gaudi::Range_<std::vector<const TYPE*> > >
339  {
340  public:
341  // ======================================================================
348  inline bool operator()
349  ( IDataProviderSvc* service ,
350  const std::string& location ) const
351  {
352  DataObject* object = this->getData( service , location ) ;
353  if ( 0 == object ) { return false ; }
354  return
355  0 != dynamic_cast<typename TYPE::Selection*> ( object ) ||
356  0 != dynamic_cast<typename TYPE::Container*> ( object ) ;
357  }
358  // ======================================================================
359  protected:
360  // ======================================================================
367  const std::string& location ) const
368  {
370  SmartDataObjectPtr getter
372  service , 0 , location ) ;
373  return getter.accessData () ;
374  }
375  // ======================================================================
376  } ;
377  // ========================================================================
379  template <class TYPE>
380  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*> > >
381  : public CheckData<Gaudi::Range_<std::vector<const TYPE*> > > {} ;
382  // ========================================================================
384  template <class TYPE>
385  struct CheckData<TYPE*> : public CheckData<TYPE> {} ;
386  // ========================================================================
388  template <class TYPE>
389  struct CheckData<TYPE&> : public CheckData<TYPE> {} ;
390  // ========================================================================
392  template <class TYPE>
393  struct CheckData<const TYPE> : public CheckData<TYPE> {} ;
394  // ========================================================================
403  template <class TYPE, class TYPE2>
405  {
406  private:
407  // ======================================================================
409  typedef GetData<TYPE> Getter ; // the actual data getter
410  // ======================================================================
411  public:
412  // ======================================================================
413  typedef typename Getter::Type Type ;
415  typedef typename Getter::return_type return_type ;
416  // ======================================================================
417  public:
418  // ======================================================================
425  template <class COMMON>
426  inline return_type operator()
427  ( const COMMON& common ,
428  IDataProviderSvc* service ,
429  const std::string& location ,
430  const std::string& location2 ) const
431  {
432  SmartDataPtr<TYPE> obj ( service , location ) ;
433  if ( !obj )
434  {
435  TYPE2* o = new TYPE2() ;
436  common.put ( service , o , location2 ) ;
437  if ( common.msgLevel ( MSG::DEBUG ) )
438  { common.debug() << "The object of type '"
439  << System::typeinfoName(typeid(*o))
440  << "' has been created from TS at address '"
441  << location2 << "'" << endmsg ; }
442  return_type _o = o ;
443  return _o ;
444  }
445  return_type ret = obj ;
447  common.Assert ( !(!ret) , "get():: No valid data at '" + location + "'" ) ;
448  if ( common.msgLevel ( MSG::DEBUG ) )
449  { common.debug() << "The object of type '"
450  << System::typeinfoName(typeid(*ret))
451  << "' has been retrieved from TS at address '"
452  << location << "'" << endmsg ; }
453  // return *VALID* data
454  return ret ;
455  // ====================================================================
456  }
457  };
458  // ========================================================================
459  template <class TYPE, class TYPE2>
460  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*> >,TYPE2>
461  {
462  private:
463  // ======================================================================
466  typedef GetData<Range> Getter ; // the actual data getter
468  typedef CheckData<Range> Checker ; // the actual data checker
469  // ======================================================================
470  public:
471  // ======================================================================
472  typedef typename Getter::Type Type ;
474  typedef typename Getter::return_type return_type ;
475  // ======================================================================
476  public:
477  // ======================================================================
484  template <class COMMON>
485  inline return_type operator()
486  ( const COMMON& common ,
487  IDataProviderSvc* service ,
488  const std::string& location ,
489  const std::string& location2 ) const
490  {
491  DataObject* obj = m_getter.getData ( service , location ) ;
492  if ( 0 == obj )
493  {
494  TYPE2* o = new TYPE2() ;
495  common.put ( service , o , location2 ) ;
496  if ( common.msgLevel ( MSG::DEBUG ) )
497  { common.debug() << "The object of type '"
498  << System::typeinfoName(typeid(*o))
499  << "' has been created from TS at address '"
500  << location2 << "'" << endmsg ; }
501  }
502  return m_getter ( common , service , location ) ;
503  // ====================================================================
504  }
505  // ======================================================================
506  private:
507  // ======================================================================
509  Getter m_getter ; // the actual data getter
510  // ======================================================================
511  };
512  // ========================================================================
513  template <class TYPE, class TYPE2>
514  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*> >,TYPE2>
515  {
516  private:
517  // ======================================================================
522  typedef GetData<Range> Getter ; // the actual data getter
523  // ======================================================================
524  public:
525  // ======================================================================
526  typedef typename Getter::Type Type ;
528  typedef typename Getter::return_type return_type ;
529  // ======================================================================
530  public:
531  // ======================================================================
538  template <class COMMON>
539  inline return_type operator()
540  ( const COMMON& common ,
541  IDataProviderSvc* service ,
542  const std::string& location ,
543  const std::string& location2 ) const
544  {
545  return return_type ( m_range ( common ,
546  service ,
547  location ,
548  location2 ) , location ) ;
549  }
550  // ======================================================================
551  private:
552  // ======================================================================
554  Helper m_range ; // the actual data getter
555  // ======================================================================
556  };
557  // ========================================================================
558  template <class TYPE, class TYPE2>
559  struct GetOrCreateData<TYPE,TYPE2*>
560  : public GetOrCreateData<TYPE,TYPE2> {} ;
561  template <class TYPE, class TYPE2>
562  struct GetOrCreateData<TYPE*,TYPE2>
563  : public GetOrCreateData<TYPE,TYPE2> {} ;
564  template <class TYPE, class TYPE2>
565  struct GetOrCreateData<TYPE*,TYPE2*>
566  : public GetOrCreateData<TYPE,TYPE2> {} ;
567  // ========================================================================
568  template <class TYPE, class TYPE2>
569  struct GetOrCreateData< TYPE,const TYPE2>
570  : public GetOrCreateData<TYPE,TYPE2> {} ;
571  template <class TYPE, class TYPE2>
572  struct GetOrCreateData<const TYPE, TYPE2>
573  : public GetOrCreateData<TYPE,TYPE2> {} ;
574  template <class TYPE, class TYPE2>
575  struct GetOrCreateData<const TYPE,const TYPE2>
576  : public GetOrCreateData<TYPE,TYPE2> {} ;
577  // ========================================================================
578  template <class TYPE, class TYPE2>
579  struct GetOrCreateData<TYPE,TYPE2&>
580  : public GetOrCreateData<TYPE,TYPE2> {} ;
581  template <class TYPE, class TYPE2>
582  struct GetOrCreateData<TYPE&,TYPE2>
583  : public GetOrCreateData<TYPE,TYPE2> {} ;
584  template <class TYPE, class TYPE2>
585  struct GetOrCreateData<TYPE&,TYPE2&>
586  : public GetOrCreateData<TYPE,TYPE2> {} ;
587  // ========================================================================
588  } // end of namespace Gaudi::Utils
589  // ==========================================================================
590 } // end of namespace Gaudi
591 // ============================================================================
592 // The END
593 // ============================================================================
594 #endif // GAUDIUTILS_GETDATA_H
595 // ============================================================================
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:56
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:35
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
Gaudi::NamedRange_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:236
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:205
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:197
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
Helper structure for implementation of "get"-functions for GaudiCommon
Definition: GaudiCommon.h:36
Data provider interface definition.
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:366
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:270
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:194
Helper structure for implementation of "exists"-functions for GaudiCommon
Definition: GetData.h:316
DataObject * accessData()
Static Object retrieval method: must call specific function.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon ...
Definition: GetData.h:404
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
_GetType< Type >::return_type return_type
the actual return type
Definition: GetData.h:87
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:261
Getter::return_type return_type
the actual return type
Definition: GetData.h:415
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:63
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:284
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor ...
Definition: GetData.h:291
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:132
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:77
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:59
virtual const id_type & identifier() const =0
Full identifier (or key)
This is a number of static methods for bootstrapping the Gaudi framework.
Definition: Bootstrap.h:14
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
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:409
virtual StatusCode retrieveObject(IRegistry *pDirectory, const std::string &path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.