Skip to content

Commit 78dcf7d

Browse files
author
Lukas Thomann
committed
updated format macros again to have better naming.
1 parent 8721f5a commit 78dcf7d

5 files changed

Lines changed: 40 additions & 30 deletions

File tree

cth/incl/cth/io/keybd/keys.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ cxpr std::string_view to_utf8_string(Key key) {
254254

255255
CTH_GEN_ENUM_DEREF_OVERLOAD(cth::io::Key)
256256

257-
CTH_FORMAT_CLASS_ATTRIBUTES(
257+
CTH_FORMAT_CLASS(
258258
cth::io::Key,
259259
"{}",
260260
([](cth::io::Key const& key) { return cth::io::to_utf8_string(key); })
@@ -288,19 +288,19 @@ struct key_update {
288288

289289
}
290290

291-
CTH_FORMAT_CLASS_ATTRIBUTES(
291+
CTH_FORMAT_CLASS(
292292
cth::io::ex_key,
293293
"{}, right: {}",
294294
([](cth::io::ex_key const& key) { return std::tie(key.key, key.right); })
295295
);
296296

297-
CTH_FORMAT_CLASS_ATTRIBUTES(
297+
CTH_FORMAT_CLASS(
298298
cth::io::key_state,
299299
"{}, down: {}",
300300
([](cth::io::key_state const& key) { return std::tie(key.exKey, key.down); })
301301
);
302302

303-
CTH_FORMAT_CLASS_ATTRIBUTES(
303+
CTH_FORMAT_CLASS(
304304
cth::io::key_update,
305305
"{}, time: {}",
306306
(

cth/incl/cth/io/log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ inline auto x = []() {
643643
CTH_ASSERT(false, "invalid CTH_LOG_LEVEL macro value defined");
644644
static_assert(false);
645645
return 10;
646-
}();
646+
}();
647647
#endif
648648

649649
#endif

cth/incl/cth/meta/concepts.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
#include <tuple>
55

66

7-
#define CPT(concept) \
8-
[]<concept> {}
9-
10-
117
namespace cth::mta {
128
template<class T, auto TCpt>
139
concept satisfies = requires { TCpt.template operator()<T>(); };

cth/incl/cth/string/format.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @param fmt_string The format string (e.g., "ID: {}, Val: {}")
3131
* @param tie_func An expression involving 'obj' that returns a tuple/tie
3232
*/
33-
#define CTH_FORMAT_CLASS(type, fmt_string, tie_func) \
33+
#define CTH_FORMAT_CLASS_CUSTOM(type, fmt_string, tie_func) \
3434
template<> \
3535
struct std::formatter<type> { \
3636
[[nodiscard]] constexpr auto parse(std::format_parse_context& ctx) { \
@@ -55,13 +55,13 @@ struct std::formatter<type> {
5555
}
5656

5757
/**
58-
* creates a format overload for a class, implicitly includes type name with type[<fmt_string>]
58+
* creates a format overload for a class, implicitly includes type name with type{<fmt_string>}
5959
* @param type The class/struct type to format
6060
* @param fmt_string The format string (e.g., "ID: {}, Val: {}")
6161
* @param tie_func An expression involving 'obj' that returns a tuple/tie
6262
*/
63-
#define CTH_FORMAT_CLASS_ATTRIBUTES(type, fmt_string, tie_func)\
64-
CTH_FORMAT_CLASS(type, #type "[" fmt_string "]", tie_func)
63+
#define CTH_FORMAT_CLASS(type, fmt_string, tie_func)\
64+
CTH_FORMAT_CLASS_CUSTOM(type, #type "{{" fmt_string "}}", tie_func)
6565

6666
/**
6767
* @brief creates a format overload for the concept

cth/tests/src/format_test.cpp

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,57 @@
88
namespace test {
99

1010

11-
enum Test1 {
12-
VALUE1,
11+
struct Test1 {
12+
int x;
13+
int y;
1314
};
1415
enum Test2 { VALUE2 };
1516

1617

17-
constexpr std::string_view to_string(Test1 e) {
18+
19+
constexpr std::string_view to_string(Test2 const& e) {
1820
switch(e) {
19-
case VALUE1: return "VALUE1";
21+
case VALUE2: return "VALUE2";
2022
default: return "unknown";
2123
}
2224
}
2325

26+
struct Test3 {
27+
Test1 a;
28+
};
2429

2530
template<class T>
26-
concept test_concept = std::is_same_v<T, Test2>;
27-
constexpr std::string_view to_string(Test2 e) {
28-
switch(e) {
29-
case VALUE2: return "VALUE2";
30-
default: return "unknown";
31-
}
32-
}
31+
concept test_concept = std::same_as<T, Test3>;
3332
}
3433

3534

36-
CTH_FORMAT_CLASS(test::Test1, "{}", test::to_string);
35+
CTH_FORMAT_CLASS(test::Test1, "x: {}, y: {}", ([](test::Test1 const& test) {return std::tie(test.x, test.y); }));
36+
CTH_FORMAT_CLASS_CUSTOM(test::Test2, "{}", test::to_string);
37+
38+
namespace test {
39+
std::string to_string(Test3 const& e) { return std::format("{}", e.a); }
40+
}
41+
3742
CTH_FORMAT_CPT(test::test_concept, test::to_string);
3843

3944

4045
namespace cth::fmt {
41-
FMT_TEST(CTH_FORMAT_CLASS, main) {
42-
auto const str = std::format("{}", ::test::VALUE1);
46+
::test::Test1 x{1, 2};
4347

44-
ASSERT_EQ("test::Test1{VALUE1}", str);
48+
FMT_TEST(CTH_FORMAT_CLASS_CUSTOM, main) {
49+
auto const str = std::format("{}", ::test::VALUE2);
50+
ASSERT_EQ(str, "VALUE2");
51+
}
52+
53+
FMT_TEST(CTH_FORMAT_CLASS, main) {
54+
auto const str = std::format("{}", x);
55+
auto const expected = std::format("test::Test1{{x: {}, y: {}}}", x.x, x.y);
56+
ASSERT_EQ(str, expected);
4557
}
4658
FMT_TEST(CTH_FORMAT_CPT, main) {
47-
auto const str = std::format("{}", ::test::VALUE2);
48-
ASSERT_EQ("test::test_concept{VALUE2}", str);
59+
auto const str = std::format("{}", ::test::Test3{x});
60+
auto const expected = std::format("test::test_concept{{{}}}", x);
61+
62+
ASSERT_EQ(str, expected);
4963
}
5064
}

0 commit comments

Comments
 (0)