The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
EventContext.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 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 <iostream>
18#include <limits>
19
33
35public:
36 using ContextID_t = size_t;
37 using ContextEvt_t = size_t;
38
39 static constexpr ContextID_t INVALID_CONTEXT_ID = std::numeric_limits<ContextID_t>::max();
40 static constexpr ContextEvt_t INVALID_CONTEXT_EVT = std::numeric_limits<ContextEvt_t>::max();
41
42 EventContext() = default;
43
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 void resetExtension() { m_extension.reset(); }
99
100 std::any detachExtension() { return std::move( m_extension ); }
101
102 template <typename T>
103 auto& getExtension() {
104 return std::any_cast<std::decay_t<T>&>( m_extension );
105 }
106
107 template <typename T>
108 const auto& getExtension() const {
109 return std::any_cast<std::decay_t<T> const&>( m_extension );
110 }
111
112 template <typename T>
113 T* tryGetExtension() noexcept {
114 return std::any_cast<T>( &m_extension );
115 }
116
117 template <typename T>
118 const T* tryGetExtension() const noexcept {
119 return std::any_cast<T>( &m_extension );
120 }
121
122 bool hasExtension() const { return m_extension.has_value(); }
123
124 template <typename T>
125 bool hasExtension() const {
126 return hasExtension() && m_extension.type() == typeid( std::decay_t<T> );
127 }
128
129 const std::type_info& getExtensionType() const { return m_extension.type(); }
130
131 friend std::ostream& operator<<( std::ostream& os, const EventContext& ctx );
132 friend std::ostream& operator<<( std::ostream& os, const EventContext* ctx );
133
134private:
139 bool m_valid{ false };
140
141 std::any m_extension;
142};
143
144template <>
145struct fmt::formatter<EventContext> : formatter<string_view> {
146 auto format( const EventContext& ec, format_context& ctx ) const {
147 if ( ec.valid() ) {
148 std::string out;
149 if ( ec.usesSubSlot() ) {
150 out = fmt::format( "s: {} e: {} sub: {}", ec.slot(), ec.evt(), ec.subSlot() );
151 } else {
152 out = fmt::format( "s: {} e: {}", ec.slot(), ec.evt() );
153 }
154 return formatter<string_view>::format( out, ctx );
155 } else {
156 return formatter<string_view>::format( "INVALID", ctx );
157 }
158 }
159};
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
auto format(const EventContext &ec, format_context &ctx) const