The Gaudi Framework  master (37c0b60a)
DataHandleHolderBase.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_DATAHANDLEHOLDERBASE
12 #define GAUDIKERNEL_DATAHANDLEHOLDERBASE 1
13 
14 #include <Gaudi/Property.h>
15 #include <GaudiKernel/DataHandle.h>
16 #include <GaudiKernel/DataObjID.h>
19 
20 #include <algorithm>
21 #include <unordered_set>
22 
23 namespace {
24  template <typename Container>
25  std::vector<Gaudi::DataHandle*> handles( Container& c, Gaudi::DataHandle::Mode mode ) {
28  [&]( const Gaudi::DataHandle* hndl ) -> bool { return hndl->mode() & mode; } );
29  return h;
30  }
31 } // namespace
32 
33 template <class BASE>
34 class GAUDI_API DataHandleHolderBase : public extends<BASE, IDataHandleHolder> {
35 public:
37 
39  return handles( m_handles, Gaudi::DataHandle::Reader );
40  }
42  return handles( m_handles, Gaudi::DataHandle::Writer );
43  }
44 
45  virtual const DataObjIDColl& extraInputDeps() const override { return m_extInputDataObjs; }
46  virtual const DataObjIDColl& extraOutputDeps() const override { return m_extOutputDataObjs; }
47 
48  void declare( Gaudi::DataHandle& handle ) override {
49  if ( !handle.owner() ) handle.setOwner( this );
50 
51  if ( handle.owner() != this ) {
52  throw GaudiException( "Attempt to declare foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
53  }
54 
55  m_handles.insert( &handle );
56  }
57 
58  void renounce( Gaudi::DataHandle& handle ) override {
59  if ( handle.owner() != this ) {
60  throw GaudiException( "Attempt to renounce foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
61  }
62  m_handles.erase( &handle );
63  }
64 
65  bool renounceInput( const DataObjID& id ) override {
66  bool renounced = false;
67  // TODO: C++20: use std::erase_if
68  for ( auto i = m_handles.begin(), last = m_handles.end(); i != last; ) {
69  if ( ( *i )->mode() == Gaudi::DataHandle::Reader && ( *i )->fullKey() == id ) {
70  i = m_handles.erase( i );
71  renounced = true;
72  } else {
73  ++i;
74  }
75  }
76  if ( auto elm = m_inputDataObjs.find( id ); elm != m_inputDataObjs.end() ) {
77  m_inputDataObjs.erase( elm );
78  renounced = true;
79  }
80  return renounced;
81  }
82 
83  const DataObjIDColl& inputDataObjs() const override { return m_inputDataObjs; }
84  const DataObjIDColl& outputDataObjs() const override { return m_outputDataObjs; }
85 
86  void addDependency( const DataObjID& id, const Gaudi::DataHandle::Mode& mode ) override {
87  if ( mode & Gaudi::DataHandle::Reader ) m_inputDataObjs.emplace( id );
88  if ( mode & Gaudi::DataHandle::Writer ) m_outputDataObjs.emplace( id );
89  }
90 
91 protected:
95  for ( auto h : m_handles ) h->init();
96  }
97 
98  DataObjIDColl m_inputDataObjs, m_outputDataObjs;
99 
100 private:
102 
103  Gaudi::Property<DataObjIDColl> m_extInputDataObjs{ this, "ExtraInputs", DataObjIDColl{} };
104  Gaudi::Property<DataObjIDColl> m_extOutputDataObjs{ this, "ExtraOutputs", DataObjIDColl{} };
105 };
106 
107 #endif // !GAUDIKERNEL_DATAHANDLEHOLDERBASE
DataHandleHolderBase
Definition: DataHandleHolderBase.h:34
DataHandleHolderBase::renounceInput
bool renounceInput(const DataObjID &id) override
Definition: DataHandleHolderBase.h:65
std::unordered_set< DataObjID, DataObjID_Hasher >
GaudiException.h
Gaudi::DataHandle::setOwner
virtual void setOwner(IDataHandleHolder *o)
Definition: DataHandle.h:52
std::vector
STL class.
std::back_inserter
T back_inserter(T... args)
GaudiException
Definition: GaudiException.h:31
DataHandleHolderBase::addDependency
void addDependency(const DataObjID &id, const Gaudi::DataHandle::Mode &mode) override
Definition: DataHandleHolderBase.h:86
Gaudi::DataHandle
Definition: DataHandle.h:38
DataHandleHolderBase::outputHandles
std::vector< Gaudi::DataHandle * > outputHandles() const override
Definition: DataHandleHolderBase.h:41
gaudirun.c
c
Definition: gaudirun.py:525
DataObjID.h
Gaudi::DataHandle::Writer
@ Writer
Definition: DataHandle.h:40
DataHandleHolderBase::m_outputDataObjs
DataObjIDColl m_outputDataObjs
Definition: DataHandleHolderBase.h:98
DataHandleHolderBase::declare
void declare(Gaudi::DataHandle &handle) override
Definition: DataHandleHolderBase.h:48
DataHandleHolderBase::initDataHandleHolder
void initDataHandleHolder()
initializes all handles - called by the sysInitialize method of any descendant of this
Definition: DataHandleHolderBase.h:94
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:53
AlgSequencer.h
h
Definition: AlgSequencer.py:31
DataHandleHolderBase::extraOutputDeps
virtual const DataObjIDColl & extraOutputDeps() const override
Definition: DataHandleHolderBase.h:46
std::copy_if
T copy_if(T... args)
DataHandleHolderBase::extraInputDeps
virtual const DataObjIDColl & extraInputDeps() const override
Definition: DataHandleHolderBase.h:45
DataHandleHolderBase::renounce
void renounce(Gaudi::DataHandle &handle) override
Definition: DataHandleHolderBase.h:58
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
DataObjID
Definition: DataObjID.h:47
Gaudi::DataHandle::Mode
Mode
Definition: DataHandle.h:40
DataHandleHolderBase::inputHandles
std::vector< Gaudi::DataHandle * > inputHandles() const override
Definition: DataHandleHolderBase.h:38
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
DataHandleHolderBase::outputDataObjs
const DataObjIDColl & outputDataObjs() const override
Definition: DataHandleHolderBase.h:84
IDataHandleHolder.h
Gaudi::DataHandle::mode
virtual Mode mode() const
Definition: DataHandle.h:55
std::begin
T begin(T... args)
DataHandle.h
Gaudi::DataHandle::Reader
@ Reader
Definition: DataHandle.h:40
DataHandleHolderBase::inputDataObjs
const DataObjIDColl & inputDataObjs() const override
Definition: DataHandleHolderBase.h:83
std::end
T end(T... args)
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
DataHandleHolderBase::m_handles
std::unordered_set< Gaudi::DataHandle * > m_handles
Definition: DataHandleHolderBase.h:101
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:37
Property.h