libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
literal.hpp
1
13{
18enum class Frontend : uint8_t
19{
20 Auto,
21 Format,
22 Printf,
23};
24
30enum class Resolution : uint8_t
31{
32 None,
33 Format,
34 Printf,
35 Ambiguous,
36};
37
42template <Print::Text Source>
43[[nodiscard]] consteval bool UsesFormatSyntax()
44{
45 for (size_t i = 0; i < Source.Size(); ++i)
46 {
47 if (Source.data[i] == '{' || Source.data[i] == '}')
48 {
49 return true;
50 }
51 }
52 return false;
53}
54
59template <Print::Text Source>
60[[nodiscard]] consteval bool UsesPrintfSyntax()
61{
62 for (size_t i = 0; i < Source.Size(); ++i)
63 {
64 if (Source.data[i] == '%')
65 {
66 return true;
67 }
68 }
69 return false;
70}
71
76template <Print::Text Source>
77[[nodiscard]] consteval bool FormatSourceValid()
78{
79 return Print::Detail::FormatFrontend::Analyze<Source>().error ==
80 Print::Detail::FormatFrontend::Error::None;
81}
82
87template <Print::Text Source>
88[[nodiscard]] consteval bool PrintfSourceValid()
89{
90 return Print::Detail::PrintfCompile::Analyze<Source>().error ==
91 Print::Printf::Error::None;
92}
93
105template <Print::Text Source, typename... Args>
106[[nodiscard]] consteval bool FormatMatches()
107{
108 if constexpr (!FormatSourceValid<Source>())
109 {
110 return false;
111 }
112 else
113 {
114 return LibXR::Format<Source>::template Matches<Args...>();
115 }
116}
117
123template <Print::Text Source, typename... Args>
124[[nodiscard]] consteval bool PrintfMatches()
125{
126 if constexpr (!PrintfSourceValid<Source>())
127 {
128 return false;
129 }
130 else
131 {
132 return Print::Printf::template Matches<Source, Args...>();
133 }
134}
135
141template <Frontend Forced, Print::Text Source, typename... Args>
142[[nodiscard]] consteval Resolution ResolveFrontend()
143{
144 constexpr bool format_match = FormatMatches<Source, Args...>();
145 constexpr bool printf_match = PrintfMatches<Source, Args...>();
146
147 if constexpr (Forced == Frontend::Format)
148 {
149 return format_match ? Resolution::Format : Resolution::None;
150 }
151 else if constexpr (Forced == Frontend::Printf)
152 {
153 return printf_match ? Resolution::Printf : Resolution::None;
154 }
155 else if constexpr (format_match && !printf_match)
156 {
157 return Resolution::Format;
158 }
159 else if constexpr (!format_match && printf_match)
160 {
161 return Resolution::Printf;
162 }
163 else if constexpr (!format_match && !printf_match)
164 {
165 return Resolution::None;
166 }
167 else
168 {
169 constexpr bool format_uses_syntax = UsesFormatSyntax<Source>();
170 constexpr bool printf_uses_syntax = UsesPrintfSyntax<Source>();
171
172 if constexpr (format_uses_syntax && !printf_uses_syntax)
173 {
174 return Resolution::Format;
175 }
176 else if constexpr (!format_uses_syntax && printf_uses_syntax)
177 {
178 return Resolution::Printf;
179 }
180 else if constexpr (!format_uses_syntax && !printf_uses_syntax)
181 {
182 return Resolution::Format;
183 }
184 else
185 {
187 }
188 }
189}
190
196template <Frontend Forced, Print::Text Source, typename... Args>
197[[nodiscard]] consteval Frontend SelectFrontend()
198{
199 constexpr auto resolution = ResolveFrontend<Forced, Source, Args...>();
200
201 if constexpr (Forced == Frontend::Format)
202 {
203 static_assert(resolution == Resolution::Format,
204 "LibXR::Logger: XR_FMT(...) literal is not accepted by the brace frontend");
205 return Frontend::Format;
206 }
207 else if constexpr (Forced == Frontend::Printf)
208 {
209 static_assert(
210 resolution == Resolution::Printf,
211 "LibXR::Logger: XR_PRINTF(...) literal is not accepted by the printf frontend");
212 return Frontend::Printf;
213 }
214 else if constexpr (resolution == Resolution::Format)
215 {
216 return Frontend::Format;
217 }
218 else if constexpr (resolution == Resolution::Printf)
219 {
220 return Frontend::Printf;
221 }
222 else if constexpr (resolution == Resolution::Ambiguous)
223 {
224 static_assert(resolution != Resolution::Ambiguous,
225 "LibXR::Logger: literal is ambiguous between brace and printf frontends; use XR_FMT(...) or XR_PRINTF(...)");
226 return Frontend::Auto;
227 }
228 else
229 {
230 static_assert(resolution != Resolution::None,
231 "LibXR::Logger: literal matches neither brace nor printf frontend");
232 return Frontend::Auto;
233 }
234}
235} // namespace Detail::LoggerLiteral
Logger 的字面量前端选择片段 Literal-frontend selection fragment of Logger
Definition literal.hpp:13
consteval bool UsesPrintfSyntax()
Returns whether one valid printf literal actually uses printf syntax.
Definition literal.hpp:60
consteval Resolution ResolveFrontend()
Selects the logger frontend for one literal plus one concrete argument list.
Definition literal.hpp:142
Frontend
Logger literal frontend selection mode.
Definition literal.hpp:19
@ Auto
select brace or printf automatically / 自动选择 brace 或 printf
@ Format
force brace-style frontend / 强制使用 brace 风格前端
@ Printf
force printf-style frontend / 强制使用 printf 风格前端
consteval bool FormatSourceValid()
Returns whether one brace-style source is source-level valid.
Definition literal.hpp:77
consteval bool FormatMatches()
Returns whether one argument list is accepted by the brace frontend, guarded by source-level validity...
Definition literal.hpp:106
consteval bool PrintfMatches()
Returns whether one argument list is accepted by the printf frontend, guarded by source-level validit...
Definition literal.hpp:124
consteval bool UsesFormatSyntax()
Returns whether one valid brace literal actually uses brace syntax.
Definition literal.hpp:43
Resolution
Result of resolving one logger literal against the available frontends.
Definition literal.hpp:31
@ Ambiguous
both frontends remain valid and both syntaxes are used / 两个前端都可用且都真的使用了自己的语法
@ Format
select brace-style frontend / 选择 brace 风格前端
@ None
matches neither frontend / 两个前端都不匹配
@ Printf
select printf-style frontend / 选择 printf 风格前端
consteval Frontend SelectFrontend()
Selects the final logger frontend after validating the resolution result.
Definition literal.hpp:197
consteval bool PrintfSourceValid()
Returns whether one printf-style source is source-level valid.
Definition literal.hpp:88