libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::Print::FormatCompiler< Frontend > Class Template Reference

Shared compile-time backend that lowers frontend text/field events into one final compiled format. More...

#include <format_compile.hpp>

Data Structures

struct  ResultData
 Final tightly-sized compiled format emitted by this backend instance. More...
 
struct  ScratchBuilder
 Single-pass scratch builder driven directly by frontend events. More...
 

Static Public Member Functions

static consteval auto Compile ()
 Compiles the frontend into one final compiled format.
 

Private Types

using Error = typename Frontend::ErrorType
 
template<size_t BlobBytes, size_t ArgCount>
using Result = ResultData<BlobBytes, ArgCount>
 

Static Private Member Functions

static consteval void EmitByte (auto &data, size_t &out, uint8_t value)
 
template<typename T >
static consteval void EmitNative (auto &data, size_t &out, T value)
 
template<typename T >
static consteval T ReadNative (const auto &data, size_t &pos)
 
static consteval auto Failed (Error error)
 
static consteval FormatProfile ProfileForOp (FormatOp op)
 Maps one opcode family to the runtime executor profile bit it requires.
 
static consteval FormatOp FastFieldOp (const FormatField &field)
 Chooses the narrowest opcode that preserves one normalized field's behavior.
 

Static Private Attributes

static constexpr size_t inline_text_limit = 2 * sizeof(size_t) - 1
 Maximum inline literal size before the backend spills to TextRef. / 超过该长度后,字面文本会从内嵌模式切换为 TextRef.
 
static constexpr size_t max_code_bytes = 3 * Frontend::SourceSize() + 1
 
static constexpr size_t max_text_pool_bytes = Frontend::SourceSize()
 
static constexpr size_t max_arg_count = Frontend::SourceSize()
 
static constexpr uint8_t unspecified_precision
 

Detailed Description

template<typename Frontend>
class LibXR::Print::FormatCompiler< Frontend >

Shared compile-time backend that lowers frontend text/field events into one final compiled format.

共享编译期后端,将前端产生的文本/字段事件直接降为最终编译格式。

Frontend contract: The frontend must provide the members using ErrorType, static constexpr const char* SourceData(), static constexpr size_t SourceSize(), and static consteval ErrorType Walk(visitor). 前端协议: 前端必须提供如下成员:using ErrorType、static constexpr const char* SourceData()、static constexpr size_t SourceSize()、以及 static consteval ErrorType Walk(visitor)。

Walk(visitor) must emit source-ordered events through visitor.Text(offset, text_size) and visitor.Field(const FormatField&). Walk(visitor) 必须按源串顺序通过 visitor.Text(offset, text_size) 与 visitor.Field(const FormatField&) 发射事件。

The shared backend now walks the frontend exactly once. During that walk it accumulates code bytes, text-pool bytes, argument metadata, and packed-argument layout into one scratch builder, then materializes the final tightly-sized result in one local finalize step. 共享后端现在只遍历前端一次:遍历时同步累积码流、文本池、参数元信息与参数打包布局, 然后在本地收尾阶段生成最终精确尺寸结果。

Definition at line 42 of file format_compile.hpp.

Member Typedef Documentation

◆ Error

template<typename Frontend >
using LibXR::Print::FormatCompiler< Frontend >::Error = typename Frontend::ErrorType
private

Definition at line 49 of file format_compile.hpp.

◆ Result

template<typename Frontend >
template<size_t BlobBytes, size_t ArgCount>
using LibXR::Print::FormatCompiler< Frontend >::Result = ResultData<BlobBytes, ArgCount>
private

Definition at line 71 of file format_compile.hpp.

Member Function Documentation

◆ Compile()

template<typename Frontend >
static consteval auto LibXR::Print::FormatCompiler< Frontend >::Compile ( )
inlinestaticnodiscardconsteval

Compiles the frontend into one final compiled format.

将前端编译为一份最终编译格式。

Definition at line 412 of file format_compile.hpp.

413 {
414 constexpr auto scratch = []() consteval {
415 ScratchBuilder builder{};
416 builder.frontend_error = Frontend::Walk(builder);
417 return builder;
418 }();
419
420 if constexpr (scratch.frontend_error != Error::None)
421 {
422 return Failed(scratch.frontend_error);
423 }
424 else
425 {
426 return scratch.template Finish<scratch.FinalCodeBytes(),
427 scratch.FinalBlobBytes(), scratch.arg_count>();
428 }
429 }

◆ EmitByte()

template<typename Frontend >
static consteval void LibXR::Print::FormatCompiler< Frontend >::EmitByte ( auto & data,
size_t & out,
uint8_t value )
inlinestaticconstevalprivate

Definition at line 91 of file format_compile.hpp.

92 {
93 data[out++] = value;
94 }

◆ EmitNative()

template<typename Frontend >
template<typename T >
static consteval void LibXR::Print::FormatCompiler< Frontend >::EmitNative ( auto & data,
size_t & out,
T value )
inlinestaticconstevalprivate

Definition at line 97 of file format_compile.hpp.

98 {
99 auto bytes = std::bit_cast<std::array<uint8_t, sizeof(T)>>(value);
100 for (auto byte : bytes)
101 {
102 data[out++] = byte;
103 }
104 }

