Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:07:11

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_DATAHANDLEHOLDERBASE
0012 #define GAUDIKERNEL_DATAHANDLEHOLDERBASE 1
0013 
0014 #include "GaudiKernel/DataHandle.h"
0015 #include "GaudiKernel/DataObjID.h"
0016 #include "GaudiKernel/GaudiException.h"
0017 #include "GaudiKernel/IDataHandleHolder.h"
0018 #include <Gaudi/Property.h>
0019 
0020 #include <algorithm>
0021 #include <unordered_set>
0022 
0023 namespace {
0024   template <typename Container>
0025   std::vector<Gaudi::DataHandle*> handles( Container& c, Gaudi::DataHandle::Mode mode ) {
0026     std::vector<Gaudi::DataHandle*> h;
0027     std::copy_if( std::begin( c ), std::end( c ), std::back_inserter( h ),
0028                   [&]( const Gaudi::DataHandle* hndl ) -> bool { return hndl->mode() & mode; } );
0029     return h;
0030   }
0031 } // namespace
0032 
0033 template <class BASE>
0034 class GAUDI_API DataHandleHolderBase : public extends<BASE, IDataHandleHolder> {
0035 public:
0036   using extends<BASE, IDataHandleHolder>::extends;
0037 
0038   std::vector<Gaudi::DataHandle*> inputHandles() const override {
0039     return handles( m_handles, Gaudi::DataHandle::Reader );
0040   }
0041   std::vector<Gaudi::DataHandle*> outputHandles() const override {
0042     return handles( m_handles, Gaudi::DataHandle::Writer );
0043   }
0044 
0045   virtual const DataObjIDColl& extraInputDeps() const override { return m_extInputDataObjs; }
0046   virtual const DataObjIDColl& extraOutputDeps() const override { return m_extOutputDataObjs; }
0047 
0048   void declare( Gaudi::DataHandle& handle ) override {
0049     if ( !handle.owner() ) handle.setOwner( this );
0050 
0051     if ( handle.owner() != this ) {
0052       throw GaudiException( "Attempt to declare foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
0053     }
0054 
0055     m_handles.insert( &handle );
0056   }
0057 
0058   void renounce( Gaudi::DataHandle& handle ) override {
0059     if ( handle.owner() != this ) {
0060       throw GaudiException( "Attempt to renounce foreign handle with algorithm!", this->name(), StatusCode::FAILURE );
0061     }
0062     m_handles.erase( &handle );
0063   }
0064 
0065   bool renounceInput( const DataObjID& id ) override {
0066     bool renounced = false;
0067     // TODO: C++20: use std::erase_if
0068     for ( auto i = m_handles.begin(), last = m_handles.end(); i != last; ) {
0069       if ( ( *i )->mode() == Gaudi::DataHandle::Reader && ( *i )->fullKey() == id ) {
0070         i         = m_handles.erase( i );
0071         renounced = true;
0072       } else {
0073         ++i;
0074       }
0075     }
0076     if ( auto elm = m_inputDataObjs.find( id ); elm != m_inputDataObjs.end() ) {
0077       m_inputDataObjs.erase( elm );
0078       renounced = true;
0079     }
0080     return renounced;
0081   }
0082 
0083   const DataObjIDColl& inputDataObjs() const override { return m_inputDataObjs; }
0084   const DataObjIDColl& outputDataObjs() const override { return m_outputDataObjs; }
0085 
0086   void addDependency( const DataObjID& id, const Gaudi::DataHandle::Mode& mode ) override {
0087     if ( mode & Gaudi::DataHandle::Reader ) m_inputDataObjs.emplace( id );
0088     if ( mode & Gaudi::DataHandle::Writer ) m_outputDataObjs.emplace( id );
0089   }
0090 
0091 protected:
0092   /// initializes all handles - called by the sysInitialize method
0093   /// of any descendant of this
0094   void initDataHandleHolder() {
0095     for ( auto h : m_handles ) h->init();
0096   }
0097 
0098   DataObjIDColl m_inputDataObjs, m_outputDataObjs;
0099 
0100 private:
0101   std::unordered_set<Gaudi::DataHandle*> m_handles;
0102 
0103   Gaudi::Property<DataObjIDColl> m_extInputDataObjs{ this, "ExtraInputs", DataObjIDColl{} };
0104   Gaudi::Property<DataObjIDColl> m_extOutputDataObjs{ this, "ExtraOutputs", DataObjIDColl{} };
0105 };
0106 
0107 #endif // !GAUDIKERNEL_DATAHANDLEHOLDERBASE