| The Gaudi Framework v32r1 (f65d50dc)

DataObjectHandle.h
Go to the documentation of this file.
1 #ifndef GAUDIHIVE_DATAOBJECTHANDLE_H
2 #define GAUDIHIVE_DATAOBJECTHANDLE_H
3
4 #include "GaudiKernel/AnyDataWrapper.h"
5 #include "GaudiKernel/DataObjectHandleBase.h"
6 #include "GaudiKernel/GaudiException.h"
7 #include "GaudiKernel/IDataHandleHolder.h"
8 #include "GaudiKernel/IProperty.h"
9 #include "GaudiKernel/IRegistry.h"
10 #include "GaudiKernel/NamedRange.h"
11
12 #include <type_traits>
13
14 //---------------------------------------------------------------------------
15 //
16 namespace details {
17 template <typename T>
18 using Converter_t = T ( * )( const DataObject* );
19
20 template <typename Range, typename StorageType>
21 Range make_range( const DataObject* obj ) {
22 auto c = static_cast<const StorageType*>( obj );
23 if ( UNLIKELY( !c ) ) return Range();
24 using std::begin;
25 using std::end;
26 auto first = begin( *c );
27 auto last = end( *c );
28 // return Range( first, last );
29 auto _first = reinterpret_cast<typename Range::const_iterator*>( &first );
30 auto _last = reinterpret_cast<typename Range::const_iterator*>( &last );
31 return Range( *_first, *_last );
32 }
33
34 template <typename ValueType, typename Range = Gaudi::Range_<typename ValueType::ConstVector>>
35 Converter_t<Range> select_range_converter( const DataObject* obj ) {
36 using Selection = typename ValueType::Selection;
37 auto sel = dynamic_cast<const Selection*>( obj );
38 if ( sel ) return &make_range<Range, typename ValueType::Selection>;
39 auto con = dynamic_cast<std::add_const_t<typename ValueType::Container>*>( obj );
40 if ( con ) return &make_range<Range, typename ValueType::Container>;
41 return nullptr;
42 }
43
44 template <typename T>
45 bool verifyType( const DataObject* dataObj ) {
46 using Type = std::add_const_t<T>;
47 assert( dataObj != nullptr );
48 auto obj = dynamic_cast<Type*>( dataObj );
49 bool ok = ( obj != nullptr );
50 if ( !ok ) {
51 const auto* registry = dataObj->registry();
52 throw GaudiException( "The type expected for " + registry->identifier() + " is " +
53 System::typeinfoName( typeid( Type ) ) +
54 " and is different from the one of the object in the store which is " +
55 System::typeinfoName( typeid( *dataObj ) ) + ".",
56 "Wrong DataObjectType", StatusCode::FAILURE );
57 }
58 assert( obj == static_cast<Type*>( dataObj ) );
59 return ok;
60 }
61
62 template <typename T>
63 struct Payload_helper {
64 using type = std::conditional_t<std::is_base_of_v<DataObject, T>, T, AnyDataWrapper<T>>;
65 };
66 template <typename T>
67 struct Payload_helper<Gaudi::Range_<T>> {
68 using type = Gaudi::Range_<T>;
69 };
70
71 template <typename T>
72 using Payload_t = typename Payload_helper<T>::type;
73 } // namespace details
74
75 //---------------------------------------------------------------------------
76
87 //---------------------------------------------------------------------------
88
89 template <typename T>
90 class DataObjectHandle : public DataObjectHandleBase {
91 public:
92 using DataObjectHandleBase::DataObjectHandleBase;
93
97 T* get() const { return get( true ); }
98
103 T* getIfExists() const { return get( false ); }
104
108 bool exist() const { return get( false ) != nullptr; }
109
113 T* getOrCreate() const;
114
118 T* put( std::unique_ptr<T> object ) const;
119
120 // [[deprecated("please pass a std::unique_ptr instead of a raw pointer")]]
121 T* put( T* object ) const { return put( std::unique_ptr<T>( object ) ); }
122
123 private:
124 T* get( bool mustExist ) const;
125 mutable bool m_goodType = false;
126 };
127
128 //---------------------------------------------------------------------------
129 //
137 template <typename T>
138 T* DataObjectHandle<T>::get( bool mustExist ) const {
139 auto dataObj = fetch();
140 if ( UNLIKELY( !dataObj ) ) {
141 if ( mustExist ) { // Problems in getting from the store
142 throw GaudiException( "Cannot retrieve " + objKey() + " from transient store.",
143 m_owner ? owner()->name() : "no owner", StatusCode::FAILURE );
144 }
145 return nullptr;
146 }
147 if ( UNLIKELY( !m_goodType ) ) m_goodType = ::details::verifyType<T>( dataObj );
148 return static_cast<T*>( dataObj );
149 }
150
151 //---------------------------------------------------------------------------
152 template <typename T>
153 T* DataObjectHandle<T>::put( std::unique_ptr<T> objectp ) const {
154 assert( m_init );
155 StatusCode rc = m_EDS->registerObject( objKey(), objectp.get() );
156 if ( !rc.isSuccess() ) {
157 throw GaudiException( "Error in put of " + objKey(), "DataObjectHandle<T>::put", StatusCode::FAILURE );
158 }
159 return objectp.release();
160 }
161
162 //---------------------------------------------------------------------------
163 template <typename T>
164 T* DataObjectHandle<T>::getOrCreate() const {
165 T* obj = get( false );
166 return obj ? obj : put( std::make_unique<T>() );
167 }
168
169 //---------------------------------------------------------------------------
171
172 template <typename T>
173 class DataObjectHandle<Gaudi::Range_<T>> : public DataObjectHandleBase {
174 public:
175 using ValueType = std::remove_cv_t<std::remove_pointer_t<typename T::value_type>>;
176 using Range = Gaudi::Range_<typename ValueType::ConstVector>;
177
178 using DataObjectHandleBase::DataObjectHandleBase;
179
183 Range get() const;
184
185 private:
186 mutable ::details::Converter_t<Range> m_converter = nullptr;
187 };
188
189 template <typename ValueType>
190 auto DataObjectHandle<Gaudi::Range_<ValueType>>::get() const -> Range {
191 auto dataObj = fetch();
192 if ( UNLIKELY( !dataObj ) ) {
193 throw GaudiException( "Cannot retrieve " + objKey() + " from transient store.",
194 m_owner ? owner()->name() : "no owner", StatusCode::FAILURE );
195 }
196 if ( UNLIKELY( !m_converter ) ) {
197 m_converter = ::details::select_range_converter<ValueType>( dataObj );
198 if ( !m_converter ) {
199 throw GaudiException( "The type requested for " + objKey() + " (" + System::typeinfoName( typeid( ValueType ) ) +
200 ")" + " cannot be obtained from object in event store" + " (" +
201 System::typeinfoName( typeid( *dataObj ) ) + ").",
202 "Wrong DataObjectType", StatusCode::FAILURE );
203 }
204 }
205 return ( *m_converter )( dataObj );
206 }
207
208 //---------------------------------------------------------------------------
210 template <typename T>
211 class DataObjectHandle<AnyDataWrapper<T>> : public DataObjectHandleBase {
212 public:
213 using DataObjectHandleBase::DataObjectHandleBase;
214
218 T* get() const { return &_get( true )->getData(); }
219 T* getIfExists() const {
220 auto data = _get( false );
221 if ( data ) {
222 return &data->getData();
223 } else {
224 return nullptr;
225 }
226 }
227
231 const T* put( T&& object ) const;
232
236 std::optional<std::size_t> size() const { return _get()->size(); }
237
238 private:
239 AnyDataWrapper<T>* _get( bool mustExist ) const;
240 mutable bool m_goodType = false;
241 };
242
243 //---------------------------------------------------------------------------
244
245 template <typename T>
246 AnyDataWrapper<T>* DataObjectHandle<AnyDataWrapper<T>>::_get( bool mustExist ) const {
247 auto obj = fetch();
248 if ( UNLIKELY( !obj ) ) {
249 if ( mustExist ) {
250 throw GaudiException( "Cannot retrieve " + objKey() + " from transient store.",
251 m_owner ? owner()->name() : "no owner", StatusCode::FAILURE );
252
253 } else {
254 return nullptr;
255 }
256 }
257 if ( UNLIKELY( !m_goodType ) ) m_goodType = ::details::verifyType<AnyDataWrapper<T>>( obj );
258 return static_cast<AnyDataWrapper<T>*>( obj );
259 }
260
261 //---------------------------------------------------------------------------
262
263 template <typename T>
264 const T* DataObjectHandle<AnyDataWrapper<T>>::put( T&& obj ) const {
265 assert( m_init );
266 auto objectp = std::make_unique<AnyDataWrapper<T>>( std::move( obj ) );
267 StatusCode rc = m_EDS->registerObject( objKey(), objectp.get() );
268 if ( rc.isFailure() ) {
269 throw GaudiException( "Error in put of " + objKey(), "DataObjectHandle<T>::put", StatusCode::FAILURE );
270 }
271 return &objectp.release()->getData();
272 }
273
274 //---------------------------- user-facing interface ----------
275
276 template <typename T>
277 class DataObjectReadHandle : public DataObjectHandle<::details::Payload_t<T>> {
278 template <typename... Args, std::size_t... Is>
279 DataObjectReadHandle( const std::tuple<Args...>& args, std::index_sequence<Is...> )
280 : DataObjectReadHandle( std::get<Is>( args )... ) {}
281
282 public:
283 DataObjectReadHandle( const DataObjID& k, IDataHandleHolder* owner )
284 : DataObjectHandle<::details::Payload_t<T>>{k, Gaudi::DataHandle::Reader, owner} {}
285
288 template <typename OWNER, typename K, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
289 DataObjectReadHandle( OWNER* owner, std::string propertyName, const K& key = {}, std::string doc = "" )
290 : DataObjectHandle<::details::Payload_t<T>>( owner, Gaudi::DataHandle::Reader, std::move( propertyName ), key,
291 std::move( doc ) ) {}
292
293 template <typename... Args>
294 DataObjectReadHandle( const std::tuple<Args...>& args )
295 : DataObjectReadHandle( args, std::index_sequence_for<Args...>{} ) {}
296 };
297
298 template <typename T>
299 class DataObjectWriteHandle : public DataObjectHandle<::details::Payload_t<T>> {
300 template <typename... Args, std::size_t... Is>
301 DataObjectWriteHandle( const std::tuple<Args...>& args, std::index_sequence<Is...> )
302 : DataObjectWriteHandle( std::get<Is>( args )... ) {}
303
304 public:
305 DataObjectWriteHandle( const DataObjID& k, IDataHandleHolder* owner )
306 : DataObjectHandle<::details::Payload_t<T>>{k, Gaudi::DataHandle::Writer, owner} {}
307
310 template <typename OWNER, typename K, typename = std::enable_if_t<std::is_base_of_v<IProperty, OWNER>>>
311 DataObjectWriteHandle( OWNER* owner, std::string propertyName, const K& key = {}, std::string doc = "" )
312 : DataObjectHandle<::details::Payload_t<T>>( owner, Gaudi::DataHandle::Writer, std::move( propertyName ), key,
313 std::move( doc ) ) {}
314
315 template <typename... Args>
316 DataObjectWriteHandle( const std::tuple<Args...>& args )
317 : DataObjectWriteHandle( args, std::index_sequence_for<Args...>{} ) {}
318 };
319
320 #endif
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(OWNER *owner, std::string propertyName, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
Definition: DataObjectHandle.h:311
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
IDataHandleHolder.h
DataObjectHandle::exist
bool exist() const
Check the existence of the object in the transient store.
Definition: DataObjectHandle.h:108
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
gaudirun.c
c
Definition: gaudirun.py:487
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
DataObjectHandle< Gaudi::Range_< T > >::Range
Gaudi::Range_< typename ValueType::ConstVector > Range
Definition: DataObjectHandle.h:176
DataObjectHandle::get
T * get() const
Retrieve object from transient data store.
Definition: DataObjectHandle.h:97
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
DataObjID
Definition: DataObjID.h:37
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(const DataObjID &k, IDataHandleHolder *owner)
Definition: DataObjectHandle.h:283
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
details::end
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:42
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:40
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(OWNER *owner, std::string propertyName, const K &key={}, std::string doc="")
Autodeclaring constructor with property name, mode, key and documentation.
Definition: DataObjectHandle.h:289
DataObjectHandle::m_goodType
bool m_goodType
Definition: DataObjectHandle.h:125
DataObjectHandle::put
T * put(std::unique_ptr< T > object) const
Register object in transient store.
Definition: DataObjectHandle.h:153
IOTest.sel
sel
Definition: IOTest.py:93
details::Payload_helper
Definition: DataObjectHandle.h:63
AnyDataWrapper.h
std
STL namespace.
std::end
T end(T... args)
details::verifyType
bool verifyType(const DataObject *dataObj)
Definition: DataObjectHandle.h:45
std::size_t
DataObjectHandle::getIfExists
T * getIfExists() const
Bypass check of existence of object in transient store Only uses main location of the.
Definition: DataObjectHandle.h:103
Gaudi::DataHandle::Reader
Definition: DataHandle.h:29
std::unique_ptr::release
T release(T... args)
std::string
STL class.
DataObjectHandle
DataObjectHandle.h GaudiKernel/DataObjectHandle.h.
Definition: AlgTool.h:26
DataObjectHandle::put
T * put(T *object) const
Definition: DataObjectHandle.h:121
IDataHandleHolder
Definition: IDataHandleHolder.h:14
details::Payload_helper< Gaudi::Range_< T > >::type
Gaudi::Range_< T > type
Definition: DataObjectHandle.h:68
gaudirun.args
args
Definition: gaudirun.py:307
DataObjectHandle< AnyDataWrapper< T > >::size
std::optional< std::size_t > size() const
Size of boxed item, if boxed item has a 'size' method.
Definition: DataObjectHandle.h:236
details::make_range
Range make_range(const DataObject *obj)
Definition: DataObjectHandle.h:21
AnyDataWrapper
Definition: AnyDataWrapper.h:26
details
Definition: AnyDataWrapper.h:10
DataObjectReadHandle
Definition: DataObjectHandle.h:277
details::Payload_t
typename Payload_helper< T >::type Payload_t
Definition: DataObjectHandle.h:72
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
NamedRange.h
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis"
GaudiException.h
IOTest.rc
rc
Definition: IOTest.py:101
GaudiPython.GaudiAlgs._get
def _get(self, location)
Definition: GaudiAlgs.py:406
DataObjectWriteHandle
Definition: DataObjectHandle.h:299
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(const std::tuple< Args... > &args, std::index_sequence< Is... >)
Definition: DataObjectHandle.h:279
IRegistry.h
GaudiPluginService.cpluginsvc.registry
def registry()
Definition: cpluginsvc.py:71
std::move
T move(T... args)
std::tuple
DataObjectHandle::getOrCreate
T * getOrCreate() const
Get object from store or create a new one if it doesn't exist.
Definition: DataObjectHandle.h:164
DataObjectHandleBase.h
DataObjectHandle< AnyDataWrapper< T > >::get
T * get() const
Retrieve object from transient data store.
Definition: DataObjectHandle.h:218
std::unique_ptr::get
T get(T... args)
details::Payload_helper::type
std::conditional_t< std::is_base_of_v< DataObject, T >, T, AnyDataWrapper< T > > type
Definition: DataObjectHandle.h:64
details::Converter_t
T(*)(const DataObject *) Converter_t
Definition: DataObjectHandle.h:18
IProperty.h
Gaudi::Functional::details::get
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
Definition: FunctionalDetails.h:332
std::unique_ptr
STL class.
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:26
std::begin
T begin(T... args)
Gaudi::Functional::details::put
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition: FunctionalDetails.h:121
details::begin
auto begin(reverse_wrapper< T > &w)
Definition: reverse.h:37
Gaudi::Range_
Useful class for representation of "sequence" of the objects through the range of valid iterators.
Definition: Range.h:85
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
Gaudi::DataHandle::Writer
Definition: DataHandle.h:29
details::select_range_converter
Converter_t< Range > select_range_converter(const DataObject *obj)
Definition: DataObjectHandle.h:35
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(const DataObjID &k, IDataHandleHolder *owner)
Definition: DataObjectHandle.h:305
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(const std::tuple< Args... > &args)
Definition: DataObjectHandle.h:316
DataObjectHandle< Gaudi::Range_< T > >::ValueType
std::remove_cv_t< std::remove_pointer_t< typename T::value_type > > ValueType
Definition: DataObjectHandle.h:175
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Tuples::Type
Type
the list of available types for ntuples
Definition: TupleObj.h:79
DataObjectReadHandle::DataObjectReadHandle
DataObjectReadHandle(const std::tuple< Args... > &args)
Definition: DataObjectHandle.h:294
DataObjectHandle< AnyDataWrapper< T > >::getIfExists
T * getIfExists() const
Definition: DataObjectHandle.h:219
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
DataObjectHandleBase::DataObjectHandleBase
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
Definition: DataObjectHandleBase.cpp:45
DataObjectWriteHandle::DataObjectWriteHandle
DataObjectWriteHandle(const std::tuple< Args... > &args, std::index_sequence< Is... >)
Definition: DataObjectHandle.h:301