The Gaudi Framework  master (37c0b60a)
IBinder.h
Go to the documentation of this file.
1 /*****************************************************************************\
2 * (c) Copyright 2021-2024 CERN for the benefit of the LHCb Collaboration *
3 * *
4 * This software is distributed under the terms of the GNU General Public *
5 * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
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 #include <GaudiKernel/IAlgTool.h>
13 #include <GaudiKernel/IInterface.h>
14 #include <type_traits>
15 #include <utility>
16 
17 class EventContext;
18 
20 
21  // see https://godbolt.org/z/KPMYd1sbr
22  template <typename IFace>
23  class Box final {
24  const IFace* m_ptr = nullptr;
25  void ( *m_destruct )( void* ) = nullptr;
26  std::aligned_storage_t<64 - 2 * sizeof( void* )> m_storage; // local storage for bound arguments... fit into a
27  // cacheline
28  public:
29  // identity binding: no actual binding is required...
30  Box( IFace const* ptr ) : m_ptr{ ptr } { assert( m_ptr != nullptr ); }
31  // bind the arguments...
32  template <typename Ret, typename... Args, typename = std::enable_if_t<std::is_base_of_v<IFace, Ret>>>
33  Box( std::in_place_type_t<Ret>, Args&&... args ) {
34  if constexpr ( sizeof( Ret ) <= sizeof( m_storage ) ) {
35  m_ptr = new ( &m_storage ) Ret{ std::forward<Args>( args )... };
36  m_destruct = []( void* ptr ) { static_cast<Ret*>( ptr )->~Ret(); };
37  } else {
38  m_ptr = new Ret{ std::forward<Args>( args )... };
39  m_destruct = []( void* ptr ) { delete static_cast<Ret*>( ptr ); };
40  }
41  }
42 
43  ~Box() {
44  if ( m_destruct ) ( *m_destruct )( const_cast<IFace*>( m_ptr ) );
45  }
46  Box( const Box& ) = delete;
47  Box& operator=( const Box& ) = delete;
48  Box( Box&& rhs ) = delete;
49  Box& operator=( Box&& ) = delete;
50 
51  operator IFace const&() const { return *m_ptr; }
52  // operator IFace&() && = delete;
53  };
54 
55  template <typename IFace>
56  struct IBinder : extend_interfaces<IAlgTool> {
58  virtual Box<IFace> bind( const EventContext& ctx ) const = 0;
59  };
60 
61  template <typename IFace>
62  struct AlgToolStub : IFace {
63 
64  using IFace::IFace;
65  AlgToolStub( const AlgToolStub& ) = delete;
66  AlgToolStub& operator=( const AlgToolStub& ) = delete;
67  AlgToolStub( AlgToolStub&& ) = delete;
69 
70  const std::string& name() const override {
71  static std::string s{ "<STUB>" };
72  return s;
73  }
74  const std::string& type() const override { return name(); }
75  const IInterface* parent() const override { return nullptr; }
76 
77  StatusCode configure() override { return StatusCode::FAILURE; }
78  StatusCode initialize() override { return StatusCode::FAILURE; }
79  StatusCode start() override { return StatusCode::FAILURE; }
80  StatusCode stop() override { return StatusCode::FAILURE; }
81  StatusCode finalize() override { return StatusCode::FAILURE; }
82  StatusCode terminate() override { return StatusCode::FAILURE; }
84  StatusCode restart() override { return StatusCode::FAILURE; }
87  StatusCode sysStart() override { return StatusCode::FAILURE; }
88  StatusCode sysStop() override { return StatusCode::FAILURE; }
91  StatusCode sysRestart() override { return StatusCode::FAILURE; }
92  };
93 
94  template <typename IFace>
95  struct Stub : implements<AlgToolStub<IFace>> {};
96 
97 } // namespace Gaudi::Interface::Bind
Gaudi::Interface::Bind::Box::operator=
Box & operator=(const Box &)=delete
Gaudi::Interface::Bind::AlgToolStub::operator=
AlgToolStub & operator=(const AlgToolStub &)=delete
Gaudi::Interface::Bind::Box::Box
Box(Box &&rhs)=delete
std::string
STL class.
Gaudi::Interface::Bind::AlgToolStub::sysStart
StatusCode sysStart() override
Definition: IBinder.h:87
Gaudi::Interface::Bind::Box::m_ptr
const IFace * m_ptr
Definition: IBinder.h:24
gaudirun.s
string s
Definition: gaudirun.py:346
Gaudi::Interface::Bind::Box
Definition: IBinder.h:23
extend_interfaces
Base class to be used to extend an interface.
Definition: extend_interfaces.h:15
Gaudi::Interface::Bind::AlgToolStub::stop
StatusCode stop() override
Definition: IBinder.h:80
Gaudi::Interface::Bind::AlgToolStub::type
const std::string & type() const override
Definition: IBinder.h:74
Gaudi::Interface::Bind::AlgToolStub::finalize
StatusCode finalize() override
Definition: IBinder.h:81
Gaudi::Interface::Bind::Box::operator=
Box & operator=(Box &&)=delete
Gaudi::StateMachine::State
State
Allowed states for classes implementing the state machine (ApplicationMgr, Algorithm,...
Definition: StateMachine.h:22
Gaudi::Interface::Bind::AlgToolStub::AlgToolStub
AlgToolStub(AlgToolStub &&)=delete
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
StatusCode
Definition: StatusCode.h:65
IInterface.h
Gaudi::Interface::Bind
Definition: IBinder.h:19
Gaudi::Interface::Bind::AlgToolStub::sysReinitialize
StatusCode sysReinitialize() override
Definition: IBinder.h:90
IAlgTool.h
Gaudi::Interface::Bind::AlgToolStub::reinitialize
StatusCode reinitialize() override
Definition: IBinder.h:83
Gaudi::Interface::Bind::AlgToolStub::FSMState
Gaudi::StateMachine::State FSMState() const override
Definition: IBinder.h:85
Gaudi::Interface::Bind::AlgToolStub::configure
StatusCode configure() override
Definition: IBinder.h:77
Gaudi::Interface::Bind::AlgToolStub::terminate
StatusCode terminate() override
Definition: IBinder.h:82
Gaudi::Interface::Bind::AlgToolStub::parent
const IInterface * parent() const override
Definition: IBinder.h:75
Gaudi::Interface::Bind::AlgToolStub::start
StatusCode start() override
Definition: IBinder.h:79
Gaudi::Interface::Bind::IBinder::DeclareInterfaceID
DeclareInterfaceID(IBinder, 1, 0)
Gaudi::StateMachine::RUNNING
@ RUNNING
Definition: StateMachine.h:26
Gaudi::Interface::Bind::Stub
Definition: IBinder.h:95
Gaudi::Interface::Bind::Box::Box
Box(const Box &)=delete
Gaudi::Interface::Bind::AlgToolStub::initialize
StatusCode initialize() override
Definition: IBinder.h:78
implements
Base class used to implement the interfaces.
Definition: implements.h:19
Gaudi::Interface::Bind::Box::Box
Box(IFace const *ptr)
Definition: IBinder.h:30
Gaudi::Interface::Bind::AlgToolStub
Definition: IBinder.h:62
Gaudi::Interface::Bind::Box::Box
Box(std::in_place_type_t< Ret >, Args &&... args)
Definition: IBinder.h:33
Gaudi::Interface::Bind::IBinder::bind
virtual Box< IFace > bind(const EventContext &ctx) const =0
Gaudi::Interface::Bind::AlgToolStub::operator=
AlgToolStub & operator=(AlgToolStub &&)=delete
gaudirun.args
args
Definition: gaudirun.py:336
IInterface
Definition: IInterface.h:239
EventContext
Definition: EventContext.h:34
Gaudi::Interface::Bind::Box::m_storage
std::aligned_storage_t< 64 - 2 *sizeof(void *)> m_storage
Definition: IBinder.h:26
Gaudi::Interface::Bind::AlgToolStub::sysStop
StatusCode sysStop() override
Definition: IBinder.h:88
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Gaudi::Interface::Bind::AlgToolStub::sysFinalize
StatusCode sysFinalize() override
Definition: IBinder.h:89
Gaudi::Interface::Bind::Box::m_destruct
void(* m_destruct)(void *)
Definition: IBinder.h:25
Gaudi::Interface::Bind::AlgToolStub::name
const std::string & name() const override
Definition: IBinder.h:70
Gaudi::Interface::Bind::AlgToolStub::sysRestart
StatusCode sysRestart() override
Definition: IBinder.h:91
Gaudi::Interface::Bind::AlgToolStub::sysInitialize
StatusCode sysInitialize() override
Definition: IBinder.h:86
Gaudi::Interface::Bind::AlgToolStub::restart
StatusCode restart() override
Definition: IBinder.h:84
Gaudi::Interface::Bind::IBinder
Definition: IBinder.h:56
Gaudi::Interface::Bind::AlgToolStub::AlgToolStub
AlgToolStub(const AlgToolStub &)=delete
Gaudi::Interface::Bind::Box::~Box
~Box()
Definition: IBinder.h:43