Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-26 09:01:13

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2024 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 #pragma once
0012 
0013 #include <Gaudi/Interfaces/IFSMCallbackHolder.h>
0014 #include <GaudiKernel/StateMachine.h>
0015 #include <GaudiKernel/extends.h>
0016 
0017 #include <map>
0018 #include <vector>
0019 
0020 namespace Gaudi {
0021 
0022   /** Helper class to implement the IFSMCallbackHolder interface.
0023    *
0024    *  FSMCallbackHolder is used by components base classes (Algorithm, Tool,
0025    *  etc.) to provide a default implementation the IFSMCallbackHolder interface.
0026    *
0027    *  When needing to implement the IFSMCallbackHolder interface in a class, it is
0028    *  enough to wrap the base of the class with FSMCallbackHolder, as in
0029    *
0030    *  \code{.cpp}
0031    *  class MyClass : public FSMCallbackHolder<BaseClass> {
0032    *    // ...
0033    *  };
0034    *  \endcode
0035    */
0036   template <class BASE>
0037   class FSMCallbackHolder : public extends<BASE, IFSMCallbackHolder> {
0038 
0039   public:
0040     using Parent = extends<BASE, IFSMCallbackHolder>;
0041     using extends<BASE, IFSMCallbackHolder>::extends;
0042 
0043     void registerCallBack( StateMachine::Transition s, IFSMCallbackHolder::CallBack c ) override {
0044       m_callbacks[s].push_back( std::move( c ) );
0045     }
0046 
0047     StatusCode sysInitialize() override {
0048       return Parent::sysInitialize().andThen( [&]() { handleCallBacks( StateMachine::INITIALIZE ); } );
0049     }
0050     StatusCode sysStart() override {
0051       return Parent::sysStart().andThen( [&]() { handleCallBacks( StateMachine::START ); } );
0052     }
0053     StatusCode sysStop() override {
0054       return Parent::sysStop().andThen( [&]() { handleCallBacks( StateMachine::STOP ); } );
0055     }
0056     StatusCode sysFinalize() override {
0057       return Parent::sysFinalize().andThen( [&]() { handleCallBacks( StateMachine::FINALIZE ); } );
0058     }
0059 
0060   private:
0061     void handleCallBacks( StateMachine::Transition state ) {
0062       std::for_each( m_callbacks[state].begin(), m_callbacks[state].end(),
0063                      []( IFSMCallbackHolder::CallBack const& c ) { c(); } );
0064     }
0065 
0066     std::map<StateMachine::Transition, std::vector<IFSMCallbackHolder::CallBack>> m_callbacks;
0067   };
0068 
0069 } // namespace Gaudi