The Gaudi Framework  v29r0 (ff2e7097)
DataObjectHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_DATAOBJECTHANDLE_H
2 #define GAUDIHIVE_DATAOBJECTHANDLE_H
3 
4 #include "GaudiKernel/AlgTool.h"
8 #include "GaudiKernel/Property.h"
10 
11 #include <type_traits>
12 
13 //---------------------------------------------------------------------------
14 
25 //---------------------------------------------------------------------------
26 
27 template <typename T>
29 {
30 public:
32 
36  T* get() const { return get( true ); }
37 
42  T* getIfExists() const { return get( false ); }
43 
47  bool exist() const { return get( false ) != nullptr; }
48 
52  T* getOrCreate();
53 
57  T* put( T* object );
58 
59 private:
60  T* get( bool mustExist ) const;
61  mutable bool m_goodType = false;
62 };
63 
64 template <typename T>
68  {
69  }
71 
72  DataObjectReadHandle( const DataObjectReadHandle& ) = delete;
74 
77  template <class OWNER, class K, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
78  inline DataObjectReadHandle( OWNER* owner, std::string name, const K& key = {}, std::string doc = "" )
79  : DataObjectHandle<T>( owner, Gaudi::DataHandle::Reader, std::move( name ), key, std::move( doc ) )
80  {
81  }
82 };
83 template <typename T>
87  {
88  }
90  {
91  }
92 
95 
98  template <class OWNER, class K, typename = typename std::enable_if<std::is_base_of<IProperty, OWNER>::value>::type>
99  inline DataObjectWriteHandle( OWNER* owner, std::string name, const K& key = {}, std::string doc = "" )
100  : DataObjectHandle<T>( owner, Gaudi::DataHandle::Writer, std::move( name ), key, std::move( doc ) )
101  {
102  }
103 };
104 
105 //---------------------------------------------------------------------------
106 //
114 template <typename T>
115 T* DataObjectHandle<T>::get( bool mustExist ) const
116 {
117 
118  auto dataObj = fetch();
119 
120  if ( UNLIKELY( !dataObj ) ) {
121  if ( mustExist ) { // Problems in getting from the store
122  throw GaudiException( "Cannot retrieve " + objKey() + " from transient store.",
123  m_owner ? owner()->name() : "no owner", StatusCode::FAILURE );
124  }
125  return nullptr;
126  }
127 
128  if ( UNLIKELY( !m_goodType ) ) { // Check type compatibility once
129 
130  T* obj = dynamic_cast<T*>( dataObj );
131  m_goodType = ( obj != nullptr );
132 
133  if ( UNLIKELY( !m_goodType ) ) {
134 
135  std::string errorMsg( "The type provided for " + objKey() + " is " + System::typeinfoName( typeid( T ) ) +
136  " and is different from the one of the object in the store." );
137  // log << MSG::ERROR << errorMsg << endmsg;
138  throw GaudiException( errorMsg, "Wrong DataObjectType", StatusCode::FAILURE );
139  }
140  // log << MSG::DEBUG << "The data type (" << typeid(T).name()
141  // << ") specified for the handle of " << dataProductName()
142  // << " is the same of the object in the store. "
143  // << "From now on the result of a static_cast will be returned." << endmsg;
144  return obj;
145  }
146 
147  // setRead();
148  // From the second read on, this is safe
149  return static_cast<T*>( dataObj );
150 }
151 
152 //---------------------------------------------------------------------------
153 template <typename T>
154 T* DataObjectHandle<T>::put( T* objectp )
155 {
156  assert( m_init );
157  StatusCode rc = m_EDS->registerObject( objKey(), objectp );
158  if ( !rc.isSuccess() ) {
159  throw GaudiException( "Error in put of " + objKey(), "DataObjectHandle<T>::put", StatusCode::FAILURE );
160  }
161  return objectp;
162 }
163 
164 //---------------------------------------------------------------------------
165 template <typename T>
167 {
168 
169  // this process needs to be locking for multi-threaded applications
170  // lock(); --> done in caller
171 
172  T* obj = get( false );
173 
174  // object exists, we are done
175  if ( obj ) {
176  // unlock();
177  return obj;
178  }
179 
180  // create it
181  return put( new T{} );
182 
183  // unlock();
184 }
185 
186 #endif
#define UNLIKELY(x)
Definition: Kernel.h:128
Define general base for Gaudi exception.
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:329
virtual const std::string & objKey() const
Definition: DataHandle.h:49
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
SmartIF< IDataProviderSvc > m_EDS
bool exist() const
Check the existence of the object in the transient store.
DataObjectReadHandle(const DataObjID &k, IDataHandleHolder *owner)
STL class.
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
T * put(T *object)
Register object in transient store.
IDataHandleHolder * m_owner
Definition: DataHandle.h:69
T move(T...args)
DataObjectWriteHandle(const DataObjID &k, IDataHandleHolder *owner)
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
DataObjectWriteHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
DataObject * fetch() const
T * getIfExists() const
Bypass check of existence of object in transient store Only uses main location of the...
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
T * getOrCreate()
Get object from store or create a new one if it doesn&#39;t exist.
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:42
T * get() const
Retrieve object from transient data store.
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
DataObjectReadHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.