libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
format_protocol.hpp
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5#include <string_view>
6#include <type_traits>
7
23#ifndef LIBXR_PRINT_ENABLE_INTEGER
24#define LIBXR_PRINT_ENABLE_INTEGER 1
25#endif
26
27#ifndef LIBXR_PRINT_ENABLE_TEXT
28#define LIBXR_PRINT_ENABLE_TEXT 1
29#endif
30
31#ifndef LIBXR_PRINT_ENABLE_POINTER
32#define LIBXR_PRINT_ENABLE_POINTER 0
33#endif
34
35#ifndef LIBXR_PRINT_ENABLE_FLOAT
36#define LIBXR_PRINT_ENABLE_FLOAT 1
37#endif
38
39#ifndef LIBXR_PRINT_INTEGER_ENABLE_BASE8_16
40#define LIBXR_PRINT_INTEGER_ENABLE_BASE8_16 1
41#endif
42
43#ifndef LIBXR_PRINT_INTEGER_ENABLE_64BIT
44#define LIBXR_PRINT_INTEGER_ENABLE_64BIT 0
45#endif
46
47#ifndef LIBXR_PRINT_FLOAT_ENABLE_FIXED
48#define LIBXR_PRINT_FLOAT_ENABLE_FIXED 1
49#endif
50
51#ifndef LIBXR_PRINT_FLOAT_ENABLE_DOUBLE
52#define LIBXR_PRINT_FLOAT_ENABLE_DOUBLE 0
53#endif
54
55#ifndef LIBXR_PRINT_FLOAT_ENABLE_SCIENTIFIC
56#define LIBXR_PRINT_FLOAT_ENABLE_SCIENTIFIC 0
57#endif
58
59#ifndef LIBXR_PRINT_FLOAT_ENABLE_GENERAL
60#define LIBXR_PRINT_FLOAT_ENABLE_GENERAL 0
61#endif
62
63#ifndef LIBXR_PRINT_FLOAT_ENABLE_LONG_DOUBLE
64#define LIBXR_PRINT_FLOAT_ENABLE_LONG_DOUBLE 0
65#endif
66
67#ifndef LIBXR_PRINT_ENABLE_WIDTH
68#define LIBXR_PRINT_ENABLE_WIDTH 1
69#endif
70
71#ifndef LIBXR_PRINT_ENABLE_PRECISION
72#define LIBXR_PRINT_ENABLE_PRECISION 1
73#endif
74
75#ifndef LIBXR_PRINT_ENABLE_ALTERNATE
76#define LIBXR_PRINT_ENABLE_ALTERNATE 0
77#endif
78
79#ifndef LIBXR_PRINT_ENABLE_EXPLICIT_ARGUMENT_INDEXING
80#define LIBXR_PRINT_ENABLE_EXPLICIT_ARGUMENT_INDEXING 0
81#endif
82
83namespace LibXR::Print::Config
84{
86inline constexpr bool enable_integer = LIBXR_PRINT_ENABLE_INTEGER;
88inline constexpr bool enable_text = LIBXR_PRINT_ENABLE_TEXT;
90inline constexpr bool enable_pointer = LIBXR_PRINT_ENABLE_POINTER;
92inline constexpr bool enable_float = LIBXR_PRINT_ENABLE_FLOAT;
93
95inline constexpr bool enable_integer_base8_16 =
96 enable_integer && LIBXR_PRINT_INTEGER_ENABLE_BASE8_16;
98inline constexpr bool enable_integer_64bit =
99 enable_integer && LIBXR_PRINT_INTEGER_ENABLE_64BIT;
100
102inline constexpr bool enable_float_fixed =
103 enable_float && LIBXR_PRINT_FLOAT_ENABLE_FIXED;
105inline constexpr bool enable_float_double =
106 enable_float && LIBXR_PRINT_FLOAT_ENABLE_DOUBLE;
108inline constexpr bool enable_float_scientific =
109 enable_float && LIBXR_PRINT_FLOAT_ENABLE_SCIENTIFIC;
111inline constexpr bool enable_float_general =
112 enable_float && LIBXR_PRINT_FLOAT_ENABLE_GENERAL;
114inline constexpr bool enable_float_long_double =
115 enable_float_double && LIBXR_PRINT_FLOAT_ENABLE_LONG_DOUBLE;
116
118inline constexpr bool enable_width = LIBXR_PRINT_ENABLE_WIDTH;
120inline constexpr bool enable_precision = LIBXR_PRINT_ENABLE_PRECISION;
122inline constexpr bool enable_alternate = LIBXR_PRINT_ENABLE_ALTERNATE;
124inline constexpr bool enable_explicit_argument_indexing =
125 LIBXR_PRINT_ENABLE_EXPLICIT_ARGUMENT_INDEXING;
126} // namespace LibXR::Print::Config
127
128namespace LibXR::Print
129{
149enum class FormatArgumentRule : uint8_t
150{
151 None,
152 SignedAny,
153 SignedChar,
154 SignedShort,
155 SignedLong,
156 SignedLongLong,
157 SignedIntMax,
158 SignedSize,
159 SignedPtrDiff,
160 UnsignedAny,
161 UnsignedChar,
162 UnsignedShort,
163 UnsignedLong,
164 UnsignedLongLong,
165 UnsignedIntMax,
166 UnsignedSize,
167 UnsignedPtrDiff,
168 Pointer,
169 Character,
170 String,
171 Float,
172 LongDouble,
173};
174
183enum class FormatFlag : uint8_t
184{
185 LeftAlign = 1U << 0,
186 ForceSign = 1U << 1,
187 SpaceSign = 1U << 2,
188 Alternate = 1U << 3,
189 ZeroPad = 1U << 4,
190 UpperCase = 1U << 5,
191 CenterAlign = 1U << 6,
192};
193
205enum class FormatOp : uint8_t
206{
207 TextInline = 0x01,
208 TextRef = 0x02,
209 TextSpace = 0x03,
210 U32Dec = 0x10,
211 U32ZeroPadWidth = 0x11,
212 StringRaw = 0x20,
213 F32FixedPrec = 0x30,
214 F64FixedPrec = 0x31,
215 GenericField = 0xF0,
216 End = 0xFF,
217};
218
223enum class FormatType : uint8_t
224{
225 End,
226 TextInline,
227 TextRef,
228 TextSpace,
229 Signed32,
230 Signed64,
231 Unsigned32,
232 Unsigned64,
233 Binary32,
234 Binary64,
235 Octal32,
236 Octal64,
237 HexLower32,
238 HexLower64,
239 HexUpper32,
240 HexUpper64,
241 Pointer,
242 Character,
243 String,
244 FloatFixed,
245 FloatScientific,
246 FloatGeneral,
247 DoubleFixed,
248 DoubleScientific,
249 DoubleGeneral,
250 LongDoubleFixed,
251 LongDoubleScientific,
252 LongDoubleGeneral,
253};
254
263enum class FormatPackKind : uint8_t
264{
265 U32,
266 U64,
267 I32,
268 I64,
269 Pointer,
270 Character,
271 StringView,
272 F32,
273 F64,
274 LongDouble,
275};
276
285enum class FormatProfile : uint8_t
286{
287 None = 0,
288 U32 = 1U << 0,
289 TextArg = 1U << 1,
290 F32Fixed = 1U << 2,
291 F64Fixed = 1U << 3,
292 Generic = 1U << 7,
293};
294
296[[nodiscard]] constexpr FormatProfile operator|(FormatProfile left, FormatProfile right)
297{
298 return static_cast<FormatProfile>(static_cast<uint8_t>(left) |
299 static_cast<uint8_t>(right));
300}
301
303constexpr FormatProfile& operator|=(FormatProfile& left, FormatProfile right)
304{
305 left = left | right;
306 return left;
307}
308
310[[nodiscard]] constexpr bool HasProfile(FormatProfile profile, FormatProfile bit)
311{
312 return (static_cast<uint8_t>(profile) & static_cast<uint8_t>(bit)) != 0;
313}
314
343{
344 FormatPackKind pack{};
345 FormatArgumentRule rule =
346 FormatArgumentRule::None;
347};
348static_assert(sizeof(FormatArgumentInfo) == 2,
349 "LibXR::Print::FormatArgumentInfo must stay tightly packed");
350
367{
368 FormatType type = FormatType::End;
369 FormatPackKind pack{};
370 FormatArgumentRule rule =
371 FormatArgumentRule::None;
372 uint8_t flags = 0;
373 char fill = ' ';
374 uint8_t width = 0;
375 uint8_t precision = 0xFF;
376};
377
386[[nodiscard]] constexpr size_t FormatArgumentBytes(FormatPackKind pack)
387{
388 switch (pack)
389 {
390 case FormatPackKind::I32:
391 return sizeof(int32_t);
392 case FormatPackKind::I64:
393 return sizeof(int64_t);
394 case FormatPackKind::U32:
395 return sizeof(uint32_t);
396 case FormatPackKind::U64:
397 return sizeof(uint64_t);
398 case FormatPackKind::Pointer:
399 return sizeof(uintptr_t);
400 case FormatPackKind::Character:
401 return sizeof(char);
402 case FormatPackKind::StringView:
403 return sizeof(std::string_view);
404 case FormatPackKind::F32:
405 return sizeof(float);
406 case FormatPackKind::F64:
407 return sizeof(double);
408 case FormatPackKind::LongDouble:
409 return sizeof(long double);
410 }
411
412 return 0;
413}
414
415} // namespace LibXR::Print
Compiled-format runtime contract consumed by Writer.
FormatArgumentRule rule
compile-time argument rule / 编译期实参匹配规则
FormatPackKind pack
packed runtime storage kind / 运行期打包存储类别
Normalized value-field semantics shared by all formatting frontends.
uint8_t flags
FormatFlag bitset / 字段修饰位集合
FormatArgumentRule rule
compile-time argument rule / 编译期实参匹配规则
FormatType type
semantic render category / 语义写出类别
FormatPackKind pack
packed runtime storage kind / 运行期打包存储类别
char fill
field fill character / 字段填充字符
uint8_t precision
parsed precision, or unspecified / 已解析精度,或未指定
uint8_t width
parsed field width / 已解析的字段宽度