libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
printf.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <type_traits>
6
7#include "format_argument.hpp"
8
9namespace LibXR::Print
10{
15template <size_t N>
16struct Text
17{
22 char data[N]{};
23
29 constexpr Text(const char (&text)[N])
30 {
31 for (size_t i = 0; i < N; ++i)
32 {
33 data[i] = text[i];
34 }
35 }
36
43 [[nodiscard]] constexpr size_t Size() const { return N - 1; }
50 [[nodiscard]] constexpr const char* Data() const { return data; }
51};
52
57class Printf
58{
59 public:
64 enum class Length : uint8_t
65 {
66 Default,
67 Char,
68 Short,
69 Long,
70 LongLong,
71 IntMax,
72 Size,
73 PtrDiff,
75 };
76
109
110 template <Text Source>
111 class Compiler;
112
113 template <Text Source>
114 struct Compiled;
115
122 template <Text Source>
123 [[nodiscard]] static consteval Compiled<Source> Build()
124 {
125 return {};
126 }
127
136 template <Text Source, typename... Args>
137 [[nodiscard]] static consteval bool Matches()
138 {
139 return Compiled<Source>::template Matches<Args...>();
140 }
141};
142} // namespace LibXR::Print
143
144#include "printf/printf_frontend_detail.hpp"
145namespace LibXR::Print
146{
147template <Text Source>
149{
150 private:
151 using Frontend = typename Printf::template Compiler<Source>;
152 inline static constexpr auto source_analysis = Detail::PrintfCompile::Analyze<Source>();
153 inline static constexpr auto result = FormatCompiler<Frontend>::Compile();
154
155 static_assert(result.compile_error != Printf::Error::NumberOverflow,
156 "LibXR::Print::Printf: numeric field is too large");
157 static_assert(
158 result.compile_error != Printf::Error::FloatPrecisionLimitExceeded,
159 "LibXR::Print::Printf: float precision exceeds the configured print limit");
160 static_assert(result.compile_error != Printf::Error::UnexpectedEnd,
161 "LibXR::Print::Printf: unexpected end of format string");
162 static_assert(
163 result.compile_error != Printf::Error::EmbeddedNul,
164 "LibXR::Print::Printf: embedded NUL bytes are not supported in the format literal");
165 static_assert(
166 result.compile_error != Printf::Error::MixedIndexing,
167 "LibXR::Print::Printf: positional and sequential arguments cannot be mixed");
168 static_assert(result.compile_error != Printf::Error::PositionalArgumentDisabled,
169 "LibXR::Print::Printf: positional argument indexing is disabled in the "
170 "current profile");
171 static_assert(result.compile_error != Printf::Error::DynamicField,
172 "LibXR::Print::Printf: dynamic width and precision are not supported");
173 static_assert(result.compile_error != Printf::Error::InvalidArgumentIndex,
174 "LibXR::Print::Printf: invalid positional argument index");
175 static_assert(result.compile_error != Printf::Error::InvalidSpecifier,
176 "LibXR::Print::Printf: invalid format specifier");
177 static_assert(result.compile_error != Printf::Error::InvalidLength,
178 "LibXR::Print::Printf: invalid length modifier");
179 static_assert(result.compile_error != Printf::Error::ConflictingArgument,
180 "LibXR::Print::Printf: one positional argument is reused with "
181 "incompatible conversions");
182 static_assert(result.compile_error != Printf::Error::TextOffsetOverflow,
183 "LibXR::Print::Printf: text pool offset is too large");
184 static_assert(result.compile_error != Printf::Error::TextSizeOverflow,
185 "LibXR::Print::Printf: text span is too large");
186 static_assert(source_analysis.error != Printf::Error::ConflictingArgument,
187 "LibXR::Print::Printf: one positional argument is reused with "
188 "incompatible conversions");
189
190 public:
195 inline static constexpr auto codes = result.codes;
200 inline static constexpr FormatProfile profile = result.profile;
201
206 [[nodiscard]] static constexpr auto ArgumentList() { return result.arg_info; }
207
212 [[nodiscard]] static constexpr auto ArgumentOrder() { return source_analysis.order; }
213
218 [[nodiscard]] static constexpr auto SourceArgumentList()
219 {
220 return source_analysis.args;
221 }
222
227 [[nodiscard]] static constexpr const auto& Codes() { return codes; }
228
233 [[nodiscard]] static constexpr FormatProfile Profile() { return profile; }
234
242 template <typename... Args>
243 [[nodiscard]] static consteval bool Matches()
244 {
245 return Detail::FormatArgument::template Matches<Args...>(SourceArgumentList());
246 }
247};
248} // namespace LibXR::Print
static consteval auto Compile()
把一个前端编译成最终字节流、参数表和 writer 摘要。 / Compiles one frontend into the final byte stream, argument table,...
单个 printf 源字符串的编译期前端,负责解析并降为共享格式语义 / Compile-time printf frontend that parses and lowers one source s...
printf 风格前端,输出内部格式表示 / Printf-style frontend that emits the internal format representation
Definition printf.hpp:58
Length
归一化后的 printf 长度修饰符 / Supported printf length modifiers after normalization
Definition printf.hpp:65
@ LongLong
ll / long long 长度修饰
@ IntMax
j / intmax_t 长度修饰
@ PtrDiff
t / ptrdiff_t 长度修饰
@ Short
h / short 长度修饰
@ Size
z / size_t 长度修饰
@ LongDouble
L / long double 长度修饰
@ Default
no length modifier / 无长度修饰符
@ Long
l / long 长度修饰
@ Char
hh / char 长度修饰
Error
通过 static_assert 暴露的编译期解析或构建失败类别 / Compile-time parse or build failure categories surfaced through st...
Definition printf.hpp:82
@ InvalidArgumentIndex
positional argument index is invalid / 位置参数索引非法
@ None
success / 成功
static consteval Compiled< Source > Build()
在编译期解析并校验 printf 格式串 / Parse and validate a printf format at compile time
Definition printf.hpp:123
static consteval bool Matches()
判断 Args... 是否与启用的转换项精确匹配 / Return whether Args... exactly match the enabled conversions
Definition printf.hpp:137
static consteval bool Matches()
判断 Args... 是否就是当前编译格式期望的那组 C++ 参数类型。 / Returns whether Args... are exactly the C++ argument types thi...
Definition printf.hpp:243
static constexpr auto codes
返回运行期 writer 最终会执行的字节流。 / Returns the final byte stream that the runtime writer will execute.
Definition printf.hpp:195
static constexpr FormatProfile Profile()
返回当前编译格式携带的 writer 分支摘要。 / Returns the writer-branch summary carried by this compiled format.
Definition printf.hpp:233
static constexpr auto ArgumentOrder()
返回每个字段对应的是第几个源参数。 / Returns, for each field, which source argument index it refers to.
Definition printf.hpp:212
static constexpr FormatProfile profile
返回当前格式需要哪些 writer 分支的编译期摘要。 / Returns the compile-time summary of which writer branches this format n...
Definition printf.hpp:200
static constexpr auto SourceArgumentList()
返回仅供编译期类型匹配使用的源参数列表。 / Returns the source-argument list used only for compile-time type matching.
Definition printf.hpp:218
static constexpr auto ArgumentList()
返回运行期参数打包时要按字段顺序读取的参数列表。 / Returns the field-ordered argument list the runtime packer will follow.
Definition printf.hpp:206
static constexpr const auto & Codes()
返回与 codes 相同的最终字节流。 / Returns the same final byte stream as codes.
Definition printf.hpp:227
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
Definition printf.hpp:17
char data[N]
含结尾零字节的字面量字节序列 / Literal bytes including the terminating zero byte
Definition printf.hpp:22
constexpr Text(const char(&text)[N])
将字符串字面量复制到结构化 NTTP 对象中 / Copy the string literal into the structural NTTP object
Definition printf.hpp:29
constexpr const char * Data() const
返回含结尾零字节的字面量指针 / Return the literal bytes including the terminating zero byte
Definition printf.hpp:50
constexpr size_t Size() const
返回不含结尾零字节的格式串长度 / Return the format length without the terminating zero byte
Definition printf.hpp:43