libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
writer_reader.hpp
1#pragma once
2
3#include <cstdint>
4#include <cstring>
5#include <string_view>
6
11{
12 public:
17 explicit CodeReader(const uint8_t* codes) : pos_(codes), base_(codes)
18 {
19 }
20
25 [[nodiscard]] FormatOp ReadOp() { return static_cast<FormatOp>(*pos_++); }
26
32 template <typename T>
33 [[nodiscard]] T Read()
34 {
35 T value{};
36 std::memcpy(&value, pos_, sizeof(T));
37 pos_ += sizeof(T);
38 return value;
39 }
40
45 [[nodiscard]] FormatType ReadFormatType() { return static_cast<FormatType>(*pos_++); }
46
56 [[nodiscard]] Spec ReadSpec()
57 {
58 return Spec{.flags = *pos_++,
59 .fill = static_cast<char>(*pos_++),
60 .width = *pos_++,
61 .precision = *pos_++};
62 }
63
68 [[nodiscard]] std::string_view ReadInlineText()
69 {
70 auto text = reinterpret_cast<const char*>(pos_);
71 size_t size = std::strlen(text);
72 pos_ += size + 1;
73 return std::string_view(text, size);
74 }
75
83 [[nodiscard]] std::string_view ReadTextRef()
84 {
85 auto offset = Read<uint16_t>();
86 auto size = Read<uint16_t>();
87 auto text = reinterpret_cast<const char*>(base_ + offset);
88 return std::string_view(text, size);
89 }
90
91 private:
92 const uint8_t* pos_ = nullptr;
93 const uint8_t* base_ = nullptr;
94};
95
100{
101 public:
106 explicit ArgumentReader(const uint8_t* data) : pos_(data) {}
107
113 template <typename T>
114 [[nodiscard]] T Read()
115 {
116 T value{};
117 std::memcpy(&value, pos_, sizeof(T));
118 pos_ += sizeof(T);
119 return value;
120 }
121
122 private:
123 const uint8_t* pos_ = nullptr;
124};
运行期参数字节块的顺序读取器 / Sequential reader for the packed runtime argument byte blob
ArgumentReader(const uint8_t *data)
从单个运行期参数打包字节块构造读取器 / Create a reader over one packed runtime argument blob
T Read()
以无对齐要求的方式读取一个已打包参数值 / Read one packed argument value without requiring alignment
编译后记录流的顺序读取器 / Sequential reader for the compiled record stream
std::string_view ReadInlineText()
读取内嵌在记录流中的短文本 / Read a null-terminated short text payload embedded in the record stream
std::string_view ReadTextRef()
读取指向尾部文本池的偏移和长度 / Read an offset-size pair pointing into the trailing text pool
CodeReader(const uint8_t *codes)
从单个编译字节块起点构造读取器 / Create a reader over the beginning of one compiled byte blob
T Read()
读取编译期发射器按本机字节序写入的 POD 值 / Read a native-endian POD value emitted by the compile-time emitter
FormatOp ReadOp()
读取下一条运行期操作码 / Read the next runtime opcode
Spec ReadSpec()
读取紧跟在 GenericField 类型字节后的 4 字节字段载荷 / Read the 4-byte field payload that follows one GenericField type...
FormatType ReadFormatType()
读取 GenericField 载荷中的语义类型字节 / Read the semantic type byte carried by one GenericField payload