libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_executor_opcode.hpp
1#pragma once
2
16template <OutputSink Sink>
17Writer::Executor<Sink>::Executor(Sink& sink, const uint8_t* codes, const uint8_t* args)
18 : sink_(sink), codes_(codes), args_(args)
19{
20}
21
31template <OutputSink Sink>
32template <FormatProfile Profile>
34{
35 while (true)
36 {
37 auto op = codes_.ReadOp();
38 if (op == FormatOp::End)
39 {
40 return ErrorCode::OK;
41 }
42
43 auto ec = DispatchOp<Profile>(op);
44 if (ec != ErrorCode::OK)
45 {
46 return ec;
47 }
48 }
49}
50
60template <OutputSink Sink>
61template <FormatProfile Profile>
63{
64 switch (op)
65 {
66 case FormatOp::TextInline:
67 return WriteRaw(codes_.ReadInlineText());
68 case FormatOp::TextRef:
69 return WriteRaw(codes_.ReadTextRef());
70 case FormatOp::TextSpace:
71 return WriteRaw(" ");
72 case FormatOp::U32Dec:
73 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
74 !Config::enable_integer)
75 {
76 return ErrorCode::STATE_ERR;
77 }
78 return WriteU32Dec(args_.Read<uint32_t>());
79 case FormatOp::I32Dec:
80 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
81 !Config::enable_integer)
82 {
83 return ErrorCode::STATE_ERR;
84 }
85 return WriteI32Dec(args_.Read<int32_t>());
86 case FormatOp::U32ZeroPadWidth:
87 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
88 !Config::enable_integer)
89 {
90 return ErrorCode::STATE_ERR;
91 }
92 return WriteU32ZeroPadWidth(codes_.Read<uint8_t>(), args_.Read<uint32_t>());
93 case FormatOp::U32Binary:
94 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
95 !Config::enable_integer || !Config::enable_integer_base8_16)
96 {
97 return ErrorCode::STATE_ERR;
98 }
99 return WriteU32Base<2>(args_.Read<uint32_t>());
100 case FormatOp::U32Octal:
101 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
102 !Config::enable_integer || !Config::enable_integer_base8_16)
103 {
104 return ErrorCode::STATE_ERR;
105 }
106 return WriteU32Base<8>(args_.Read<uint32_t>());
107 case FormatOp::U32HexLower:
108 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
109 !Config::enable_integer || !Config::enable_integer_base8_16)
110 {
111 return ErrorCode::STATE_ERR;
112 }
113 return WriteU32Base<16>(args_.Read<uint32_t>());
114 case FormatOp::U32HexUpper:
115 if constexpr (!HasProfile(Profile, FormatProfile::NarrowInt) ||
116 !Config::enable_integer || !Config::enable_integer_base8_16)
117 {
118 return ErrorCode::STATE_ERR;
119 }
120 return WriteU32Base<16, true>(args_.Read<uint32_t>());
121 case FormatOp::StringRaw:
122 if constexpr (!HasProfile(Profile, FormatProfile::TextArg) || !Config::enable_text)
123 {
124 return ErrorCode::STATE_ERR;
125 }
126 return WriteStringRaw(args_.Read<std::string_view>());
127 case FormatOp::CharacterRaw:
128 if constexpr (!HasProfile(Profile, FormatProfile::TextArg) || !Config::enable_text)
129 {
130 return ErrorCode::STATE_ERR;
131 }
132 return WriteCharacterRaw(args_.Read<char>());
133 case FormatOp::GenericField:
134 if constexpr (!HasProfile(Profile, FormatProfile::Generic))
135 {
136 return ErrorCode::STATE_ERR;
137 }
138 return DispatchGenericField<Profile>(codes_.ReadFormatType());
139 case FormatOp::End:
140 default:
141 return ErrorCode::STATE_ERR;
142 }
143}
ErrorCode Run()
持续执行记录流,直到遇到 FormatOp::End / Run until the compiled record stream reaches FormatOp::End
ErrorCode DispatchOp(FormatOp op)
将一个运行期操作码分发到选中的特化路径 / Dispatch one runtime opcode to the selected specialized path
Executor(Sink &sink, const uint8_t *codes, const uint8_t *args)
将一个输出端、一份编译字节块和一份参数字节块绑定起来 / Bind one sink with one compiled byte blob and one packed argument blob