◆ Failed()

template<typename Frontend >
static consteval auto LibXR::Print::FormatCompiler< Frontend >::Failed ( Error error)
inlinestaticnodiscardconstevalprivate

Definition at line 117 of file format_compile.hpp.

118 {
119 Result<1, 0> result{};
120 result.compile_error = error;
121 return result;
122 }

◆ FastFieldOp()

template<typename Frontend >
static consteval FormatOp LibXR::Print::FormatCompiler< Frontend >::FastFieldOp ( const FormatField & field)
inlinestaticnodiscardconstevalprivate

Chooses the narrowest opcode that preserves one normalized field's behavior.

为一个规范化字段选择在行为不变前提下最窄的操作码。

Definition at line 157 of file format_compile.hpp.

158 {
159 if (field.type == FormatType::Unsigned32 && field.pack == FormatPackKind::U32 &&
160 field.flags == 0 && field.fill == ' ' && field.width == 0 &&
161 field.precision == unspecified_precision)
162 {
163 return FormatOp::U32Dec;
164 }
165
166 if (field.type == FormatType::Unsigned32 && field.pack == FormatPackKind::U32 &&
167 field.flags == static_cast<uint8_t>(FormatFlag::ZeroPad) &&
168 field.fill == ' ' && field.width != 0 &&
169 field.precision == unspecified_precision)
170 {
171 return FormatOp::U32ZeroPadWidth;
172 }
173
174 if (field.type == FormatType::String &&
175 field.pack == FormatPackKind::StringView && field.flags == 0 &&
176 field.fill == ' ' &&
177 field.width == 0 && field.precision == unspecified_precision)
178 {
179 return FormatOp::StringRaw;
180 }
181
182 if (field.type == FormatType::FloatFixed && field.pack == FormatPackKind::F32 &&
183 field.flags == 0 && field.fill == ' ' && field.width == 0 &&
184 field.precision != unspecified_precision)
185 {
186 return FormatOp::F32FixedPrec;
187 }
188
189 if (field.type == FormatType::DoubleFixed && field.pack == FormatPackKind::F64 &&
190 field.flags == 0 && field.fill == ' ' && field.width == 0 &&
191 field.precision != unspecified_precision)
192 {
193 return FormatOp::F64FixedPrec;
194 }
195
196 return FormatOp::GenericField;
197 }

◆ ProfileForOp()

template<typename Frontend >
static consteval FormatProfile LibXR::Print::FormatCompiler< Frontend >::ProfileForOp ( FormatOp op)
inlinestaticnodiscardconstevalprivate

Maps one opcode family to the runtime executor profile bit it requires.

将一个操作码族映射到它所需的运行期执行器 profile 位。

Definition at line 128 of file format_compile.hpp.

129 {
130 switch (op)
131 {
132 case FormatOp::U32Dec:
133 case FormatOp::U32ZeroPadWidth:
134 return FormatProfile::U32;
135 case FormatOp::StringRaw:
136 return FormatProfile::TextArg;
137 case FormatOp::F32FixedPrec:
138 return FormatProfile::F32Fixed;
139 case FormatOp::F64FixedPrec:
140 return FormatProfile::F64Fixed;
141 case FormatOp::GenericField:
142 return FormatProfile::Generic;
143 case FormatOp::TextInline:
144 case FormatOp::TextRef:
145 case FormatOp::TextSpace:
146 case FormatOp::End:
147 return FormatProfile::None;
148 }
149
150 return FormatProfile::None;
151 }

◆ ReadNative()

template<typename Frontend >
template<typename T >
static consteval T LibXR::Print::FormatCompiler< Frontend >::ReadNative ( const auto & data,
size_t & pos )
inlinestaticnodiscardconstevalprivate

Definition at line 107 of file format_compile.hpp.

108 {
109 std::array<uint8_t, sizeof(T)> bytes{};
110 for (size_t i = 0; i < sizeof(T); ++i)
111 {
112 bytes[i] = data[pos++];
113 }
114 return std::bit_cast<T>(bytes);
115 }

Field Documentation

◆ inline_text_limit

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::inline_text_limit = 2 * sizeof(size_t) - 1
staticconstexprprivate

Maximum inline literal size before the backend spills to TextRef. / 超过该长度后,字面文本会从内嵌模式切换为 TextRef.

Definition at line 74 of file format_compile.hpp.

◆ max_arg_count

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_arg_count = Frontend::SourceSize()
staticconstexprprivate

Definition at line 87 of file format_compile.hpp.

◆ max_code_bytes

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_code_bytes = 3 * Frontend::SourceSize() + 1
staticconstexprprivate

Definition at line 85 of file format_compile.hpp.

◆ max_text_pool_bytes

template<typename Frontend >
size_t LibXR::Print::FormatCompiler< Frontend >::max_text_pool_bytes = Frontend::SourceSize()
staticconstexprprivate

Definition at line 86 of file format_compile.hpp.

◆ unspecified_precision

template<typename Frontend >
uint8_t LibXR::Print::FormatCompiler< Frontend >::unspecified_precision
staticconstexprprivate
Initial value:
=
std::numeric_limits<uint8_t>::max()

Definition at line 88 of file format_compile.hpp.


The documentation for this class was generated from the following file: