The Gaudi Framework  v33r0 (d5ea422b)
EventContext.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 #ifndef GAUDIKERNEL_EVENTCONTEXT_H
12 #define GAUDIKERNEL_EVENTCONTEXT_H 1
13 
15 #include <any>
16 #include <cstddef>
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  template <typename T>
99  auto& getExtension() {
100  return std::any_cast<std::decay_t<T>&>( m_extension );
101  }
102 
103  template <typename T>
104  const auto& getExtension() const {
105  return std::any_cast<std::decay_t<T> const&>( m_extension );
106  }
107 
108  bool hasExtension() const { return m_extension.has_value(); }
109 
110  template <typename T>
111  bool hasExtension() const {
112  return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
113  }
114 
115  const std::type_info& getExtensionType() const { return m_extension.type(); }
116 
117 private:
122  bool m_valid{false};
123 
124  std::any m_extension;
125 };
126 
128  if ( ctx.valid() ) {
129  os << "s: " << ctx.slot() << " e: " << ctx.evt();
130  if ( ctx.usesSubSlot() ) os << " sub: " << ctx.subSlot();
131  return os;
132  } else {
133  return os << "INVALID";
134  }
135 }
136 
138  if ( c ) {
139  return os << *c;
140  } else {
141  return os << "INVALID";
142  }
143 }
144 
145 #endif // GAUDIKERNEL_EVENTCONTEXT_H
void setEvt(const ContextEvt_t e)
Definition: EventContext.h:65
ContextID_t slot() const
Definition: EventContext.h:51
ContextEvt_t evt() const
Definition: EventContext.h:50
void setEventID(const EventIDBase &e)
Definition: EventContext.h:85
ContextID_t m_evt_slot
Definition: EventContext.h:120
size_t ContextID_t
Definition: EventContext.h:36
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
bool valid() const
Definition: EventContext.h:54
const auto & getExtension() const
Definition: EventContext.h:104
auto & emplaceExtension(Args &&... args)
Definition: EventContext.h:88
auto & getExtension()
Definition: EventContext.h:99
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:40
auto & setExtension(T &&t)
Definition: EventContext.h:93
const std::type_info & getExtensionType() const
Definition: EventContext.h:115
size_t ContextEvt_t
Definition: EventContext.h:37
EventIDBase m_eid
Definition: EventContext.h:118
static constexpr ContextID_t INVALID_CONTEXT_ID
Definition: EventContext.h:39
void setSubSlot(const ContextID_t subslot)
Definition: EventContext.h:75
void setSlot(const ContextID_t s)
Definition: EventContext.h:70
T max(T... args)
std::any m_extension
Definition: EventContext.h:124
ContextID_t subSlot() const
Definition: EventContext.h:52
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(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
Definition: EventContext.h:44
ContextEvt_t m_evt_num
Definition: EventContext.h:119
ContextID_t m_sub_slot
Definition: EventContext.h:121
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition: EventIDBase.h:66
string s
Definition: gaudirun.py:328
bool hasExtension() const
Definition: EventContext.h:108
void setValid(const bool b=true)
Definition: EventContext.h:77
std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
Definition: EventContext.h:127
STL class.
EventContext()=default
bool hasExtension() const
Definition: EventContext.h:111
const EventIDBase & eventID() const
Definition: EventContext.h:55
bool usesSubSlot() const
Definition: EventContext.h:53