The Gaudi Framework  master (37c0b60a)
EventContext.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 #pragma once
12 
14 #include <any>
15 #include <cstddef>
16 #include <fmt/format.h>
17 #include <iostream>
18 #include <limits>
19 
34 class EventContext {
35 public:
36  using ContextID_t = size_t;
37  using ContextEvt_t = size_t;
38 
41 
42  EventContext() = default;
43 
46  : m_evt_num( e ), m_evt_slot( s ), m_sub_slot( subSlot ) {
48  }
49 
50  ContextEvt_t evt() const { return m_evt_num; }
51  ContextID_t slot() const { return m_evt_slot; }
52  ContextID_t subSlot() const { return m_sub_slot; }
53  bool usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
54  bool valid() const { return m_valid; }
55  const EventIDBase& eventID() const { return m_eid; }
56 
57  void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
60  m_evt_num = e;
61  m_evt_slot = s;
63  }
64 
65  void setEvt( const ContextEvt_t e ) {
66  if ( e == INVALID_CONTEXT_EVT ) setValid( false );
67  m_evt_num = e;
68  }
69 
70  void setSlot( const ContextID_t s ) {
71  if ( s == INVALID_CONTEXT_ID ) setValid( false );
72  m_evt_slot = s;
73  }
74 
75  void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
76 
77  void setValid( const bool b = true ) {
78  m_valid = b;
79  if ( !m_valid ) {
82  }
83  }
84 
85  void setEventID( const EventIDBase& e ) { m_eid = e; }
86 
87  template <typename ValueType, typename... Args>
88  auto& emplaceExtension( Args&&... args ) {
89  return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
90  }
91 
92  template <typename T>
93  auto& setExtension( T&& t ) {
94  m_extension = std::forward<T>( t );
95  return getExtension<T>();
96  }
97 
98  void resetExtension() { m_extension.reset(); }
99 
100  std::any detachExtension() { return std::move( m_extension ); }
101 
102  template <typename T>
103  auto& getExtension() {
104  return std::any_cast<std::decay_t<T>&>( m_extension );
105  }
106 
107  template <typename T>
108  const auto& getExtension() const {
109  return std::any_cast<std::decay_t<T> const&>( m_extension );
110  }
111 
112  template <typename T>
113  T* tryGetExtension() noexcept {
114  return std::any_cast<T>( &m_extension );
115  }
116 
117  template <typename T>
118  const T* tryGetExtension() const noexcept {
119  return std::any_cast<T>( &m_extension );
120  }
121 
122  bool hasExtension() const { return m_extension.has_value(); }
123 
124  template <typename T>
125  bool hasExtension() const {
126  return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
127  }
128 
129  const std::type_info& getExtensionType() const { return m_extension.type(); }
130 
131  friend std::ostream& operator<<( std::ostream& os, const EventContext& ctx );
132  friend std::ostream& operator<<( std::ostream& os, const EventContext* ctx );
133 
134 private:
139  bool m_valid{ false };
140 
141  std::any m_extension;
142 };
143 
144 template <>
145 struct fmt::formatter<EventContext> : formatter<string_view> {
146  auto format( const EventContext& ec, format_context& ctx ) const {
147  if ( ec.valid() ) {
149  if ( ec.usesSubSlot() ) {
150  out = fmt::format( "s: {} e: {} sub: {}", ec.slot(), ec.evt(), ec.subSlot() );
151  } else {
152  out = fmt::format( "s: {} e: {}", ec.slot(), ec.evt() );
153  }
155  } else {
156  return formatter<string_view>::format( "INVALID", ctx );
157  }
158  }
159 };
EventContext::ContextID_t
size_t ContextID_t
Definition: EventContext.h:36
EventContext::emplaceExtension
auto & emplaceExtension(Args &&... args)
Definition: EventContext.h:88
EventContext::valid
bool valid() const
Definition: EventContext.h:54
std::string
STL class.
EventContext::setValid
void setValid(const bool b=true)
Definition: EventContext.h:77
std::move
T move(T... args)
EventContext::setSubSlot
void setSubSlot(const ContextID_t subslot)
Definition: EventContext.h:75
EventContext::eventID
const EventIDBase & eventID() const
Definition: EventContext.h:55
gaudirun.s
string s
Definition: gaudirun.py:346
std::type_info
fmt::formatter< EventContext >::format
auto format(const EventContext &ec, format_context &ctx) const
Definition: EventContext.h:146
EventContext::set
void set(const ContextEvt_t e=0, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:57
EventContext::getExtensionType
const std::type_info & getExtensionType() const
Definition: EventContext.h:129
EventContext::EventContext
EventContext()=default
EventContext::INVALID_CONTEXT_ID
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:39
EventContext::usesSubSlot
bool usesSubSlot() const
Definition: EventContext.h:53
EventContext::m_valid
bool m_valid
Definition: EventContext.h:139
EventContext::setEvt
void setEvt(const ContextEvt_t e)
Definition: EventContext.h:65
EventContext::m_evt_slot
ContextID_t m_evt_slot
Definition: EventContext.h:137
EventContext::m_extension
std::any m_extension
Definition: EventContext.h:141
EventContext::operator<<
friend std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
Definition: EventContext.cpp:13
EventContext::tryGetExtension
const T * tryGetExtension() const noexcept
Definition: EventContext.h:118
EventContext::m_sub_slot
ContextID_t m_sub_slot
Definition: EventContext.h:138
EventContext::getExtension
const auto & getExtension() const
Definition: EventContext.h:108
bug_34121.t
t
Definition: bug_34121.py:31
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
EventContext::tryGetExtension
T * tryGetExtension() noexcept
Definition: EventContext.h:113
std::ostream
STL class.
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:51
EventContext::INVALID_CONTEXT_EVT
static constexpr ContextEvt_t INVALID_CONTEXT_EVT
Definition: EventContext.h:40
EventContext::m_eid
EventIDBase m_eid
Definition: EventContext.h:135
EventContext::m_evt_num
ContextEvt_t m_evt_num
Definition: EventContext.h:136
EventContext::hasExtension
bool hasExtension() const
Definition: EventContext.h:122
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
EventIDBase.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
EventContext::hasExtension
bool hasExtension() const
Definition: EventContext.h:125
EventContext::EventContext
EventContext(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:44
EventContext::resetExtension
void resetExtension()
Definition: EventContext.h:98
EventContext::subSlot
ContextID_t subSlot() const
Definition: EventContext.h:52
gaudirun.args
args
Definition: gaudirun.py:336
EventContext
Definition: EventContext.h:34
EventContext::setExtension
auto & setExtension(T &&t)
Definition: EventContext.h:93
EventIDBase
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:66
EventContext::setSlot
void setSlot(const ContextID_t s)
Definition: EventContext.h:70
std::numeric_limits::max
T max(T... args)
EventContext::setEventID
void setEventID(const EventIDBase &e)
Definition: EventContext.h:85
EventContext::evt
ContextEvt_t evt() const
Definition: EventContext.h:50
EventContext::ContextEvt_t
size_t ContextEvt_t
Definition: EventContext.h:37
EventContext::detachExtension
std::any detachExtension()
Definition: EventContext.h:100
PrepareBase.out
out
Definition: PrepareBase.py:20
EventContext::getExtension
auto & getExtension()
Definition: EventContext.h:103