libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
format_frontend_binding_base.hpp
1#pragma once
2
8{
9
17template <typename Arg>
18[[nodiscard]] consteval ArgumentSummary ClassifyArgument()
19{
20 using Traits = FormatArgument::TypeTraits<Arg>;
21 using Decayed = typename Traits::Decayed;
22 using Normalized = typename Traits::Normalized;
23
24 if constexpr (std::is_same_v<Decayed, bool>)
25 {
26 return ArgumentSummary{.kind = ArgumentKind::Bool};
27 }
28 else if constexpr (std::is_same_v<Decayed, char>)
29 {
30 return ArgumentSummary{.kind = ArgumentKind::Character};
31 }
32 else if constexpr (Traits::is_string_like)
33 {
34 return ArgumentSummary{.kind = ArgumentKind::String};
35 }
36 else if constexpr (Traits::is_pointer_like)
37 {
38 return ArgumentSummary{.kind = ArgumentKind::Pointer};
39 }
40 else if constexpr (Traits::is_signed_integer)
41 {
42 return ArgumentSummary{
43 .kind = ArgumentKind::Signed,
44 .uses_64bit_storage = sizeof(Normalized) > sizeof(int32_t),
45 };
46 }
47 else if constexpr (Traits::is_unsigned_integer)
48 {
49 return ArgumentSummary{
50 .kind = ArgumentKind::Unsigned,
51 .uses_64bit_storage = sizeof(Normalized) > sizeof(uint32_t),
52 };
53 }
54 else if constexpr (std::is_same_v<Decayed, float>)
55 {
56 return ArgumentSummary{.kind = ArgumentKind::Float32};
57 }
58 else if constexpr (std::is_same_v<Decayed, double>)
59 {
60 return ArgumentSummary{.kind = ArgumentKind::Float64};
61 }
62 else if constexpr (std::is_same_v<Decayed, long double>)
63 {
64 return ArgumentSummary{.kind = ArgumentKind::LongDouble};
65 }
66 else
67 {
68 return ArgumentSummary{};
69 }
70}
71
79template <typename... Args>
80[[nodiscard]] consteval auto ClassifyArguments()
81{
82 return std::array<ArgumentSummary, sizeof...(Args)>{ClassifyArgument<Args>()...};
83}
84
92[[nodiscard]] constexpr bool HasSignOption(const ParsedField& field)
93{
94 return field.force_sign || field.space_sign;
95}
96
103[[nodiscard]] constexpr uint8_t UnspecifiedPrecision()
104{
105 return std::numeric_limits<uint8_t>::max();
106}
107
117[[nodiscard]] constexpr uint8_t BuildFlags(const ParsedField& parsed, bool upper_case)
118{
119 uint8_t flags = 0;
120 if (parsed.align == Align::Left)
121 {
122 flags |= static_cast<uint8_t>(FormatFlag::LeftAlign);
123 }
124 if (parsed.align == Align::Center)
125 {
126 flags |= static_cast<uint8_t>(FormatFlag::CenterAlign);
127 }
128 if (parsed.force_sign)
129 {
130 flags |= static_cast<uint8_t>(FormatFlag::ForceSign);
131 }
132 if (parsed.space_sign)
133 {
134 flags |= static_cast<uint8_t>(FormatFlag::SpaceSign);
135 }
136 if (parsed.alternate)
137 {
138 flags |= static_cast<uint8_t>(FormatFlag::Alternate);
139 }
140 if (parsed.zero_pad && parsed.align == Align::None)
141 {
142 flags |= static_cast<uint8_t>(FormatFlag::ZeroPad);
143 }
144 if (upper_case)
145 {
146 flags |= static_cast<uint8_t>(FormatFlag::UpperCase);
147 }
148 return flags;
149}
150
162[[nodiscard]] constexpr FormatField MakeField(const ParsedField& parsed, FormatType type,
163 FormatPackKind pack,
164 bool upper_case = false)
165{
166 return FormatField{
167 .type = type,
168 .pack = pack,
169 .rule = FormatArgumentRule::None,
170 .flags = BuildFlags(parsed, upper_case),
171 .fill = parsed.fill,
172 .width = parsed.width,
173 .precision = parsed.has_precision ? parsed.precision : UnspecifiedPrecision(),
174 };
175}
176
186[[nodiscard]] constexpr bool IsDefaultOr(char presentation, char expected)
187{
188 return presentation == 0 || presentation == expected;
189}
190
198[[nodiscard]] constexpr bool IsNonDecimalPresentation(char presentation)
199{
200 return presentation == 'b' || presentation == 'B' || presentation == 'o' ||
201 presentation == 'x' || presentation == 'X';
202}
203
210[[nodiscard]] constexpr char DefaultFloatPresentation()
211{
212 if (Config::enable_float_general)
213 {
214 return 'g';
215 }
216 if (Config::enable_float_fixed)
217 {
218 return 'f';
219 }
220 if (Config::enable_float_scientific)
221 {
222 return 'e';
223 }
224 return 0;
225}
226} // 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 notspecified"
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