The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
EventContext.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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 <format>
18#include <iostream>
19#include <limits>
20
34
36public:
37 using ContextID_t = size_t;
38 using ContextEvt_t = size_t;
39
40 static constexpr ContextID_t INVALID_CONTEXT_ID = std::numeric_limits<ContextID_t>::max();
41 static constexpr ContextEvt_t INVALID_CONTEXT_EVT = std::numeric_limits<ContextEvt_t>::max();
42
43 EventContext() = default;
44
50
51 ContextEvt_t evt() const { return m_evt_num; }
52 ContextID_t slot() const { return m_evt_slot; }
53 ContextID_t subSlot() const { return m_sub_slot; }
54 bool usesSubSlot() const { return m_sub_slot != INVALID_CONTEXT_ID; }
55 bool valid() const { return m_valid; }
56 const EventIDBase& eventID() const { return m_eid; }
57
58 void set( const ContextEvt_t e = 0, const ContextID_t s = INVALID_CONTEXT_ID,
61 m_evt_num = e;
62 m_evt_slot = s;
64 }
65
66 void setEvt( const ContextEvt_t e ) {
67 if ( e == INVALID_CONTEXT_EVT ) setValid( false );
68 m_evt_num = e;
69 }
70
71 void setSlot( const ContextID_t s ) {
72 if ( s == INVALID_CONTEXT_ID ) setValid( false );
73 m_evt_slot = s;
74 }
75
76 void setSubSlot( const ContextID_t subslot ) { m_sub_slot = subslot; }
77
78 void setValid( const bool b = true ) {
79 m_valid = b;
80 if ( !m_valid ) {
83 }
84 }
85
86 void setEventID( const EventIDBase& e ) { m_eid = e; }
87
88 template <typename ValueType, typename... Args>
89 auto& emplaceExtension( Args&&... args ) {
90 return m_extension.emplace<ValueType>( std::forward<Args>( args )... );
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 void resetExtension() { m_extension.reset(); }
100
101 std::any detachExtension() { return std::move( m_extension ); }
102
103 template <typename T>
104 auto& getExtension() {
105 return std::any_cast<std::decay_t<T>&>( m_extension );
106 }
107
108 template <typename T>
109 const auto& getExtension() const {
110 return std::any_cast<std::decay_t<T> const&>( m_extension );
111 }
112
113 template <typename T>
114 T* tryGetExtension() noexcept {
115 return std::any_cast<T>( &m_extension );
116 }
117
118 template <typename T>
119 const T* tryGetExtension() const noexcept {
120 return std::any_cast<T>( &m_extension );
121 }
122
123 bool hasExtension() const { return m_extension.has_value(); }
124
125 template <typename T>
126 bool hasExtension() const {
127 return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
128 }
129
130 const std::type_info& getExtensionType() const { return m_extension.type(); }
131
132 friend std::ostream& operator<<( std::ostream& os, const EventContext& ctx );
133 friend std::ostream& operator<<( std::ostream& os, const EventContext* ctx );
134
135private:
140 bool m_valid{ false };
141
142 std::any m_extension;
143};
144
147template <class BASE>
149 auto format( const EventContext& ec, auto& ctx ) const {
150 if ( ec.valid() ) {
151 std::string out;
152 if ( ec.usesSubSlot() ) {
153 out = std::format( "s: {} e: {} sub: {}", ec.slot(), ec.evt(), ec.subSlot() );
154 } else {
155 out = std::format( "s: {} e: {}", ec.slot(), ec.evt() );
156 }
157 return BASE::format( out, ctx );
158
159 } else {
160 return BASE::format( "INVALID", ctx );
161 }
162 }
163};
164
165template <>
166struct std::formatter<EventContext> : EventContextFormatter<std::formatter<std::string_view>> {};
167
168template <>
169struct fmt::formatter<EventContext> : EventContextFormatter<fmt::formatter<std::string_view>> {};
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
This class represents an entry point to all the event specific data.
EventContext()=default
ContextEvt_t evt() const
const EventIDBase & eventID() const
auto & emplaceExtension(Args &&... args)
void setSubSlot(const ContextID_t subslot)
void setValid(const bool b=true)
ContextID_t subSlot() const
EventContext(const ContextEvt_t e, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
ContextID_t slot() const
bool hasExtension() const
const auto & getExtension() const
bool hasExtension() const
void set(const ContextEvt_t e=0, const ContextID_t s=INVALID_CONTEXT_ID, const ContextID_t subSlot=INVALID_CONTEXT_ID)
ContextID_t m_sub_slot
const T * tryGetExtension() const noexcept
bool valid() const
ContextID_t m_evt_slot
bool usesSubSlot() const
static constexpr ContextEvt_t INVALID_CONTEXT_EVT
friend std::ostream & operator<<(std::ostream &os, const EventContext &ctx)
const std::type_info & getExtensionType() const
void setEventID(const EventIDBase &e)
auto & getExtension()
std::any detachExtension()
void setEvt(const ContextEvt_t e)
void resetExtension()
auto & setExtension(T &&t)
void setSlot(const ContextID_t s)
ContextEvt_t m_evt_num
size_t ContextID_t
EventIDBase m_eid
T * tryGetExtension() noexcept
static constexpr ContextID_t INVALID_CONTEXT_ID
std::any m_extension
size_t ContextEvt_t
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
Definition EventIDBase.h:65
Custom formatter for EventContext to support std::format and fmt.
auto format(const EventContext &ec, auto &ctx) const