R3BROOT
R3B analysis software
Loading...
Searching...
No Matches
R3BUcesbMappingFlag.h
Go to the documentation of this file.
1#pragma once
2/******************************************************************************
3 * Copyright (C) 2019 GSI Helmholtzzentrum für Schwerionenforschung GmbH *
4 * Copyright (C) 2019-2025 Members of R3B Collaboration *
5 * *
6 * This software is distributed under the terms of the *
7 * GNU General Public Licence (GPL) version 3, *
8 * copied verbatim in the file "LICENSE". *
9 * *
10 * In applying this license GSI does not waive the privileges and immunities *
11 * granted to it by virtue of its status as an Intergovernmental Organization *
12 * or submit itself to any jurisdiction. *
13 ******************************************************************************/
14
15#include <bitset>
16#include <cstdint>
17#include <ext_data_client.h>
18#include <fmt/core.h>
19#include <fmt/format.h>
20#include <iostream>
21#include <string>
22#include <type_traits>
23
24constexpr auto UCESB_MAP_BITSIZE = 32;
25
26namespace R3B
27{
28 enum class UcesbMap : uint32_t
29 {
30 zero = 0x0000U,
31 match = EXT_DATA_ITEM_MAP_MATCH,
32 no_dest = EXT_DATA_ITEM_MAP_NO_DEST,
33 not_found = EXT_DATA_ITEM_MAP_NOT_FOUND,
34 type_mismatch = EXT_DATA_ITEM_MAP_TYPE_MISMATCH,
35 ctrl_mismatch = EXT_DATA_ITEM_MAP_CTRL_MISMATCH,
36 array_fewer = EXT_DATA_ITEM_MAP_ARRAY_FEWER,
37 array_more = EXT_DATA_ITEM_MAP_ARRAY_MORE,
38 not_done = EXT_DATA_ITEM_MAP_NOT_DONE,
39 ok = EXT_DATA_ITEM_MAP_OK,
40 ok_no_dest = EXT_DATA_ITEM_MAP_OK_NO_DEST,
41 };
42
43 using UcesbMapUType = std::underlying_type<UcesbMap>::type;
44
45 inline auto operator|(const UcesbMap& left, const UcesbMap& right) -> UcesbMap
46 {
47 auto res = static_cast<UcesbMapUType>(left) | static_cast<UcesbMapUType>(right);
48 return static_cast<UcesbMap>(res);
49 }
50 inline void operator|=(UcesbMap& left, const UcesbMap& right) { left = left | right; }
51
52 inline auto operator&(const UcesbMap& left, const UcesbMap& right) -> UcesbMap
53 {
54 auto res = static_cast<UcesbMapUType>(left) & static_cast<UcesbMapUType>(right);
55 return static_cast<UcesbMap>(res);
56 }
57
58 inline void operator&=(UcesbMap& left, const UcesbMap& right) { left = left & right; }
59
60 inline auto operator~(const UcesbMap& map) -> UcesbMap
61 {
62 return static_cast<UcesbMap>(~(static_cast<UcesbMapUType>(map)));
63 }
64
65 inline auto operator<<(std::ostream& ostream, const UcesbMap& map) -> std::ostream&
66 {
67 ostream << std::bitset<UCESB_MAP_BITSIZE>(static_cast<UcesbMapUType>(map));
68 return ostream;
69 }
70
71 inline auto UcesbMap2String(UcesbMap map) -> std::string
72 {
73 switch (map)
74 {
75 case UcesbMap::zero:
76 return std::string{ "zero" };
77 case UcesbMap::match:
78 return std::string{ "match" };
80 return std::string{ "no_dest" };
82 return std::string{ "not_found" };
84 return std::string{ "type_mismatch" };
86 return std::string{ "ctrl_mismatch" };
88 return std::string{ "array_fewer" };
90 return std::string{ "array_more" };
92 return std::string{ "not_done" };
93 case UcesbMap::ok:
94 return std::string{ "ok" };
96 return std::string{ "ok_no_dest" };
97 default:
98 return fmt::format("", std::bitset<UCESB_MAP_BITSIZE>(static_cast<UcesbMapUType>(map)).to_string());
99 }
100 }
101} // namespace R3B
102
103template <>
104class fmt::formatter<R3B::UcesbMap>
105{
106 public:
107 constexpr auto parse(format_parse_context& ctx)
108 {
109 if (ctx.begin() == ctx.end())
110 {
111 return ctx.end();
112 }
113 const auto* specifier_iter = ctx.begin();
114 if (*specifier_iter == 's' or *specifier_iter == 'b')
115 {
116 presentation_ = *specifier_iter;
117 ++specifier_iter; // NOLINT
118 }
119
120 if (specifier_iter == ctx.end())
121 {
122 throw fmt::format_error("UcesbMap format failed: missing right curly bracket!");
123 }
124
125 if (*specifier_iter != '}')
126 {
127 throw fmt::format_error("UcesbMap format failed: only one speicifier is allowed");
128 }
129 return specifier_iter;
130 }
131
132 template <typename FmtContent>
133 constexpr auto format(const R3B::UcesbMap& flag, FmtContent& ctn) const
134 {
135 switch (presentation_)
136 {
137 default:
138 case 's':
139 return fmt::format_to(ctn.out(), "{}", R3B::UcesbMap2String(flag));
140 case 'b':
141 return fmt::format_to(ctn.out(), "0x{:b}", static_cast<R3B::UcesbMapUType>(flag));
142 }
143 }
144
145 private:
146 char presentation_ = 's';
147};
constexpr auto UCESB_MAP_BITSIZE
constexpr auto parse(format_parse_context &ctx)
constexpr auto format(const R3B::UcesbMap &flag, FmtContent &ctn) const
STL class.
auto operator<<(std::ostream &ostream, const UcesbMap &map) -> std::ostream &
auto operator|(const UcesbMap &left, const UcesbMap &right) -> UcesbMap
void operator|=(UcesbMap &left, const UcesbMap &right)
auto UcesbMap2String(UcesbMap map) -> std::string
auto operator~(const UcesbMap &map) -> UcesbMap
void operator&=(UcesbMap &left, const UcesbMap &right)
auto operator&(const UcesbMap &left, const UcesbMap &right) -> UcesbMap
std::underlying_type< UcesbMap >::type UcesbMapUType