| The Gaudi Framework v29r1 (5cd95fa2)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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"
5 #include "GaudiKernel/Algorithm.h"
6 #include "GaudiKernel/DataObjectHandleBase.h"
7 #include "GaudiKernel/GaudiException.h"
8 #include "GaudiKernel/Property.h"
9 #include "GaudiKernel/ServiceLocatorHelper.h"
10
11 #include <type_traits>
12
13 //---------------------------------------------------------------------------
14
25 //---------------------------------------------------------------------------
26
27 template <typename T>
28 class DataObjectHandle : public DataObjectHandleBase
29 {
30 public:
31 using DataObjectHandleBase::DataObjectHandleBase;
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>
65 struct DataObjectReadHandle : public DataObjectHandle<T> {
66 DataObjectReadHandle( const DataObjID& k, IDataHandleHolder* owner )
67 : DataObjectHandle<T>{k, Gaudi::DataHandle::Reader, owner}
68 {
69 }
70 DataObjectReadHandle( const std::string& k, IDataHandleHolder* owner ) : DataObjectReadHandle{DataObjID{k}, owner} {}
71
72 DataObjectReadHandle( const DataObjectReadHandle& ) = delete;
73 DataObjectReadHandle( DataObjectReadHandle&& ) = default;
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>
84 struct DataObjectWriteHandle : public DataObjectHandle<T> {
85 DataObjectWriteHandle( const DataObjID& k, IDataHandleHolder* owner )
86 : DataObjectHandle<T>{k, Gaudi::DataHandle::Writer, owner}
87 {
88 }
89 DataObjectWriteHandle( const std::string& k, IDataHandleHolder* owner ) : DataObjectWriteHandle{DataObjID{k}, owner}
90 {
91 }
92
93 DataObjectWriteHandle( const DataObjectWriteHandle& ) = delete;
94 DataObjectWriteHandle( DataObjectWriteHandle&& ) = default;
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>
166 T* DataObjectHandle<T>::getOrCreate()
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
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
Algorithm.h
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:22
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
StatusCode::FAILURE
Definition: StatusCode.h:31
DataObjID
Definition: DataObjID.h:37
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:329
Gaudi::DataHandle::objKey
virtual const std::string & objKey() const
Definition: DataHandle.h:49
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
AlgTool.h
DataObjectHandle::m_goodType
bool m_goodType
Definition: DataObjectHandle.h:61
DataObjectHandleBase::m_EDS
SmartIF< IDataProviderSvc > m_EDS
Definition: DataObjectHandleBase.h:73
DataObjectHandleBase::m_init
bool m_init
Definition: DataObjectHandleBase.h:76
DataObjectHandle::exist
bool exist() const
Check the existence of the object in the transient store.
Definition: DataObjectHandle.h:47
DataObjectWriteHandle
Definition: DataObjectHandle.h:84
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(const DataObjID &k, IDataHandleHolder *owner)
Definition: DataObjectHandle.h:66
Gaudi::DataHandle::Reader
Definition: DataHandle.h:31
std::string
STL class.
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:27
IDataHandleHolder
Definition: IDataHandleHolder.h:14
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
GaudiException.h
gaudirun.type
type
Definition: gaudirun.py:133
IOTest.rc
rc
Definition: IOTest.py:103
DataObjectHandle::put
T * put(T *object)
Register object in transient store.
Definition: DataObjectHandle.h:154
Gaudi::DataHandle::m_owner
IDataHandleHolder * m_owner
Definition: DataHandle.h:69
std::move
T move(T...args)
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(const DataObjID &k, IDataHandleHolder *owner)
Definition: DataObjectHandle.h:85
DataObjectHandleBase.h
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:25
Property.h
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
Definition: DataObjectHandle.h:99
Gaudi::DataHandle::Writer
Definition: DataHandle.h:31
ServiceLocatorHelper.h
DataObjectHandleBase::fetch
DataObject * fetch() const
Definition: DataObjectHandleBase.cpp:64
DataObjectHandle::getIfExists
T * getIfExists() const
Bypass check of existence of object in transient store Only uses main location of the...
Definition: DataObjectHandle.h:42
IDataProviderSvc::registerObject
virtual StatusCode registerObject(const std::string &fullPath, DataObject *pObject)=0
Register object with the data store.
DataObjectHandle::getOrCreate
T * getOrCreate()
Get object from store or create a new one if it doesn&#39;t exist.
Definition: DataObjectHandle.h:166
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:42
DataObjectHandle::get
T * get() const
Retrieve object from transient data store.
Definition: DataObjectHandle.h:36
DataObjectHandleBase::DataObjectHandleBase
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
Definition: DataObjectHandleBase.cpp:47
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(OWNER *owner, std::string name, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
Definition: DataObjectHandle.h:78
DataObjectReadHandle
Definition: DataObjectHandle.h:65