| The Gaudi Framework v27r1

- 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/DataObjectHandleBase.h"
5 #include "GaudiKernel/ServiceLocatorHelper.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/Property.h"
8 #include "GaudiKernel/Algorithm.h"
9 #include "GaudiKernel/AlgTool.h"
10
11
12 //---------------------------------------------------------------------------
13
24 //---------------------------------------------------------------------------
25
26
27 template<typename T>
28 class DataObjectHandle : public DataObjectHandleBase {
29
30 public:
31
32 DataObjectHandle();
33 DataObjectHandle(const DataObjID& k, Gaudi::DataHandle::Mode a,
34 IDataHandleHolder* o);
35 DataObjectHandle(const std::string& k, Gaudi::DataHandle::Mode a,
36 IDataHandleHolder* o);
37
38 virtual ~DataObjectHandle() {}
39
43 T* get() { return get(true); }
44
49 T* getIfExists() { return get(false); }
50
54 bool exist() { return get(false) != NULL; }
55
59 T* getOrCreate();
60
64 void put (T* object);
65
66 private:
67
68 T* get(bool mustExist);
69
70 };
71
72 //---------------------------------------------------------------------------
73
74 template<typename T>
75 DataObjectHandle<T>::DataObjectHandle():
76 DataObjectHandleBase() {}
77
78 //---------------------------------------------------------------------------
79 template<typename T>
80 DataObjectHandle<T>::DataObjectHandle(const DataObjID & k,
81 Gaudi::DataHandle::Mode a,
82 IDataHandleHolder* owner):
83 DataObjectHandleBase(k,a,owner) {}
84
85 template<typename T>
86 DataObjectHandle<T>::DataObjectHandle(const std::string & k,
87 Gaudi::DataHandle::Mode a,
88 IDataHandleHolder* owner):
89 DataObjectHandleBase(k,a,owner) {}
90
91 //---------------------------------------------------------------------------
92
100 template<typename T>
101 T* DataObjectHandle<T>::get(bool mustExist) {
102
103 if (!m_init) {
104 init();
105 }
106
107 //MsgStream log(m_MS,"DataObjectHandle");
108
109 DataObject* dataObjectp = NULL;
110
111 StatusCode sc = m_EDS->retrieveObject(objKey(), dataObjectp);
112
113 T* returnObject = NULL;
114 if ( LIKELY( sc.isSuccess() ) ){
115
116 if (UNLIKELY(!m_goodType)){ // Check type compatibility once
117
118 // DP: can use a gaudi feature?
119 m_goodType = (NULL != dynamic_cast<T*> (dataObjectp));
120 //( typeid(tmp) == typeid(*dataObjectp) ) ;
121
122 T tmp;
123
124 const std::string dataType(typeid(tmp).name());
125
126 if (!m_goodType){
127 std::string errorMsg("The type provided for "+ objKey()
128 + " is " + dataType
129 + " and is different form the one of the object in the store.");
130 //log << MSG::ERROR << errorMsg << endmsg;
131 throw GaudiException (errorMsg,"Wrong DataObjectType",StatusCode::FAILURE);
132 }
133 else{
134 //log << MSG::DEBUG << "The data type (" << dataType
135 // << ") specified for the handle of " << dataProductName()
136 // << " is the same of the object in the store. "
137 // << "From now on the result of a static_cast will be returned." << endmsg;
138 }
139 }
140
141 if (LIKELY(m_goodType)) { // From the second read on, this is safe
142 returnObject = static_cast<T*> (dataObjectp);
143 }
144
145 }
146 else if(mustExist){ // Problems in getting from the store
147 throw GaudiException("Cannot retrieve " + objKey() +
148 " from transient store.",
149 m_owner != 0 ? owner()->name() : "no owner",
150 StatusCode::FAILURE);
151 }
152
153 // setRead();
154 return returnObject;
155 }
156
157 //---------------------------------------------------------------------------
158 template<typename T>
159 void DataObjectHandle<T>::put (T *objectp){
160
161 if (!m_init) {
162 init();
163 }
164
165 StatusCode sc = m_EDS->registerObject(objKey(), objectp);
166 sc.ignore();
167 // if ( LIKELY( sc.isSuccess() ) )
168 // setWritten();
169 }
170
171 //---------------------------------------------------------------------------
172 template<typename T>
173 T* DataObjectHandle<T>::getOrCreate (){
174
175 //this process needs to be locking for multi-threaded applications
176 //lock(); --> done in caller
177
178 T* obj = get(false);
179
180 //object exists, we are done
181 if(obj != NULL){
182
183 //unlock();
184 return obj;
185 }
186
187 //MsgStream log(m_MS,"DataObjectHandle");
188 //log << MSG::DEBUG << "Object " << objKey() << " does not exist, creating it" << endmsg;
189
190 //create it
191 obj = new T();
192 put(obj);
193
194 //unlock();
195 return obj;
196 }
197
198
199 #endif
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
Algorithm.h
DataObjectHandle::~DataObjectHandle
virtual ~DataObjectHandle()
Definition: DataObjectHandle.h:38
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:23
DataObjectHandle::exist
bool exist()
Check the existence of the object in the transient store.
Definition: DataObjectHandle.h:54
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
DataObjID
Definition: DataObjID.h:38
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
AlgTool.h
DataObjectHandle::put
void put(T *object)
Register object in transient store.
Definition: DataObjectHandle.h:159
merge_files.sc
int sc
Definition: merge_files.py:167
DataObjectHandle::get
T * get()
Retrieve object from transient data store.
Definition: DataObjectHandle.h:43
std::string
STL class.
DataObjectHandle::DataObjectHandle
DataObjectHandle()
Definition: DataObjectHandle.h:75
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:24
IDataHandleHolder
Definition: IDataHandleHolder.h:17
DataObjectHandle::getIfExists
T * getIfExists()
Bypass check of existence of object in transient store Only uses main location of the...
Definition: DataObjectHandle.h:49
StatusCode::FAILURE
Definition: StatusCode.h:29
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiException.h
LIKELY
#define LIKELY(x)
Definition: Kernel.h:125
DataObjectHandleBase.h
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:24
Property.h
ServiceLocatorHelper.h
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:108
Gaudi::DataHandle::Mode
Mode
Definition: DataHandle.h:30
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
DataObjectHandle::getOrCreate
T * getOrCreate()
Get object from store or create a new one if it doesn't exist.
Definition: DataObjectHandle.h:173