17
17
#include < list>
18
18
#include < map>
19
19
#include < shared_mutex>
20
+ #include < variant>
20
21
#include < vector>
21
22
22
23
#include " ExclusiveAccess.h"
@@ -122,7 +123,8 @@ struct InfoTreeNode {
122
123
static constexpr uint64_t IndentSize = 4 ;
123
124
124
125
std::string Key;
125
- std::string Value;
126
+ using VariantType = std::variant<uint64_t , std::string, bool , std::monostate>;
127
+ VariantType Value;
126
128
std::string Units;
127
129
// Need to specify a default value number of elements here as `InfoTreeNode`'s
128
130
// size is unknown. This is a vector (rather than a Key->Value map) since:
@@ -131,31 +133,31 @@ struct InfoTreeNode {
131
133
// * The same key can appear multiple times
132
134
std::unique_ptr<llvm::SmallVector<InfoTreeNode, 8 >> Children;
133
135
134
- InfoTreeNode () : InfoTreeNode(" " , " " , " " ) {}
135
- InfoTreeNode (std::string Key, std::string Value, std::string Units)
136
+ InfoTreeNode () : InfoTreeNode(" " , std::monostate{} , " " ) {}
137
+ InfoTreeNode (std::string Key, VariantType Value, std::string Units)
136
138
: Key(Key), Value(Value), Units(Units) {}
137
139
138
140
// / Add a new info entry as a child of this node. The entry requires at least
139
141
// / a key string in \p Key. The value in \p Value is optional and can be any
140
142
// / type that is representable as a string. The units in \p Units is optional
141
143
// / and must be a string.
142
- template <typename T = std::string >
144
+ template <typename T = std::monostate >
143
145
InfoTreeNode *add (std::string Key, T Value = T(),
144
146
const std::string &Units = std::string()) {
145
147
assert (!Key.empty () && " Invalid info key" );
146
148
147
149
if (!Children)
148
150
Children = std::make_unique<llvm::SmallVector<InfoTreeNode, 8 >>();
149
151
150
- std::string ValueStr ;
151
- if constexpr (std::is_same_v<T, bool >)
152
- ValueStr = Value ? " Yes " : " No " ;
152
+ VariantType ValueVariant ;
153
+ if constexpr (std::is_same_v<T, bool > || std::is_same_v<T, std::monostate> )
154
+ ValueVariant = Value;
153
155
else if constexpr (std::is_arithmetic_v<T>)
154
- ValueStr = std::to_string (Value);
156
+ ValueVariant = static_cast < uint64_t > (Value);
155
157
else
156
- ValueStr = Value;
158
+ ValueVariant = std::string{ Value} ;
157
159
158
- return &Children->emplace_back (Key, ValueStr , Units);
160
+ return &Children->emplace_back (Key, ValueVariant , Units);
159
161
}
160
162
161
163
std::optional<InfoTreeNode *> get (StringRef Key) {
@@ -184,8 +186,23 @@ struct InfoTreeNode {
184
186
MaxKeySize - (Key.size () + KeyIndentSize) + IndentSize;
185
187
186
188
llvm::outs () << std::string (KeyIndentSize, ' ' ) << Key
187
- << std::string (ValIndentSize, ' ' ) << Value
188
- << (Units.empty () ? " " : " " ) << Units << " \n " ;
189
+ << std::string (ValIndentSize, ' ' );
190
+ std::visit (
191
+ [](auto &&V) {
192
+ using T = std::decay_t <decltype (V)>;
193
+ if constexpr (std::is_same_v<T, std::string>)
194
+ llvm::outs () << V;
195
+ else if constexpr (std::is_same_v<T, bool >)
196
+ llvm::outs () << (V ? " Yes" : " No" );
197
+ else if constexpr (std::is_same_v<T, uint64_t >)
198
+ llvm::outs () << V;
199
+ else if constexpr (std::is_same_v<T, std::monostate>) {
200
+ // Do nothing
201
+ } else
202
+ static_assert (false , " doPrint visit not exhaustive" );
203
+ },
204
+ Value);
205
+ llvm::outs () << (Units.empty () ? " " : " " ) << Units << " \n " ;
189
206
}
190
207
191
208
// Print children
0 commit comments