Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
format_boost.h
1// SPDX-FileCopyrightText: © 2025-2026 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#if defined(__clang__)
7# pragma clang diagnostic push
8# pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
9#endif
10#include <boost/describe.hpp>
11#if defined(__clang__)
12# pragma clang diagnostic pop
13#endif
14
15#include <fmt/format.h>
16
17// Universal formatter for all types described by BOOST_DESCRIBE_STRUCT or
18// BOOST_DESCRIBE_CLASS.
19template <class T>
20struct fmt::formatter<
21 T,
22 char,
23 std::enable_if_t<
24 boost::describe::has_describe_bases<T>::value
25 && boost::describe::has_describe_members<T>::value && !std::is_union_v<T>>>
26{
27 constexpr auto parse (format_parse_context &ctx)
28 {
29 const auto * it = ctx.begin ();
30 const auto * end = ctx.end ();
31
32 if (it != end && *it != '}')
33 {
34 throw format_error ("invalid format");
35 }
36
37 return it;
38 }
39
40 auto format (T const &t, format_context &ctx) const
41 {
42 using namespace boost::describe;
43
44 using Bd = describe_bases<T, mod_any_access>;
45 using Md = describe_members<T, mod_any_access>;
46
47 auto out = ctx.out ();
48
49 *out++ = '{';
50
51 bool first = true;
52
53 boost::mp11::mp_for_each<Bd> ([&] (auto D) {
54 if (!first)
55 {
56 *out++ = ',';
57 }
58
59 first = false;
60
61 out = fmt::format_to (out, " {}", (typename decltype (D)::type const &) t);
62 });
63
64 boost::mp11::mp_for_each<Md> ([&] (auto D) {
65 if (!first)
66 {
67 *out++ = ',';
68 }
69
70 first = false;
71
72 out = fmt::format_to (out, " .{}={}", D.name, t.*D.pointer);
73 });
74
75 if (!first)
76 {
77 *out++ = ' ';
78 }
79
80 *out++ = '}';
81
82 return out;
83 }
84};
85
86namespace zrythm::detail
87{
89{
90 int a_;
91 std::string b_;
92 BOOST_DESCRIBE_CLASS (BoostDescribeFormatTest, (), (a_, b_), (), ())
93};
94static_assert (fmt::formattable<BoostDescribeFormatTest>);
95}