| The Gaudi Framework v32r1 (f65d50dc)

EventContext.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_EVENTCONTEXT_H
2 #define GAUDIKERNEL_EVENTCONTEXT_H 1
3
4 #include "GaudiKernel/EventIDBase.h"
5 #include <any>
6 #include <cstddef>
7 #include <iostream>
8 #include <limits>
9
24 class EventContext {
25 public:
26 using ContextID_t = size_t;
27 using ContextEvt_t = size_t;
28
29 static constexpr ContextID_t INVALID_CONTEXT_ID = std::numeric_limits<ContextID_t>::max();
30 static constexpr ContextEvt_t INVALID_CONTEXT_EVT = std::numeric_limits<ContextEvt_t>::max();
31
32 EventContext() = default;
33
34 EventContext( const ContextEvt_t e, const ContextID_t s = INVALID_CONTEXT_ID,
35 const ContextID_t subSlot = INVALID_CONTEXT_ID )
36 : m_evt_num( e ), m_evt_slot( s ), m_sub_slot( subSlot ) {
37 m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
38 }
39
40 ContextEvt_t evt() const { return m_evt_num; }
41 ContextID_t slot() const { return m_evt_slot; }
42 ContextID_t subSlot() const { return m_sub_slot; }
43 bool usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
44 bool valid() const { return m_valid; }
45 const EventIDBase& eventID() const { return m_eid; }
46
47 void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
48 const ContextID_t subSlot = INVALID_CONTEXT_ID ) {
49 m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
50 m_evt_num = e;
51 m_evt_slot = s;
52 m_sub_slot = subSlot;
53 }
54
55 void setEvt( const ContextEvt_t e ) {
56 if ( e == INVALID_CONTEXT_EVT ) setValid( false );
57 m_evt_num = e;
58 }
59
60 void setSlot( const ContextID_t s ) {
61 if ( s == INVALID_CONTEXT_ID ) setValid( false );
62 m_evt_slot = s;
63 }
64
65 void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
66
67 void setValid( const bool b = true ) {
68 m_valid = b;
69 if ( !m_valid ) {
70 m_evt_slot = INVALID_CONTEXT_ID;
71 m_evt_num = INVALID_CONTEXT_EVT;
72 }
73 }
74
75 void setEventID( const EventIDBase& e ) { m_eid = e; }
76
77 template <typename ValueType, typename... Args>
78 auto& emplaceExtension( Args&&... args ) {
79 return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
80 }
81
82 template <typename T>
83 auto& setExtension( T&& t ) {
84 m_extension = std::forward<T>( t );
85 return getExtension<T>();
86 }
87
88 template <typename T>
89 auto& getExtension() {
90 return std::any_cast<std::decay_t<T>&>( m_extension );
91 }
92
93 template <typename T>
94 const auto& getExtension() const {
95 return std::any_cast<std::decay_t<T> const&>( m_extension );
96 }
97
98 bool hasExtension() const { return m_extension.has_value(); }
99
100 template <typename T>
101 bool hasExtension() const {
102 return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
103 }
104
105 const std::type_info& getExtensionType() const { return m_extension.type(); }
106
107 private:
108 EventIDBase m_eid{};
109 ContextEvt_t m_evt_num{INVALID_CONTEXT_EVT};
110 ContextID_t m_evt_slot{INVALID_CONTEXT_ID};
111 ContextID_t m_sub_slot{INVALID_CONTEXT_ID};
112 bool m_valid{false};
113
114 std::any m_extension;
115 };
116
117 inline std::ostream& operator<<( std::ostream& os, const EventContext& ctx ) {
118 if ( ctx.valid() ) {
119 os << "s: " << ctx.slot() << " e: " << ctx.evt();
120 if ( ctx.usesSubSlot() ) os << " sub: " << ctx.subSlot();
121 return os;
122 } else {
123 return os << "INVALID";
124 }
125 }
126
127 inline std::ostream& operator<<( std::ostream& os, const EventContext* c ) {
128 if ( c ) {
129 return os << *c;
130 } else {
131 return os << "INVALID";
132 }
133 }
134
135 #endif // GAUDIKERNEL_EVENTCONTEXT_H
EventContext::m_valid
bool m_valid
Definition: EventContext.h:112
EventContext::setEvt
void setEvt(const ContextEvt_t e)
Definition: EventContext.h:55
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:41
gaudirun.c
c
Definition: gaudirun.py:487
EventContext::evt
ContextEvt_t evt() const
Definition: EventContext.h:40
EventContext::setEventID
void setEventID(const EventIDBase &e)
Definition: EventContext.h:75
EventContext::m_evt_slot
ContextID_t m_evt_slot
Definition: EventContext.h:110
EventContext::ContextID_t
size_t ContextID_t
Definition: EventContext.h:26
EventContext
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
EventContext::valid
bool valid() const
Definition: EventContext.h:44
EventContext::getExtension
const auto & getExtension() const
Definition: EventContext.h:94
EventContext::emplaceExtension
auto & emplaceExtension(Args &&... args)
Definition: EventContext.h:78
std::type_info
EventContext::getExtension
auto & getExtension()
Definition: EventContext.h:89
EventIDBase.h
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
EventContext::INVALID_CONTEXT_EVT
static constexpr ContextEvt_t INVALID_CONTEXT_EVT
Definition: EventContext.h:30
EventContext::setExtension
auto & setExtension(T &&t)
Definition: EventContext.h:83
EventContext::getExtensionType
const std::type_info & getExtensionType() const
Definition: EventContext.h:105
gaudirun.args
args
Definition: gaudirun.py:307
EventContext::ContextEvt_t
size_t ContextEvt_t
Definition: EventContext.h:27
EventContext::m_eid
EventIDBase m_eid
Definition: EventContext.h:108
EventContext::INVALID_CONTEXT_ID
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:29
EventContext::setSubSlot
void setSubSlot(const ContextID_t subslot)
Definition: EventContext.h:65
EventContext::setSlot
void setSlot(const ContextID_t s)
Definition: EventContext.h:60
std::numeric_limits::max
T max(T... args)
EventContext::m_extension
std::any m_extension
Definition: EventContext.h:114
EventContext::subSlot
ContextID_t subSlot() const
Definition: EventContext.h:42
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:47
EventContext::EventContext
EventContext(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:34
EventContext::m_evt_num
ContextEvt_t m_evt_num
Definition: EventContext.h:109
EventContext::m_sub_slot
ContextID_t m_sub_slot
Definition: EventContext.h:111
EventIDBase
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:56
gaudirun.s
string s
Definition: gaudirun.py:316
EventContext::hasExtension
bool hasExtension() const
Definition: EventContext.h:98
EventContext::setValid
void setValid(const bool b=true)
Definition: EventContext.h:67
operator<<
std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
Definition: EventContext.h:117
std::ostream
STL class.
EventContext::EventContext
EventContext()=default
EventContext::hasExtension
bool hasExtension() const
Definition: EventContext.h:101
EventContext::eventID
const EventIDBase & eventID() const
Definition: EventContext.h:45
EventContext::usesSubSlot
bool usesSubSlot() const
Definition: EventContext.h:43