6
6
#include < string_view>
7
7
#include < cstring>
8
8
#include < iterator>
9
+ #ifdef __cpp_lib_format
10
+ # include < format>
11
+ #endif
9
12
#include " ecsact/runtime/common.h"
10
13
#include " ecsact/codegen/plugin.h"
11
14
@@ -15,23 +18,36 @@ namespace ecsact {
15
18
* Helper type to give a more C++ friendly write function
16
19
* @example
17
20
* void ecsact_codegen_plugin
18
- * ( ecsact_package_id package_id
19
- * , ecsact_codegen_write_fn_t write_fn
21
+ * ( ecsact_package_id package_id
22
+ * , ecsact_codegen_write_fn_t write_fn
23
+ * , ecsact_codegen_report_fn_t report_fn
20
24
* )
21
25
* {
22
- * ecsact::codegen_plugin_context ctx{package_id, write_fn};
23
- * ctx.write("Hello, World!\n");
26
+ * ecsact::codegen_plugin_context ctx{package_id, write_fn, report_fn};
27
+ * ctx.writef("Hello, World!\n");
28
+ * ctx.info("We made it!");
24
29
* }
25
30
*/
26
31
struct codegen_plugin_context {
27
- const ecsact_package_id package_id;
28
- const ecsact_codegen_write_fn_t write_fn;
29
- int indentation = 0 ;
32
+ const ecsact_package_id package_id;
33
+ const ecsact_codegen_write_fn_t write_fn;
34
+ const ecsact_codegen_report_fn_t report_fn;
35
+ int indentation = 0 ;
30
36
31
37
std::string get_indent_str () {
32
38
return std::string (indentation, ' \t ' );
33
39
}
34
40
41
+ void report_ (
42
+ ecsact_codegen_report_type report_type,
43
+ const char * str_data,
44
+ int32_t str_data_len
45
+ ) {
46
+ if (report_fn != nullptr ) {
47
+ report_fn (report_type, str_data, str_data_len);
48
+ }
49
+ }
50
+
35
51
void write_ (const char * str_data, int32_t str_data_len) {
36
52
assert (indentation >= 0 );
37
53
@@ -56,6 +72,7 @@ struct codegen_plugin_context {
56
72
}
57
73
58
74
template <typename T>
75
+ [[deprecated(" use writef instead" )]]
59
76
void write (T&& arg) {
60
77
using NoRefT = std::remove_cvref_t <T>;
61
78
@@ -72,6 +89,7 @@ struct codegen_plugin_context {
72
89
}
73
90
74
91
template <typename ... T>
92
+ [[deprecated(" use writef instead" )]]
75
93
void write (T&&... args) {
76
94
(write<T>(std::forward<T>(args)), ...);
77
95
}
@@ -87,6 +105,38 @@ struct codegen_plugin_context {
87
105
}
88
106
}
89
107
}
108
+
109
+ #ifdef __cpp_lib_format
110
+ template <typename ... Args>
111
+ auto writef (std::format_string<Args...> fmt, Args&&... args) {
112
+ auto str = std::format (fmt, std::make_format_args (args...));
113
+ write_ (str.data (), static_cast <int32_t >(str.size ()));
114
+ }
115
+
116
+ template <typename ... Args>
117
+ auto info (std::format_string<Args...> fmt, Args&&... args) {
118
+ auto str = std::format (fmt, std::make_format_args (args...));
119
+ report_ (ECSACT_CODEGEN_REPORT_INFO, str.data (), str.size ());
120
+ }
121
+
122
+ template <typename ... Args>
123
+ auto warn (std::format_string<Args...> fmt, Args&&... args) {
124
+ auto str = std::format (fmt, std::make_format_args (args...));
125
+ report_ (ECSACT_CODEGEN_REPORT_WARNING, str.data (), str.size ());
126
+ }
127
+
128
+ template <typename ... Args>
129
+ auto error (std::format_string<Args...> fmt, Args&&... args) {
130
+ auto str = std::format (fmt, std::make_format_args (args...));
131
+ report_ (ECSACT_CODEGEN_REPORT_ERROR, str.data (), str.size ());
132
+ }
133
+
134
+ template <typename ... Args>
135
+ auto fatal (std::format_string<Args...> fmt, Args&&... args) {
136
+ auto str = std::format (fmt, std::make_format_args (args...));
137
+ report_ (ECSACT_CODEGEN_REPORT_FATAL, str.data (), str.size ());
138
+ }
139
+ #endif
90
140
};
91
141
92
142
} // namespace ecsact
0 commit comments