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/format.h>
19#include <iostream>
20
21constexpr auto UCESB_MAP_BITSIZE = 32;
22
23namespace R3B
24{
25 enum class UcesbMap : uint32_t
26 {
27 zero = 0x0000U,
28 match = EXT_DATA_ITEM_MAP_MATCH,
29 no_dest = EXT_DATA_ITEM_MAP_NO_DEST,
30 not_found = EXT_DATA_ITEM_MAP_NOT_FOUND,
31 type_mismatch = EXT_DATA_ITEM_MAP_TYPE_MISMATCH,
32 ctrl_mismatch = EXT_DATA_ITEM_MAP_CTRL_MISMATCH,
33 array_fewer = EXT_DATA_ITEM_MAP_ARRAY_FEWER,
34 array_more = EXT_DATA_ITEM_MAP_ARRAY_MORE,
35 not_done = EXT_DATA_ITEM_MAP_NOT_DONE,
36 ok = EXT_DATA_ITEM_MAP_OK,
37 ok_no_dest = EXT_DATA_ITEM_MAP_OK_NO_DEST,
38 };
39
40 using UcesbMapUType = std::underlying_type<UcesbMap>::type;
41
42 inline auto operator|(const UcesbMap& left, const UcesbMap& right) -> UcesbMap
43 {
44 auto res = static_cast<UcesbMapUType>(left) | static_cast<UcesbMapUType>(right);
45 return static_cast<UcesbMap>(res);
46 }
47 inline void operator|=(UcesbMap& left, const UcesbMap& right) { left = left | right; }
48
49 inline auto operator&(const UcesbMap& left, const UcesbMap& right) -> UcesbMap
50 {
51 auto res = static_cast<UcesbMapUType>(left) & static_cast<UcesbMapUType>(right);
52 return static_cast<UcesbMap>(res);
53 }
54
55 inline void operator&=(UcesbMap& left, const UcesbMap& right) { left = left & right; }
56
57 inline auto operator~(const UcesbMap& map) -> UcesbMap
58 {
59 return static_cast<UcesbMap>(~(static_cast<UcesbMapUType>(map)));
60 }
61
62 inline auto operator<<(std::ostream& ostream, const UcesbMap& map) -> std::ostream&
63 {
64 ostream << std::bitset<UCESB_MAP_BITSIZE>(static_cast<UcesbMapUType>(map));
65 return ostream;
66 }
67
68 inline auto UcesbMap2String(UcesbMap map) -> std::string
69 {
70 switch (map)
71 {
72 case UcesbMap::zero:
73 return std::string{ "zero" };
74 case UcesbMap::match:
75 return std::string{ "match" };
77 return std::string{ "no_dest" };
79 return std::string{ "not_found" };
81 return std::string{ "type_mismatch" };
83 return std::string{ "ctrl_mismatch" };
85 return std::string{ "array_fewer" };
87 return std::string{ "array_more" };
89 return std::string{ "not_done" };
90 case UcesbMap::ok:
91 return std::string{ "ok" };
93 return std::string{ "ok_no_dest" };
94 default:
95 return fmt::format("", std::bitset<UCESB_MAP_BITSIZE>(static_cast<UcesbMapUType>(map)).to_string());
96 }
97 }
98} // namespace R3B
99
100template <>
101class fmt::formatter<R3B::UcesbMap>
102{
103 public:
104 constexpr auto parse(format_parse_context& ctx)
105 {
106 if (ctx.begin() == ctx.end())
107 {
108 return ctx.end();
109 }
110 const auto* specifier_iter = ctx.begin();
111 if (*specifier_iter == 's' or *specifier_iter == 'b')
112 {
113 presentation_ = *specifier_iter;
114 ++specifier_iter; // NOLINT
115 }
116
117 if (specifier_iter == ctx.end())
118 {
119 throw format_error("UcesbMap format failed: missing right curly bracket!");
120 }
121
122 if (*specifier_iter != '}')
123 {
124 throw format_error("UcesbMap format failed: only one speicifier is allowed");
125 }
126 return specifier_iter;
127 }
128
129 template <typename FmtContent>
130 constexpr auto format(const R3B::UcesbMap& flag, FmtContent& ctn) const
131 {
132 switch (presentation_)
133 {
134 default:
135 case 's':
136 return format_to(ctn.out(), "{}", R3B::UcesbMap2String(flag));
137 case 'b':
138 return format_to(ctn.out(), "0x{:b}", static_cast<R3B::UcesbMapUType>(flag));
139 }
140 }
141
142 private:
143 char presentation_ = 's';
144};
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