Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
EventContext.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_EVENTCONTEXT_H
2 #define GAUDIKERNEL_EVENTCONTEXT_H 1
3 
4 #if __cplusplus >= 201703
5 # include <any>
6 namespace evt_context_detail = std;
7 #else
8 # include <boost/any.hpp>
9 namespace evt_context_detail = boost;
10 #endif
11 
13 #include <cstddef>
14 #include <iostream>
15 #include <limits>
16 
31 class EventContext {
32 public:
33  using ContextID_t = size_t;
34  using ContextEvt_t = size_t;
35 
38 
39  EventContext() = default;
40 
41  EventContext( const ContextEvt_t e, const ContextID_t s = INVALID_CONTEXT_ID,
42  const ContextID_t subSlot = INVALID_CONTEXT_ID )
43  : m_evt_num( e ), m_evt_slot( s ), m_sub_slot( subSlot ) {
44  m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
45  }
46 
47  ContextEvt_t evt() const { return m_evt_num; }
48  ContextID_t slot() const { return m_evt_slot; }
49  ContextID_t subSlot() const { return m_sub_slot; }
50  bool usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
51  bool valid() const { return m_valid; }
52  const EventIDBase& eventID() const { return m_eid; }
53 
54  void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
56  m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
57  m_evt_num = e;
58  m_evt_slot = s;
60  }
61 
62  void setEvt( const ContextEvt_t e ) {
63  if ( e == INVALID_CONTEXT_EVT ) setValid( false );
64  m_evt_num = e;
65  }
66 
67  void setSlot( const ContextID_t s ) {
68  if ( s == INVALID_CONTEXT_ID ) setValid( false );
69  m_evt_slot = s;
70  }
71 
72  void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
73 
74  void setValid( const bool b = true ) {
75  m_valid = b;
76  if ( !m_valid ) {
79  }
80  }
81 
82  void setEventID( const EventIDBase& e ) { m_eid = e; }
83 
84  template <typename ValueType, typename... Args>
85  auto& emplaceExtension( Args&&... args ) {
86 #if __cplusplus >= 201703
87  return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
88 #else
89  return setExtension( ValueType( std::forward<Args>( args )... ) );
90 #endif
91  }
92 
93  template <typename T>
94  auto& setExtension( T&& t ) {
95  m_extension = std::forward<T>( t );
96  return getExtension<T>();
97  }
98 
99  template <typename T>
100  auto& getExtension() {
101  return ::evt_context_detail::any_cast<std::decay_t<T>&>( m_extension );
102  }
103 
104  template <typename T>
105  const auto& getExtension() const {
106  return ::evt_context_detail::any_cast<std::decay_t<T> const&>( m_extension );
107  }
108 
109  bool hasExtension() const {
110 #if __cplusplus >= 201703
111  return m_extension.has_value();
112 #else
113  return !m_extension.empty();
114 #endif
115  }
116 
117  template <typename T>
118  bool hasExtension() const {
119  return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
120  }
121 
122  const std::type_info& getExtensionType() const { return m_extension.type(); }
123 
124 private:
126  ContextEvt_t m_evt_num{INVALID_CONTEXT_EVT};
127  ContextID_t m_evt_slot{INVALID_CONTEXT_ID};
128  ContextID_t m_sub_slot{INVALID_CONTEXT_ID};
129  bool m_valid{false};
130 
131  ::evt_context_detail::any m_extension;
132 };
133 
134 inline std::ostream& operator<<( std::ostream& os, const EventContext& ctx ) {
135  if ( ctx.valid() ) {
136  os << "s: " << ctx.slot() << " e: " << ctx.evt();
137  if ( ctx.usesSubSlot() ) os << " sub: " << ctx.subSlot();
138  return os;
139  } else {
140  return os << "INVALID";
141  }
142 }
143 
145  if ( c ) {
146  return os << *c;
147  } else {
148  return os << "INVALID";
149  }
150 }
151 
152 #endif // GAUDIKERNEL_EVENTCONTEXT_H
void setEvt(const ContextEvt_t e)
Definition: EventContext.h:62
bool hasExtension() const
Definition: EventContext.h:118
void setEventID(const EventIDBase &e)
Definition: EventContext.h:82
ContextID_t slot() const
Definition: EventContext.h:48
const auto & getExtension() const
Definition: EventContext.h:105
ContextID_t m_evt_slot
Definition: EventContext.h:127
auto & emplaceExtension(Args &&...args)
Definition: EventContext.h:85
bool usesSubSlot() const
Definition: EventContext.h:50
size_t ContextID_t
Definition: EventContext.h:33
This class represents an entry point to all the event specific data.
Definition: EventContext.h:31
ContextEvt_t evt() const
Definition: EventContext.h:47
auto & getExtension()
Definition: EventContext.h:100
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
static constexpr ContextEvt_t INVALID_CONTEXT_EVT
Definition: EventContext.h:37
auto & setExtension(T &&t)
Definition: EventContext.h:94
size_t ContextEvt_t
Definition: EventContext.h:34
EventIDBase m_eid
Definition: EventContext.h:125
const std::type_info & getExtensionType() const
Definition: EventContext.h:122
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:36
void setSubSlot(const ContextID_t subslot)
Definition: EventContext.h:72
void setSlot(const ContextID_t s)
Definition: EventContext.h:67
EventContext(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:41
ContextEvt_t m_evt_num
Definition: EventContext.h:126
ContextID_t m_sub_slot
Definition: EventContext.h:128
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:56
string s
Definition: gaudirun.py:312
const EventIDBase & eventID() const
Definition: EventContext.h:52
bool hasExtension() const
Definition: EventContext.h:109
void setValid(const bool b=true)
Definition: EventContext.h:74
ContextID_t subSlot() const
Definition: EventContext.h:49
::evt_context_detail::any m_extension
Definition: EventContext.h:131
std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
Definition: EventContext.h:134
STL class.
EventContext()=default
bool valid() const
Definition: EventContext.h:51