libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_executor_opcode.hpp
1#pragma once
2
13template <OutputSink Sink, FormatProfile Profile>
14Writer::Executor<Sink, Profile>::Executor(Sink& sink, const uint8_t* codes,
15 const uint8_t* args)
16 : sink_(sink),
17 codes_(codes),
18 args_(args)
19{
20}
21
28template <OutputSink Sink, FormatProfile Profile>
30{
31 while (true)
32 {
33 auto op = codes_.ReadOp();
34 if (op == FormatOp::End)
35 {
36 return ErrorCode::OK;
37 }
38
39 auto ec = DispatchOp(op);
40 if (ec != ErrorCode::OK)
41 {
42 return ec;
43 }
44 }
45}
46
53template <OutputSink Sink, FormatProfile Profile>
55{
56 switch (op)
57 {
58 case FormatOp::TextInline:
59 return WriteRaw(codes_.ReadInlineText());
60 case FormatOp::TextRef:
61 return WriteRaw(codes_.ReadTextRef());
62 case FormatOp::TextSpace:
63 return WriteRaw(" ");
64 case FormatOp::U32Dec:
65 if constexpr (!HasProfile(Profile, FormatProfile::U32) ||
66 !Config::enable_integer)
67 {
68 return ErrorCode::STATE_ERR;
69 }
70 return WriteU32Dec(args_.Read<uint32_t>());
71 case FormatOp::U32ZeroPadWidth:
72 if constexpr (!HasProfile(Profile, FormatProfile::U32) ||
73 !Config::enable_integer)
74 {
75 return ErrorCode::STATE_ERR;
76 }
77 return WriteU32ZeroPadWidth(codes_.Read<uint8_t>(), args_.Read<uint32_t>());
78 case FormatOp::StringRaw:
79 if constexpr (!HasProfile(Profile, FormatProfile::TextArg) ||
80 !Config::enable_text)
81 {
82 return ErrorCode::STATE_ERR;
83 }
84 return WriteStringRaw(args_.Read<std::string_view>());
85 case FormatOp::F32FixedPrec:
86 if constexpr (!HasProfile(Profile, FormatProfile::F32Fixed) ||
87 !FloatEnabled(FormatType::FloatFixed))
88 {
89 return ErrorCode::STATE_ERR;
90 }
91 return WriteF32FixedPrec(codes_.Read<uint8_t>(), args_.Read<float>());
92 case FormatOp::F64FixedPrec:
93 if constexpr (!HasProfile(Profile, FormatProfile::F64Fixed) ||
94 !FloatEnabled(FormatType::DoubleFixed))
95 {
96 return ErrorCode::STATE_ERR;
97 }
98 return WriteF64FixedPrec(codes_.Read<uint8_t>(), args_.Read<double>());
99 case FormatOp::GenericField:
100 if constexpr (!HasProfile(Profile, FormatProfile::Generic))
101 {
102 return ErrorCode::STATE_ERR;
103 }
104 return DispatchGenericField(codes_.ReadFormatType());
105 case FormatOp::End:
106 default:
107 return ErrorCode::STATE_ERR;
108 }
109}
ErrorCode Run()
持续执行记录流,直到遇到 FormatOp::End / Run until the compiled record stream reaches FormatOp::End
Executor(Sink &sink, const uint8_t *codes, const uint8_t *args)
将一个输出端、一份编译字节块和一份参数字节块绑定起来 / Bind one sink with one compiled byte blob and one packed argument blob
ErrorCode DispatchOp(FormatOp op)
将一个运行期操作码分发到选中的特化路径 / Dispatch one runtime opcode to the selected specialized path