2 * Copyright (c) 2024 EfficiOS, inc.
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2C_FMT_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_FMT_HPP
10 #include "cpp-common/bt2/value.hpp"
11 #include "cpp-common/vendor/fmt/format.h" /* IWYU pragma: keep */
12 #include "cpp-common/vendor/wise-enum/wise_enum.h"
19 using EnableIfIsWiseEnum =
20 typename std::enable_if<wise_enum::is_wise_enum<T>::value, wise_enum::string_type>::type;
22 } /* namespace internal */
27 ::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept
29 return wise_enum::to_string<T>(val);
32 inline std::string format_as(const bt2::ConstValue val) noexcept
39 return val.asBool().value() ? "true" : "false";
41 case ValueType::UnsignedInteger:
42 return fmt::format("{}u", val.asUnsignedInteger().value());
44 case ValueType::SignedInteger:
45 return fmt::format("{}", val.asSignedInteger().value());
48 return fmt::format("{}", val.asReal().value());
50 case ValueType::String:
51 return fmt::format("\"{}\"", val.asString().value());
53 case ValueType::Array:
55 std::string ret {'['};
56 const char *maybeComma = "";
58 for (const auto elem : val.asArray()) {
59 ret += fmt::format("{}{}", maybeComma, elem);
69 std::string ret {'{'};
70 const char *maybeComma = "";
72 val.asMap().forEach([&](const bt2c::CStringView k, const bt2::ConstValue v) {
73 ret += fmt::format("{}{}: {}", maybeComma, k, v);
90 ::internal::EnableIfIsWiseEnum<T> format_as(const T val) noexcept
92 return wise_enum::to_string<T>(val);
95 inline std::string format_as(const UuidView uuid)
100 } /* namespace bt2c */
102 #endif /* BABELTRACE_CPP_COMMON_BT2C_FMT_HPP */