From 4ad155e19f4d3a299a8cb1e00686f91b93cd522e Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Tue, 21 Nov 2023 10:32:11 +0100 Subject: [PATCH] refactor: ostream/format support for fragment_category --- include/dwarfs/fragment_category.h | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/include/dwarfs/fragment_category.h b/include/dwarfs/fragment_category.h index 4425bb8e..3568bc77 100644 --- a/include/dwarfs/fragment_category.h +++ b/include/dwarfs/fragment_category.h @@ -24,9 +24,12 @@ #include #include #include +#include #include +#include + namespace dwarfs { class fragment_category { @@ -101,8 +104,41 @@ class fragment_category { value_type subcategory_{uninitialized}; }; +inline std::ostream& +operator<<(std::ostream& os, fragment_category const& cat) { + if (cat) { + os << cat.value(); + + if (cat.has_subcategory()) { + os << '.' << cat.subcategory(); + } + } else { + os << "uninitialized"; + } + + return os; +} + } // namespace dwarfs +template <> +struct fmt::formatter : formatter { + template + auto format(dwarfs::fragment_category const& cat, FormatContext& ctx) { + if (cat) { + if (cat.has_subcategory()) { + return formatter::format( + fmt::format("{}.{}", cat.value(), cat.subcategory()), ctx); + } else { + return formatter::format(fmt::format("{}", cat.value()), + ctx); + } + } else { + return formatter::format("uninitialized", ctx); + } + } +}; + namespace std { template <>