2 Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
6 * @file AsgDataHandles/WriteHandle.icc
7 * @author Nils Krumnack <Nils.Erik.Krumnack@cern.h>
8 * @author S. Binet, P. Calafiura, scott snyder <snyder@bnl.gov> (for original version)
9 * @brief Handle class for recording to StoreGate.
12#include "xAODRootAccess/TActiveStore.h"
13#include "xAODRootAccess/TStore.h"
20//************************************************************************
26// * @brief Default constructor.
28// * The handle will not be usable until a non-blank key is assigned.
32// WriteHandle<T>::WriteHandle()
33// : VarHandleBase(ClassID_traits<T>::ID(), Gaudi::DataHandle::Writer)
39 * @brief Constructor with full arguments.
40 * @param sgkey StoreGate key of the referenced object.
44WriteHandle<T>::WriteHandle (const std::string& sgkey)
45 : VarHandleBase (sgkey)
51 * @brief Constructor from a WriteHandleKey.
52 * @param key The key object holding the clid/key/store.
54 * This will raise an exception if the StoreGate key is blank,
55 * or if the event store cannot be found.
59WriteHandle<T>::WriteHandle (const WriteHandleKey<T>& key)
60 : VarHandleBase (key, nullptr)
66 * @brief Constructor from a WriteHandleKey and an explicit event context.
67 * @param key The key object holding the clid/key.
68 * @param ctx The event context.
70 * This will raise an exception if the StoreGate key is blank,
71 * or if the event store cannot be found.
73 * If the default event store has been requested, then the thread-specific
74 * store from the event context will be used.
78WriteHandle<T>::WriteHandle (const WriteHandleKey<T>& key,
79 const EventContext& ctx)
80 : VarHandleBase (key, &ctx)
86 * @brief Copy constructor.
90WriteHandle<T>::WriteHandle(const WriteHandle& h)
97 * @brief Move constructor.
100WriteHandle<T>::WriteHandle(WriteHandle&& h)
101 : VarHandleBase(std::move(h))
103 // m_lockAuxPending = h.m_lockAuxPending;
104 // h.m_lockAuxPending = nullptr;
109 * @brief Assignment operator.
114WriteHandle<T>::operator= (const WriteHandle& h)
117 this->VarHandleBase::operator=(h);
123 * @brief Move operator.
128WriteHandle<T>::operator= (WriteHandle&& h)
131 this->VarHandleBase::operator=(std::move(h));
132 // m_lockAuxPending = h.m_lockAuxPending;
133 // h.m_lockAuxPending = nullptr;
142 * Lock an aux object if m_lockAuxPending is true.
145WriteHandle<T>::~WriteHandle()
147 // if (m_lockAuxPending) {
148 // m_lockAuxPending->setConst();
153//************************************************************************
154// Deference. These all return only the cached pointer.
159// * @brief Dereference the pointer.
160// * Returns the cached pointer. Throws ExcNullWriteHandle if null.
164// typename WriteHandle<T>::pointer_type
165// WriteHandle<T>::operator->()
167// return WriteHandle<T>::checkedCachedPtr();
172 * @brief Dereference the pointer.
173 * Returns the cached pointer. Throws ExcNullWriteHandle if null.
177typename WriteHandle<T>::reference_type
178WriteHandle<T>::operator*()
180 if (m_ptr == nullptr)
181 throw std::runtime_error ("dereferenced handle is null: " + key());
187// * @brief Dereference the pointer.
188// * Returns the cached pointer.
192// typename WriteHandle<T>::const_pointer_type
193// WriteHandle<T>::cptr() const
195// return reinterpret_cast<pointer_type>(this->m_ptr);
200// * @brief Dereference the pointer.
201// * Returns the cached pointer.
205// typename WriteHandle<T>::pointer_type
206// WriteHandle<T>::ptr()
208// return cachedPtr();
213// * @brief Return the cached pointer directly; no lookup.
217// typename WriteHandle<T>::pointer_type
218// WriteHandle<T>::cachedPtr()
220// return reinterpret_cast<pointer_type>(this->m_ptr);
225 * @brief Can the handle be successfully dereferenced?
229bool WriteHandle<T>::isValid()
231 return this->m_ptr != nullptr;
235//************************************************************************
240 * @brief Record a const object to the store.
241 * @param data The object to record.
246WriteHandle<T>::WriteHandle::record (std::unique_ptr<T> data)
249 if (xAOD::TActiveStore::store()->record (std::move (data), key()).isFailure())
250 return StatusCode::FAILURE;
251 return StatusCode::SUCCESS;
256// * @brief Record a non-const object to the store.
257// * @param data The object to record.
258// * @param isConst If true, record the object as const.
263// WriteHandle<T>::WriteHandle::recordNonConst (std::unique_ptr<T> data)
265// return this->doRecord (std::move(data), false, false);
270 * @brief Record a const object and its auxiliary store to the store.
271 * @param data The object to record.
272 * @param auxstore Auxiliary store object.
275template <class AUXSTORE>
278WriteHandle<T>::WriteHandle::record (std::unique_ptr<T> data,
279 std::unique_ptr<AUXSTORE> auxstore)
281 // If there's no store association, do it now.
282 if (data->getStore() == nullptr)
283 data->setStore (auxstore.get());
286 if (xAOD::TActiveStore::store()->record (std::move (auxstore), key() + "Aux.").isFailure())
287 return StatusCode::FAILURE;
288 if (xAOD::TActiveStore::store()->record (std::move (data), key()).isFailure())
289 return StatusCode::FAILURE;
290 return StatusCode::SUCCESS;
295// * @brief Record a non-const object and its auxiliary store to the store.
296// * @param data The object to record.
297// * @param auxstore Auxiliary store object.
300// template <class AUXSTORE>
303// WriteHandle<T>::WriteHandle::recordNonConst (std::unique_ptr<T> data,
304// std::unique_ptr<AUXSTORE> auxstore)
306// return record (std::move(data), std::move(auxstore), false);
311// * @brief Record a const shared DataObject to the store.
312// * @param data The object to record.
314// * The event store takes shared ownership of the object.
319// WriteHandle<T>::record (SG::DataObjectSharedPtr<T> data)
321// return this->doRecord (std::move(data), true, false);
326// * @brief Record a non-const shared DataObject to the store.
327// * @param data The object to record.
329// * The event store takes shared ownership of the object.
334// WriteHandle<T>::recordNonConst (SG::DataObjectSharedPtr<T> data)
336// return this->doRecord (std::move(data), false, false);
341 * @brief Record an object to the store.
342 * @param data The object to record.
343 * @param returnExisting Allow an existing object?
345 * Unlike record(), this does not change the handle object.
346 * That means that one will not be able to get the object back
347 * by dereferencing the handle.
348 * Returns the object placed in the store, or nullptr if there
350 * If there was already an object in the store with the given key,
351 * then return null, unless @c returnExisting is true, in which case
352 * return success. In either case, @c data is destroyed.
356typename WriteHandle<T>::const_pointer_type
357WriteHandle<T>::put (std::unique_ptr<T> data/*,
358 bool returnExisting / *= false*/) const
360 const_pointer_type result = data.get();
361 if (xAOD::TActiveStore::store()->record (std::move (data), key()).isFailure())
362 throw std::runtime_error ("failed to record object: " + key());
368// * @brief Record an object to the store.
369// * @param data The object to record.
370// * @param returnExisting Allow an existing object?
372// * Unlike record(), this does not change the handle object.
373// * That means that one will not be able to get the object back
374// * by dereferencing the handle.
375// * Returns the object placed in the store, or nullptr if there
377// * If there was already an object in the store with the given key,
378// * then return null, unless @c returnExisting is true, in which case
379// * return success. In either case, @c data is destroyed.
383// typename WriteHandle<T>::const_pointer_type
384// WriteHandle<T>::put (std::unique_ptr<const T> data,
385// bool returnExisting /*= false*/) const
387// IProxyDict* store = nullptr;
388// return doPut (nullptr, std::move(data), returnExisting, store);
393// * @brief Record an object to the store.
394// * @param data The object to record.
395// * @param returnExisting Allow an existing object?
397// * Unlike record(), this does not change the handle object.
398// * That means that one will not be able to get the object back
399// * by dereferencing the handle.
400// * Returns the object placed in the store, or nullptr if there
402// * If there was already an object in the store with the given key,
403// * then return null, unless @c returnExisting is true, in which case
404// * return success. In either case, @c data is destroyed.
408// typename WriteHandle<T>::const_pointer_type
409// WriteHandle<T>::put (std::unique_ptr<const ConstDataVector<T> > data,
410// bool returnExisting /*= false*/) const
412// IProxyDict* store = nullptr;
413// std::unique_ptr<const T> coll (data.release()->asDataVector());
414// return doPut (nullptr, std::move(coll), returnExisting, store);
419// * @brief Record an object to the store.
420// * @param ctx The event context to use.
421// * @param data The object to record.
422// * @param returnExisting Allow an existing object?
424// * Unlike record(), this does not change the handle object.
425// * That means that one will not be able to get the object back
426// * by dereferencing the handle.
427// * Returns the object placed in the store, or nullptr if there
429// * If there was already an object in the store with the given key,
430// * then return null, unless @c returnExisting is true, in which case
431// * return success. In either case, @c data is destroyed.
435// typename WriteHandle<T>::const_pointer_type
436// WriteHandle<T>::put (const EventContext& ctx,
437// std::unique_ptr<const ConstDataVector<T> > data,
438// bool returnExisting /*= false*/) const
440// IProxyDict* store = nullptr;
441// std::unique_ptr<const T> coll (data.release()->asDataVector());
442// return doPut (&ctx, std::move(coll), returnExisting, store);
447// * @brief Record an object to the store.
448// * @param ctx The event context to use.
449// * @param data The object to record.
450// * @param returnExisting Allow an existing object?
452// * Unlike record(), this does not change the handle object.
453// * That means that one will not be able to get the object back
454// * by dereferencing the handle.
455// * Returns the object placed in the store, or nullptr if there
457// * If there was already an object in the store with the given key,
458// * then return null, unless @c returnExisting is true, in which case
459// * return success. In either case, @c data is destroyed.
463// typename WriteHandle<T>::const_pointer_type
464// WriteHandle<T>::put (const EventContext& ctx,
465// std::unique_ptr<T> data,
466// bool returnExisting /*= false*/) const
468// IProxyDict* store = nullptr;
469// return doPut (&ctx, std::move(data), returnExisting, store);
474// * @brief Record an object to the store.
475// * @param ctx The event context to use.
476// * @param data The object to record.
477// * @param returnExisting Allow an existing object?
479// * Unlike record(), this does not change the handle object.
480// * That means that one will not be able to get the object back
481// * by dereferencing the handle.
482// * Returns the object placed in the store, or nullptr if there
484// * If there was already an object in the store with the given key,
485// * then return null, unless @c returnExisting is true, in which case
486// * return success. In either case, @c data is destroyed.
490// typename WriteHandle<T>::const_pointer_type
491// WriteHandle<T>::put (const EventContext& ctx,
492// std::unique_ptr<const T> data,
493// bool returnExisting /*= false*/) const
495// IProxyDict* store = nullptr;
496// return doPut (&ctx, std::move(data), returnExisting, store);
501// * @brief Record an object to the store.
502// * @param data The object to record.
504// * Unlike record(), this does not change the handle object.
505// * That means that one will not be able to get the object back
506// * by dereferencing the handle.
507// * Returns the object placed in the store, or nullptr if there
510// * The event store takes shared ownership of the object.
514// typename WriteHandle<T>::const_pointer_type
515// WriteHandle<T>::put (SG::DataObjectSharedPtr<T> data) const
517// IProxyDict* store = nullptr;
518// return doPut (nullptr, std::move(data), false, store);
523// * @brief Record an object to the store.
524// * @param ctx The event context to use.
525// * @param data The object to record.
527// * Unlike record(), this does not change the handle object.
528// * That means that one will not be able to get the object back
529// * by dereferencing the handle.
530// * Returns the object placed in the store, or nullptr if there
533// * The event store takes shared ownership of the object.
537// typename WriteHandle<T>::const_pointer_type
538// WriteHandle<T>::put (const EventContext& ctx,
539// SG::DataObjectSharedPtr<T> data) const
541// IProxyDict* store = nullptr;
542// return doPut (&ctx, std::move(data), false, store);
547// * @brief Record an object and its auxiliary store to the store.
548// * @param data The object to record.
549// * @param auxstore Auxiliary store object.
551// * Unlike record(), this does not change the handle object.
552// * That means that one will not be able to get the object back
553// * by dereferencing the handle.
554// * Returns the object placed in the store, or nullptr if there
556// * If there was already an object in the store with the given key,
557// * then return null, and the objects passed in are destroyed.
560// template <class AUXSTORE>
561// typename WriteHandle<T>::const_pointer_type
562// WriteHandle<T>::put (std::unique_ptr<T> data,
563// std::unique_ptr<AUXSTORE> auxstore) const
565// return doPut (nullptr, std::move(data), std::move(auxstore));
571// * @brief Record an object and its auxiliary store to the store.
572// * @param data The object to record.
573// * @param auxstore Auxiliary store object.
575// * Unlike record(), this does not change the handle object.
576// * That means that one will not be able to get the object back
577// * by dereferencing the handle.
578// * Returns the object placed in the store, or nullptr if there
580// * If there was already an object in the store with the given key,
581// * then return null, and the objects passed in are destroyed.
583// * Unlike the version taking unique_ptr<T>, this does not alter the
584// * store pointer of @c data.
587// template <class AUXSTORE>
588// typename WriteHandle<T>::const_pointer_type
589// WriteHandle<T>::put (std::unique_ptr<const T> data,
590// std::unique_ptr<const AUXSTORE> auxstore) const
592// return doPut (nullptr, std::move(data), std::move(auxstore));
597// * @brief Record an object and its auxiliary store to the store.
598// * @param ctx The event context to use.
599// * @param data The object to record.
600// * @param auxstore Auxiliary store object.
602// * Unlike record(), this does not change the handle object.
603// * That means that one will not be able to get the object back
604// * by dereferencing the handle.
605// * Returns the object placed in the store, or nullptr if there
607// * If there was already an object in the store with the given key,
608// * then return null, and the objects passed in are destroyed.
611// template <class AUXSTORE>
612// typename WriteHandle<T>::const_pointer_type
613// WriteHandle<T>::put (const EventContext& ctx,
614// std::unique_ptr<T> data,
615// std::unique_ptr<AUXSTORE> auxstore) const
617// return doPut (&ctx, std::move(data), std::move(auxstore));
622// * @brief Record an object and its auxiliary store to the store.
623// * @param ctx The event context to use.
624// * @param data The object to record.
625// * @param auxstore Auxiliary store object.
627// * Unlike record(), this does not change the handle object.
628// * That means that one will not be able to get the object back
629// * by dereferencing the handle.
630// * Returns the object placed in the store, or nullptr if there
632// * If there was already an object in the store with the given key,
633// * then return null, and the objects passed in are destroyed.
635// * Unlike the version taking unique_ptr<T>, this does not alter the
636// * store pointer of @c data.
639// template <class AUXSTORE>
640// typename WriteHandle<T>::const_pointer_type
641// WriteHandle<T>::put (const EventContext& ctx,
642// std::unique_ptr<const T> data,
643// std::unique_ptr<const AUXSTORE> auxstore) const
645// return doPut (&ctx, std::move(data), std::move(auxstore));
650 * @brief Alternate notation for record. Records a non-const object.
651 * @param data Object to record.
653 * Throws an exception on failure.
657WriteHandle<T>::operator= (std::unique_ptr<T> data)
659 if (recordNonConst (std::move(data)).isFailure()) {
660 throw std::runtime_error ("WriteHandle<T>::operator=(unique_ptr) Record failed.");
667// * @brief Make an alias.
668// * @param key Alternate key by which the referenced object should be known.
670// * The current handle should be valid and referencing an object
671// * (i.e., @c record should have been called on it).
673// * The object will also be known by the name given in @c key.
676// StatusCode WriteHandle<T>::alias (const WriteHandleKey<T>& key)
678// return symLink_impl (this->clid(), key.key());
683// * @brief Make an explicit link.
684// * @param key Alternate clid by which the referenced object
685// * should be known. The SG key must match the key of the
688// * You should generally not be using this!
690// * The current handle should be valid and referencing an object
691// * (i.e., @c record should have been called on it).
693// * This makes a symlink: the object will be retrievable
694// * as a different type.
696// * Note that if @c T and @c @U are related via @c SG_BASE and/or
697// * @c DATAVECTOR_BASE, then you shouldn't need to explicitly make a symlink;
698// * that should happen automatically.
700// * If a @c U* is not convertable to a @c T* via C++ rules, then you likely
701// * will be, at best, relying on undefined behavior. You will probably
702// * get warnings from the undefined behavior sanitizer when if you try
703// * to dereference the @c U*.
705// * This usage is here mainly to assist in migrating some existing
706// * patterns to MT. You should think several times before using
711// StatusCode WriteHandle<T>::symLink (const WriteHandleKey<U>& other)
713// if (this->key() != other.key()) {
714// REPORT_ERROR (StatusCode::FAILURE)
715// << "symLink: SG keys do not match: " << other.key() << " vs "
717// return StatusCode::FAILURE;
719// return symLink_impl (other.clid(), other.key());
724// * @brief Return the cached pointer directly.
726// * If it is null, throw ExcNullWriteHandle.
729// typename WriteHandle<T>::pointer_type
730// WriteHandle<T>::checkedCachedPtr()
733// throwExcNullWriteHandle (clid(), key(), store());
734// return cachedPtr();
739// * @brief Helper for record.
740// * @param data The object to record.
741// * @param isConst If true, record the object as const.
742// * @param returnExisting Allow an existing object.
746// StatusCode WriteHandle<T>::doRecord (U data,
748// bool returnExisting)
750// typedef typename U::element_type elt_t;
752// // make sure the BaseInfo(Base) structure is initialized
753// SG::BaseInfo<elt_t>::baseinfo();
755// // If s_isConst is set for this type, then we want to automatically
756// // make it const when recorded.
757// bool allowMods = !isConst;
758// if (ClassID_traits<elt_t>::s_isConst)
761// void* dataPtr(data.get());
762// std::unique_ptr<DataObject> dobj (SG::asStorable (std::move (data)));
763// return this->record_impl (std::move(dobj), dataPtr, allowMods, returnExisting);
768// * @brief Helper for put.
769// * @param ctx The event context, or nullptr to use the current context.
770// * @param data The object to record.
771// * @param returnExisting Allow an existing object.
772// * @param[out] store The store being used.
774// * Unlike record(), this does not change the handle object.
775// * That means that will not be able to get the object back
776// * by dereferencing the handle.
777// * Returns the object placed in the store, or nullptr if there
779// * If there was already an object in the store with the given key,
780// * then return null, unless @c returnExisting is true, in which case
781// * return success. In either case, @c data is destroyed.
785// typename WriteHandle<T>::const_pointer_type
786// WriteHandle<T>::doPut (const EventContext* ctx,
788// bool returnExisting,
789// IProxyDict* & store) const
791// //typedef typename U::element_type elt_t;
794// // make sure the BaseInfo(Base) structure is initialized
795// SG::BaseInfo<elt_t>::baseinfo();
797// const void* dataPtr = data.get();
798// std::unique_ptr<DataObject> dobj (SG::asStorable (std::move (data)));
799// return reinterpret_cast<const T*>
800// (this->put_impl (ctx, std::move(dobj), dataPtr, false, returnExisting, store));
805// * @brief Helper for recording an object and its auxiliary store to the store.
806// * @param ctx The event context, or nullptr to use the current context.
807// * @param data The object to record.
808// * @param auxstore Auxiliary store object.
810// * Unlike record(), this does not change the handle object.
811// * That means that will not be able to get the object back
812// * by dereferencing the handle.
813// * Returns the object placed in the store, or nullptr if there
815// * If there was already an object in the store with the given key,
816// * then return null, and the objects passed in are destroyed.
819// template <class AUXSTORE>
820// typename WriteHandle<T>::const_pointer_type
821// WriteHandle<T>::doPut (const EventContext* ctx,
822// std::unique_ptr<T> data,
823// std::unique_ptr<AUXSTORE> auxstore) const
827// // If there's no store association, do it now.
828// if (data->getStore() == nullptr)
829// data->setStore (auxstore.get());
831// IProxyDict* store = nullptr;
832// const T* ptr = this->doPut (ctx, std::move(data), false, store);
833// if (!ptr) return nullptr;
835// SG::DataObjectSharedPtr<DataObject> dobj
836// (SG::asStorable (std::move (auxstore)));
837// SG::DataProxy* proxy = store->recordObject (std::move(dobj),
838// this->name() + "Aux.",
842// REPORT_ERROR (StatusCode::FAILURE)
843// << "recordObject of aux store failed";
845// // If we've failed here, then the aux store object has been deleted,
846// // but not the primary object. Null out the store pointer to prevent
847// // having a dangling pointer to a deleted object.
848// dref.setStore (static_cast<SG::IConstAuxStore*>(nullptr));
857// * @brief Helper for recording an object and its auxiliary store to the store.
858// * @param ctx The event context, or nullptr to use the current context.
859// * @param data The object to record.
860// * @param auxstore Auxiliary store object.
862// * Unlike record(), this does not change the handle object.
863// * That means that will not be able to get the object back
864// * by dereferencing the handle.
865// * Returns the object placed in the store, or nullptr if there
867// * If there was already an object in the store with the given key,
868// * then return null, and the objects passed in are destroyed.
871// template <class AUXSTORE>
872// typename WriteHandle<T>::const_pointer_type
873// WriteHandle<T>::doPut (const EventContext* ctx,
874// std::unique_ptr<const T> data,
875// std::unique_ptr<const AUXSTORE> auxstore) const
877// IProxyDict* store = nullptr;
878// const T* ptr = this->doPut (ctx, std::move(data), false, store);
879// if (!ptr) return nullptr;
881// SG::DataObjectSharedPtr<DataObject> dobj
882// (SG::asStorable (std::move (auxstore)));
883// SG::DataProxy* proxy = store->recordObject (std::move(dobj),
884// this->name() + "Aux.",
888// REPORT_ERROR (StatusCode::FAILURE)
889// << "recordObject of aux store failed";
898// * @brief Record an object and its auxiliary store to the store.
899// * @param data The object to record.
900// * @param auxstore Auxiliary store object.
901// * @param isConst If true, record the objects as const.
904// template <class AUXSTORE>
906// WriteHandle<T>::record (std::unique_ptr<T> data,
907// std::unique_ptr<AUXSTORE> auxstore,
912// // If there's no store association, do it now.
913// if (data->getStore() == nullptr)
914// data->setStore (auxstore.get());
917// // Temporarily clear the store association, in order to prevent
918// // the aux store from being locked at this point.
919// IAuxStore* store = dref.getStore();
921// dref.setStore (static_cast<SG::IAuxStore*>(nullptr));
922// CHECK (this->record(std::move(data)));
923// // Deliberately not using RAII here. If there is an error,
924// // then the object referenced by data will be deleted.
925// dref.setStore (store);
928// CHECK (this->recordNonConst(std::move(data)));
930// // Store and proxy must be valid if we get to this point.
932// SG::DataObjectSharedPtr<DataObject> dobj
933// (SG::asStorable (std::move (auxstore)));
934// SG::DataProxy* proxy = m_store->recordObject (std::move(dobj),
935// this->name() + "Aux.",
939// REPORT_ERROR (StatusCode::FAILURE)
940// << "recordObject of aux store failed";
942// // If we've failed here, then the aux store object has been deleted,
943// // but not the primary object. Null out the store pointer to prevent
944// // having a dangling pointer to a deleted object.
945// dref.setStore (static_cast<SG::IConstAuxStore*>(nullptr));
946// return StatusCode::FAILURE;
949// if (m_proxy->isConst())
950// m_lockAuxPending = proxy;
952// return StatusCode::SUCCESS;
957 * @brief Return a @c WriteHandle referencing @c key.
958 * @param key The key object holding the clid/key/store.
960 * This will raise an exception if the StoreGate key is blank,
961 * or if the event store cannot be found.
964WriteHandle<T> makeHandle (const WriteHandleKey<T>& key)
966 return WriteHandle<T> (key);
971 * @brief Return a @c WriteHandle referencing @c key for an explicit context.
972 * @param key The key object holding the clid/key/store.
973 * @param ctx The event context.
975 * This will raise an exception if the StoreGate key is blank,
976 * or if the event store cannot be found.
978 * If the default event store has been requested, then the thread-specific
979 * store from the event context will be used.
982WriteHandle<T> makeHandle (const WriteHandleKey<T>& key,
983 const EventContext& ctx)
985 return WriteHandle<T> (key, ctx);