libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
format_frontend_binding_base.hpp
1#pragma once
2
7{
8
14template <typename Arg>
15[[nodiscard]] consteval ArgumentSummary ClassifyArgument()
16{
17 using Traits = FormatArgument::TypeTraits<Arg>;
18 using Decayed = typename Traits::Decayed;
19 using Normalized = typename Traits::Normalized;
20
21 if constexpr (std::is_same_v<Decayed, bool>)
22 {
23 return ArgumentSummary{.kind = ArgumentKind::Bool};
24 }
25 else if constexpr (std::is_same_v<Decayed, char>)
26 {
27 return ArgumentSummary{.kind = ArgumentKind::Character};
28 }
29 else if constexpr (Traits::is_string_like)
30 {
31 return ArgumentSummary{.kind = ArgumentKind::String};
32 }
33 else if constexpr (Traits::is_pointer_like)
34 {
35 return ArgumentSummary{.kind = ArgumentKind::Pointer};
36 }
37 else if constexpr (Traits::is_signed_integer)
38 {
39 return ArgumentSummary{
40 .kind = ArgumentKind::Signed,
41 .uses_64bit_storage = sizeof(Normalized) > sizeof(int32_t),
42 };
43 }
44 else if constexpr (Traits::is_unsigned_integer)
45 {
46 return ArgumentSummary{
47 .kind = ArgumentKind::Unsigned,
48 .uses_64bit_storage = sizeof(Normalized) > sizeof(uint32_t),
49 };
50 }
51 else if constexpr (std::is_same_v<Decayed, float>)
52 {
53 return ArgumentSummary{.kind = ArgumentKind::Float32};
54 }
55 else if constexpr (std::is_same_v<Decayed, double>)
56 {
57 return ArgumentSummary{.kind = ArgumentKind::Float64};
58 }
59 else if constexpr (std::is_same_v<Decayed, long double>)
60 {
61 return ArgumentSummary{.kind = ArgumentKind::LongDouble};
62 }
63 else
64 {
65 return ArgumentSummary{};
66 }
67}
68
74template <typename... Args>
75[[nodiscard]] consteval auto ClassifyArguments()
76{
77 return std::array<ArgumentSummary, sizeof...(Args)>{ClassifyArgument<Args>()...};
78}
79
85[[nodiscard]] constexpr bool HasSignOption(const ParsedField& field)
86{
87 return field.force_sign || field.space_sign;
88}
89
94[[nodiscard]] constexpr uint8_t UnspecifiedPrecision()
95{
96 return std::numeric_limits<uint8_t>::max();
97}
98
105[[nodiscard]] constexpr uint8_t BuildFlags(const ParsedField& parsed, bool upper_case)
106{
107 uint8_t flags = 0;
108 if (parsed.align == Align::Left)
109 {
110 flags |= static_cast<uint8_t>(FormatFlag::LeftAlign);
111 }
112 if (parsed.align == Align::Center)
113 {
114 flags |= static_cast<uint8_t>(FormatFlag::CenterAlign);
115 }
116 if (parsed.force_sign)
117 {
118 flags |= static_cast<uint8_t>(FormatFlag::ForceSign);
119 }
120 if (parsed.space_sign)
121 {
122 flags |= static_cast<uint8_t>(FormatFlag::SpaceSign);
123 }
124 if (parsed.alternate)
125 {
126 flags |= static_cast<uint8_t>(FormatFlag::Alternate);
127 }
128 if (parsed.zero_pad && parsed.align == Align::None)
129 {
130 flags |= static_cast<uint8_t>(FormatFlag::ZeroPad);
131 }
132 if (upper_case)
133 {
134 flags |= static_cast<uint8_t>(FormatFlag::UpperCase);
135 }
136 return flags;
137}
138
147[[nodiscard]] constexpr FormatField MakeField(const ParsedField& parsed, FormatType type,
148 FormatPackKind pack, bool upper_case = false)
149{
150 return FormatField{
151 .type = type,
152 .pack = pack,
153 .rule = FormatArgumentRule::None,
154 .flags = BuildFlags(parsed, upper_case),
155 .fill = parsed.fill,
156 .width = parsed.width,
157 .precision = parsed.has_precision ? parsed.precision : UnspecifiedPrecision(),
158 };
159}
160
167[[nodiscard]] constexpr bool IsDefaultOr(char presentation, char expected)
168{
169 return presentation == 0 || presentation == expected;
170}
171
177[[nodiscard]] constexpr bool IsNonDecimalPresentation(char presentation)
178{
179 return presentation == 'b' || presentation == 'B' || presentation == 'o' ||
180 presentation == 'x' || presentation == 'X';
181}
182
187[[nodiscard]] constexpr char DefaultFloatPresentation()
188{
189 if (Config::enable_float_general)
190 {
191 return 'g';
192 }
193 if (Config::enable_float_fixed)
194 {
195 return 'f';
196 }
197 if (Config::enable_float_scientific)
198 {
199 return 'e';
200 }
201 return 0;
202}
203} // namespace ArgumentResolution
brace 前端共享的参数分类与字段形状构造辅助函数 / Shared brace-frontend argument classification and field-shape helpers
constexpr bool HasSignOption(const ParsedField &field)
判断某个已解析字段是否请求了显式符号策略 / Return whether one parsed field requested an explicit sign policy
constexpr FormatField MakeField(const ParsedField &parsed, FormatType type, FormatPackKind pack, bool upper_case=false)
根据 brace 字段属性构造一条共享 FormatField 记录 / Build one shared FormatField record from parsed brace-field prop...
consteval auto ClassifyArguments()
为 brace 前端归类整组 C++ 参数类型 / Classify one full C++ argument list for the brace frontend
constexpr uint8_t BuildFlags(const ParsedField &parsed, bool upper_case)
根据一个已解析 brace 字段构造共享 FormatFlag 位集合 / Build the shared FormatFlag bitset from one parsed brace field
constexpr bool IsNonDecimalPresentation(char presentation)
判断展示字符是否选择了非十进制整数族 / Return whether the presentation selects one non-decimal integer family
constexpr bool IsDefaultOr(char presentation, char expected)
判断展示字符是否缺省或等于目标字符 / Return whether the presentation is absent or matches the expected token
constexpr uint8_t UnspecifiedPrecision()
返回“未指定精度”的本地哨兵值 / Return the local sentinel meaning "precision not specified"
consteval ArgumentSummary ClassifyArgument()
将一个 C++ 参数类型归类到 brace 前端使用的本地参数类别 / Classify one C++ argument type into the brace frontend's local ca...
constexpr char DefaultFloatPresentation()
在当前功能开关下选择前端默认浮点展示字符 / Choose the frontend default float presentation under current feature gates