The Gaudi Framework  v30r4 (9b837755)
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 
32 {
33 public:
34  using ContextID_t = size_t;
35  using ContextEvt_t = size_t;
36 
39 
40  EventContext() = default;
41 
42  EventContext( const ContextEvt_t e, const ContextID_t s = INVALID_CONTEXT_ID,
43  const ContextID_t subSlot = INVALID_CONTEXT_ID )
44  : m_evt_num( e ), m_evt_slot( s ), m_sub_slot( subSlot )
45  {
46  m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
47  }
48 
49  ContextEvt_t evt() const { return m_evt_num; }
50  ContextID_t slot() const { return m_evt_slot; }
51  ContextID_t subSlot() const { return m_sub_slot; }
52  bool usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
53  bool valid() const { return m_valid; }
54  const EventIDBase& eventID() const { return m_eid; }
55 
56  void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
58  {
59  m_valid = ( e != INVALID_CONTEXT_EVT && s != INVALID_CONTEXT_ID );
60  m_evt_num = e;
61  m_evt_slot = s;
63  }
64 
65  void setEvt( const ContextEvt_t e )
66  {
67  if ( e == INVALID_CONTEXT_EVT ) setValid( false );
68  m_evt_num = e;
69  }
70 
71  void setSlot( const ContextID_t s )
72  {
73  if ( s == INVALID_CONTEXT_ID ) setValid( false );
74  m_evt_slot = s;
75  }
76 
77  void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
78 
79  void setValid( const bool b = true )
80  {
81  m_valid = b;
82  if ( !m_valid ) {
85  }
86  }
87 
88  void setEventID( const EventIDBase& e ) { m_eid = e; }
89 
90  template <typename ValueType, typename... Args>
91  auto& emplaceExtension( Args&&... args )
92  {
93 #if __cplusplus >= 201703
94  return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
95 #else
96  return setExtension( ValueType( std::forward<Args>( args )... ) );
97 #endif
98  }
99 
100  template <typename T>
101  auto& setExtension( T&& t )
102  {
103  m_extension = std::forward<T>( t );
104  return getExtension<T>();
105  }
106 
107  template <typename T>
108  auto& getExtension()
109  {
110  return ::evt_context_detail::any_cast<std::decay_t<T>&>( m_extension );
111  }
112 
113  template <typename T>
114  const auto& getExtension() const
115  {
116  return ::evt_context_detail::any_cast<std::decay_t<T> const&>( m_extension );
117  }
118 
119  bool hasExtension() const
120  {
121 #if __cplusplus >= 201703
122  return m_extension.has_value();
123 #else
124  return !m_extension.empty();
125 #endif
126  }
127 
128  template <typename T>
129  bool hasExtension() const
130  {
131  return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
132  }
133 
134  const std::type_info& getExtensionType() const { return m_extension.type(); }
135 
136 private:
138  ContextEvt_t m_evt_num{INVALID_CONTEXT_EVT};
139  ContextID_t m_evt_slot{INVALID_CONTEXT_ID};
140  ContextID_t m_sub_slot{INVALID_CONTEXT_ID};
141  bool m_valid{false};
142 
143  ::evt_context_detail::any m_extension;
144 };
145 
147 {
148  if ( ctx.valid() ) {
149  os << "s: " << ctx.slot() << " e: " << ctx.evt();
150  if ( ctx.usesSubSlot() ) os << " sub: " << ctx.subSlot();
151  return os;
152  } else {
153  return os << "INVALID";
154  }
155 }
156 
158 {
159  if ( c ) {
160  return os << *c;
161  } else {
162  return os << "INVALID";
163  }
164 }
165 
166 #endif // GAUDIKERNEL_EVENTCONTEXT_H
void setEvt(const ContextEvt_t e)
Definition: EventContext.h:65
bool hasExtension() const
Definition: EventContext.h:129
void setEventID(const EventIDBase &e)
Definition: EventContext.h:88
ContextID_t slot() const
Definition: EventContext.h:50
const auto & getExtension() const
Definition: EventContext.h:114
ContextID_t m_evt_slot
Definition: EventContext.h:139
auto & emplaceExtension(Args &&...args)
Definition: EventContext.h:91
bool usesSubSlot() const
Definition: EventContext.h:52
size_t ContextID_t
Definition: EventContext.h:34
This class represents an entry point to all the event specific data.
Definition: EventContext.h:31
ContextEvt_t evt() const
Definition: EventContext.h:49
auto & getExtension()
Definition: EventContext.h:108
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:38
auto & setExtension(T &&t)
Definition: EventContext.h:101
size_t ContextEvt_t
Definition: EventContext.h:35
EventIDBase m_eid
Definition: EventContext.h:137
const std::type_info & getExtensionType() const
Definition: EventContext.h:134
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:37
void setSubSlot(const ContextID_t subslot)
Definition: EventContext.h:77
void setSlot(const ContextID_t s)
Definition: EventContext.h:71
EventContext(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:42
ContextEvt_t m_evt_num
Definition: EventContext.h:138
ContextID_t m_sub_slot
Definition: EventContext.h:140
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:60
string s
Definition: gaudirun.py:253
const EventIDBase & eventID() const
Definition: EventContext.h:54
bool hasExtension() const
Definition: EventContext.h:119
void setValid(const bool b=true)
Definition: EventContext.h:79
ContextID_t subSlot() const
Definition: EventContext.h:51
::evt_context_detail::any m_extension
Definition: EventContext.h:143
std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
Definition: EventContext.h:146
STL class.
EventContext()=default
bool valid() const
Definition: EventContext.h:53