The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
DataHandleHolderBase.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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#pragma once
12
13#include <Gaudi/Property.h>
18
19#include <algorithm>
20#include <unordered_set>
21
22namespace {
23 template <typename Container>
24 std::vector<Gaudi::DataHandle*> handles( Container& c, Gaudi::DataHandle::Mode mode ) {
25 std::vector<Gaudi::DataHandle*> h;
26 std::copy_if( std::begin( c ), std::end( c ), std::back_inserter( h ),
27 [&]( const Gaudi::DataHandle* hndl ) -> bool { return hndl->mode() & mode; } );
28 return h;
29 }
30} // namespace
31
32template <class BASE>
33class GAUDI_API DataHandleHolderBase : public extends<BASE, IDataHandleHolder> {
34public:
36
37 std::vector<Gaudi::DataHandle*> inputHandles() const override {
38 return handles( m_handles, Gaudi::DataHandle::Reader );
39 }
40 std::vector<Gaudi::DataHandle*> outputHandles() const override {
41 return handles( m_handles, Gaudi::DataHandle::Writer );
42 }
43
44 virtual const DataObjIDColl& extraInputDeps() const override { return m_extInputDataObjs; }
45 virtual const DataObjIDColl& extraOutputDeps() const override { return m_extOutputDataObjs; }
46
47 void declare( Gaudi::DataHandle& handle ) override {
48 if ( !handle.owner() ) handle.setOwner( this );
49
50 if ( handle.owner() != this ) {
51 throw GaudiException( "Attempt to declare foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
52 }
53
54 m_handles.insert( &handle );
55 }
56
57 void renounce( Gaudi::DataHandle& handle ) override {
58 if ( handle.owner() != this ) {
59 throw GaudiException( "Attempt to renounce foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
60 }
61 m_handles.erase( &handle );
62 }
63
64 bool renounceInput( const DataObjID& id ) override {
65 bool renounced = false;
66 // TODO: C++20: use std::erase_if
67 for ( auto i = m_handles.begin(), last = m_handles.end(); i != last; ) {
68 if ( ( *i )->mode() == Gaudi::DataHandle::Reader && ( *i )->fullKey() == id ) {
69 i = m_handles.erase( i );
70 renounced = true;
71 } else {
72 ++i;
73 }
74 }
75 if ( auto elm = m_inputDataObjs.find( id ); elm != m_inputDataObjs.end() ) {
76 m_inputDataObjs.erase( elm );
77 renounced = true;
78 }
79 return renounced;
80 }
81
82 const DataObjIDColl& inputDataObjs() const override { return m_inputDataObjs; }
83 const DataObjIDColl& outputDataObjs() const override { return m_outputDataObjs; }
84
85 void addDependency( const DataObjID& id, const Gaudi::DataHandle::Mode& mode ) override {
86 if ( mode & Gaudi::DataHandle::Reader ) m_inputDataObjs.emplace( id );
87 if ( mode & Gaudi::DataHandle::Writer ) m_outputDataObjs.emplace( id );
88 }
89
90protected:
94 for ( auto h : m_handles ) h->init();
95 }
96
98
99private:
100 std::unordered_set<Gaudi::DataHandle*> m_handles;
101
104};
std::unordered_set< DataObjID, DataObjID_Hasher > DataObjIDColl
Definition DataObjID.h:121
#define GAUDI_API
Definition Kernel.h:49
const DataObjIDColl & outputDataObjs() const override
std::vector< Gaudi::DataHandle * > inputHandles() const override
void declare(Gaudi::DataHandle &handle) override
std::vector< Gaudi::DataHandle * > outputHandles() const override
void initDataHandleHolder()
initializes all handles - called by the sysInitialize method of any descendant of this
bool renounceInput(const DataObjID &id) override
virtual const DataObjIDColl & extraInputDeps() const override
void addDependency(const DataObjID &id, const Gaudi::DataHandle::Mode &mode) override
void renounce(Gaudi::DataHandle &handle) override
const DataObjIDColl & inputDataObjs() const override
Gaudi::Property< DataObjIDColl > m_extOutputDataObjs
std::unordered_set< Gaudi::DataHandle * > m_handles
Gaudi::Property< DataObjIDColl > m_extInputDataObjs
virtual const DataObjIDColl & extraOutputDeps() const override
virtual void setOwner(IDataHandleHolder *o)
Definition DataHandle.h:51
virtual IDataHandleHolder * owner() const
Definition DataHandle.h:52
virtual Mode mode() const
Definition DataHandle.h:54
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Define general base for Gaudi exception.
constexpr static const auto FAILURE
Definition StatusCode.h:100
Base class used to extend a class implementing other interfaces.
Definition extends.h:19