Commit | Line | Data |
---|---|---|
7cf8258b SM |
1 | /* |
2 | * Copyright (c) 2024 EfficiOS, inc. | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
ae2be88d SM |
7 | #ifndef BABELTRACE_CPP_COMMON_BT2C_FMT_HPP |
8 | #define BABELTRACE_CPP_COMMON_BT2C_FMT_HPP | |
9 | ||
57731356 | 10 | #include "cpp-common/bt2/value.hpp" |
7cf8258b | 11 | #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */ |
91730cec | 12 | #include "cpp-common/vendor/wise-enum/wise_enum.h" |
7cf8258b | 13 | |
8f7f6134 SM |
14 | #include "uuid.hpp" |
15 | ||
91730cec SM |
16 | namespace internal { |
17 | ||
18 | template <typename T> | |
19 | using EnableIfIsWiseEnum = | |
e6a82b00 | 20 | typename std::enable_if<wise_enum::is_wise_enum<T>::value, wise_enum::string_type>::type; |
91730cec SM |
21 | |
22 | } /* namespace internal */ | |
23 | ||
7cf8258b SM |
24 | namespace bt2 { |
25 | ||
91730cec SM |
26 | template <typename T> |
27 | ::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept | |
28 | { | |
29 | return wise_enum::to_string<T>(val); | |
30 | } | |
31 | ||
57731356 SM |
32 | inline std::string format_as(const bt2::ConstValue val) noexcept |
33 | { | |
34 | switch (val.type()) { | |
35 | case ValueType::Null: | |
36 | return "null"; | |
37 | ||
38 | case ValueType::Bool: | |
39 | return val.asBool().value() ? "true" : "false"; | |
40 | ||
41 | case ValueType::UnsignedInteger: | |
42 | return fmt::format("{}u", val.asUnsignedInteger().value()); | |
43 | ||
44 | case ValueType::SignedInteger: | |
45 | return fmt::format("{}", val.asSignedInteger().value()); | |
46 | ||
47 | case ValueType::Real: | |
48 | return fmt::format("{}", val.asReal().value()); | |
49 | ||
50 | case ValueType::String: | |
51 | return fmt::format("\"{}\"", val.asString().value()); | |
52 | ||
53 | case ValueType::Array: | |
54 | { | |
55 | std::string ret {'['}; | |
56 | const char *maybeComma = ""; | |
57 | ||
58 | for (const auto elem : val.asArray()) { | |
59 | ret += fmt::format("{}{}", maybeComma, elem); | |
60 | maybeComma = ", "; | |
61 | } | |
62 | ||
63 | ret += ']'; | |
64 | return ret; | |
65 | } | |
66 | ||
67 | case ValueType::Map: | |
68 | { | |
69 | std::string ret {'{'}; | |
70 | const char *maybeComma = ""; | |
71 | ||
72 | val.asMap().forEach([&](const bt2c::CStringView k, const bt2::ConstValue v) { | |
73 | ret += fmt::format("{}{}: {}", maybeComma, k, v); | |
74 | maybeComma = ", "; | |
75 | }); | |
76 | ||
77 | ret += '}'; | |
78 | return ret; | |
79 | } | |
80 | } | |
81 | ||
82 | bt_common_abort(); | |
83 | } | |
84 | ||
7cf8258b | 85 | } /* namespace bt2 */ |
e05975eb SM |
86 | |
87 | namespace bt2c { | |
88 | ||
91730cec SM |
89 | template <typename T> |
90 | ::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept | |
91 | { | |
92 | return wise_enum::to_string<T>(val); | |
93 | } | |
94 | ||
e05975eb SM |
95 | inline std::string format_as(const UuidView uuid) |
96 | { | |
97 | return uuid.str(); | |
98 | } | |
99 | ||
100 | } /* namespace bt2c */ | |
ae2be88d SM |
101 | |
102 | #endif /* BABELTRACE_CPP_COMMON_BT2C_FMT_HPP */ |