cpp-reflect
C++ Reflection and Annotations Library
Loading...
Searching...
No Matches
default.cppm
Go to the documentation of this file.
1// Copyright (c) 2024-2025, Víctor Castillo Agüero.
2// SPDX-License-Identifier: GPL-3.0-or-later
3
8
10
11import std;
12
13import packtl;
14import reflect;
15
17
18export namespace formats {
19 template <typename O>
20 struct default_fmt: refl::visitor<default_fmt<O>> {
21 struct args_t {
22 bool pretty = true;
23 unsigned int indent = 2;
24 };
25
26 explicit default_fmt(O& out_, args_t args_): refl::visitor<default_fmt<O>>(), out(out_), args(args_) {}
27
28 template <typename T>
29 void handle_value(const T& it) {
30 if constexpr (std::is_same_v<T, std::atomic_flag>) {
31 out << (it.test() ? "SET" : "CLEAR");
32 } else if constexpr (packtl::is_type<std::unique_ptr, T>::value) {
33 const auto* value = it.get();
34 out << " {";
35 out << std::format("0x{:X}", (std::size_t)value);
36 out << "} ";
37 } else if constexpr (packtl::is_type<std::shared_ptr, T>::value) {
38 const auto* value = it.get();
39 out << "{";
40 out << std::format("0x{:X}", (std::size_t)value);
41 out << "} ";
42 } else if constexpr (packtl::is_type<std::weak_ptr, T>::value) {
43 const auto* value = it.get();
44 out << " {";
45 out << std::format("0x{:X}", (std::size_t)value);
46 out << "}: ";
47 } else if constexpr (refl::Reflected<T>) {
48 if (visited_.contains((std::size_t)&it)) {
49 out << "<circular reference>";
50 return;
51 }
52 visited_.emplace((std::size_t)&it);
53 } else if constexpr (std::is_convertible_v<T, std::string>) {
54 out << std::format("{:?}", it);
55 } else if constexpr (std::same_as<T, char>) {
56 out << std::format("0x{:X} '{}'", (int)it, it);
57 } else if constexpr (std::formattable<T, char>) {
58 out << std::format("{}", it);
59 }
60
61 this->visit_value(it);
62 out << ";";
63 }
64
65 template <typename T>
66 void handle_iterable(const T& iterable) {
67 this->visit_iterable(iterable);
68 }
69
70 template <typename T, typename Field>
71 void handle_field(const T& obj) {
72 switch (Field::access) {
74 out << " -";
75 break;
77 // out << "🔒";
78 out << "🔒";
79 break;
80 // 🔓
82 out << " \\";
83 break;
85 out << " ";
86 break;
87 default:
88 break;
89 }
90
91 out << Field::name;
92 out << ": " << refl::type_name<typename Field::type> << " = ";
93
94 this->template visit_obj_field<T, Field>(obj);
95 }
96
97 template <typename T>
98 void handle_obj(const T& obj) {
99 out << refl::type_name<T> << " {" << "\n";
100 this->visit_obj(obj);
101 // for (std::size_t i = 0; i < indent; ++i) {
102 // for (std::size_t j = 0; j < args.indent; ++j) {
103 // out << " ";
104 // }
105 // }
106 out << "}";
107 }
108
109 template <refl::Reflected R>
110 void serialize(const R& obj) {
111 this->visit(obj);
112 out << "\n";
113 }
114
115 private:
116 std::unordered_set<std::size_t> visited_{};
117 O& out;
118 args_t args;
119 };
120} // namespace formats
constexpr auto type_name
void handle_iterable(const T &iterable)
Definition default.cppm:66
default_fmt(O &out_, args_t args_)
Definition default.cppm:26
void serialize(const R &obj)
Definition default.cppm:110
void handle_field(const T &obj)
Definition default.cppm:71
void handle_value(const T &it)
Definition default.cppm:29
void handle_obj(const T &obj)
Definition default.cppm:98
void visit_obj_field(const R &obj)
Definition visitor.cppm:114
void visit_value(const T &obj)
Definition visitor.cppm:84
void visit_iterable(const T &iterable)
Definition visitor.cppm:128