libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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
110
111 template <Text Source>
112 class Compiler;
113
114 template <Text Source>
115 struct Compiled;
116
123 template <Text Source>
124 [[nodiscard]] static consteval Compiled<Source> Build()
125 {
126 return {};
127 }
128
137 template <Text Source, typename... Args>
138 [[nodiscard]] static consteval bool Matches()
139 {
140 return Compiled<Source>::template Matches<Args...>();
141 }
142};
143} // namespace LibXR::Print
144
145#include "printf/printf_frontend_detail.hpp"
146namespace LibXR::Print
147{
148template <Text Source>
150{
151 private:
152 using Frontend = typename Printf::template Compiler<Source>;
153 inline static constexpr auto source_analysis = Detail::PrintfCompile::Analyze<Source>();
154 inline static constexpr auto result = FormatCompiler<Frontend>::Compile();
155
156 static_assert(result.compile_error != Printf::Error::NumberOverflow,
157 "LibXR::Print::Printf: numeric field is too large");
158 static_assert(
159 result.compile_error != Printf::Error::FloatPrecisionLimitExceeded,
160 "LibXR::Print::Printf: float precision exceeds the configured print limit");
161 static_assert(result.compile_error != Printf::Error::UnexpectedEnd,
162 "LibXR::Print::Printf: unexpected end of format string");
163 static_assert(
164 result.compile_error != Printf::Error::EmbeddedNul,
165 "LibXR::Print::Printf: embedded NUL bytes are not supported in the format literal");
166 static_assert(
167 result.compile_error != Printf::Error::MixedIndexing,
168 "LibXR::Print::Printf: positional and sequential arguments cannot be mixed");
169 static_assert(result.compile_error != Printf::Error::PositionalArgumentDisabled,
170 "LibXR::Print::Printf: positional argument indexing is disabled in the "
171 "current profile");
172 static_assert(result.compile_error != Printf::Error::DynamicField,
173 "LibXR::Print::Printf: dynamic width and precision are not supported");
174 static_assert(result.compile_error != Printf::Error::InvalidArgumentIndex,
175 "LibXR::Print::Printf: invalid positional argument index");
176 static_assert(result.compile_error != Printf::Error::InvalidSpecifier,
177 "LibXR::Print::Printf: invalid format specifier");
178 static_assert(result.compile_error != Printf::Error::InvalidLength,
179 "LibXR::Print::Printf: invalid length modifier");
180 static_assert(result.compile_error != Printf::Error::ConflictingArgument,
181 "LibXR::Print::Printf: one positional argument is reused with "
182 "incompatible conversions");
183 static_assert(result.compile_error != Printf::Error::TextOffsetOverflow,
184 "LibXR::Print::Printf: text pool offset is too large");
185 static_assert(result.compile_error != Printf::Error::TextSizeOverflow,
186 "LibXR::Print::Printf: text span is too large");
187 static_assert(source_analysis.error != Printf::Error::ConflictingArgument,
188 "LibXR::Print::Printf: one positional argument is reused with "
189 "incompatible conversions");
190
191 public:
196 inline static constexpr auto codes = result.codes;
201 inline static constexpr FormatProfile profile = result.profile;
202
207 [[nodiscard]] static constexpr auto ArgumentList() { return result.arg_info; }
208
213 [[nodiscard]] static constexpr auto ArgumentOrder() { return source_analysis.order; }
214
219 [[nodiscard]] static constexpr auto SourceArgumentList()
220 {
221 return source_analysis.args;
222 }
223
228 [[nodiscard]] static constexpr const auto& Codes() { return codes; }
229
234 [[nodiscard]] static constexpr FormatProfile Profile() { return profile; }
235
243 template <typename... Args>
244 [[nodiscard]] static consteval bool Matches()
245 {
246 return Detail::FormatArgument::template Matches<Args...>(SourceArgumentList());
247 }
248};
249} // 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
long long 长度修饰 / ll
@ IntMax
intmax_t 长度修饰 / j
@ PtrDiff
ptrdiff_t 长度修饰 / t
@ Short
short 长度修饰 / h
@ Size
size_t 长度修饰 / z
@ LongDouble
long double 长度修饰 / L
@ Default
无长度修饰符 / no length modifier
@ Long
long 长度修饰 / l
@ Char
char 长度修饰 / hh
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
@ DynamicField
不支持 * 宽度或精度 / * width / precision is not supported
static consteval Compiled< Source > Build()
在编译期解析并校验 printf 格式串 / Parse and validate a printf format at compile time
Definition printf.hpp:124
static consteval bool Matches()
判断 Args... 是否与启用的转换项精确匹配 / Return whether Args... exactly match the enabled conversions
Definition printf.hpp:138
static consteval bool Matches()
判断 Args... 是否就是当前编译格式期望的那组 C++ 参数类型 / Returns whether Args... are exactly the C++ argument types this...
Definition printf.hpp:244
static constexpr auto codes
返回运行期 writer 最终会执行的字节流 / Returns the final byte stream that the runtime writer will execute.
Definition printf.hpp:196
static constexpr FormatProfile Profile()
返回当前编译格式携带的 writer 分支摘要 / Returns the writer-branch summary carried by this compiled format.
Definition printf.hpp:234
static constexpr auto ArgumentOrder()
返回每个字段对应的是第几个源参数 / Returns, for each field, which source argument index it refers to.
Definition printf.hpp:213
static constexpr FormatProfile profile
返回当前格式需要哪些 writer 分支的编译期摘要 / Returns the compile-time summary of which writer branches this format ne...
Definition printf.hpp:201
static constexpr auto SourceArgumentList()
返回仅供编译期类型匹配使用的源参数列表 / Returns the source-argument list used only for compile-time type matching.
Definition printf.hpp:219
static constexpr auto ArgumentList()
返回运行期参数打包时要按字段顺序读取的参数列表 / Returns the field-ordered argument list the runtime packer will follow.
Definition printf.hpp:207
static constexpr const auto & Codes()
返回与 codes 相同的最终字节流 / Returns the same final byte stream as codes.
Definition printf.hpp:228
作为 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