libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stdio.hpp
1#pragma once
2
3#include <cstddef>
4#include <string_view>
5#include <tuple>
6#include <utility>
7
8#include "mutex.hpp"
9#include "print.hpp"
10#include "read_port.hpp"
11#include "write_port.hpp"
12
13namespace LibXR
14{
15
19class STDIO
20{
21 public:
22 // Shared global stdio binding state.
23 // 共享的全局 stdio 绑定状态。
24 // NOLINTBEGIN
25 static inline ReadPort* read_ = nullptr;
26 static inline WritePort* write_ = nullptr;
27 static inline LibXR::Mutex* write_mutex_ =
28 nullptr;
30 nullptr;
31 // NOLINTEND
32
33 private:
34 // Compiled-format bridge shared by the brace and printf frontends.
35 // brace 与 printf 两个前端共用的编译格式桥接层。
36
41 {
42 public:
47 explicit CompiledSink(WritePort::Stream& stream);
48
54 [[nodiscard]] ErrorCode Write(std::string_view chunk);
55
60 [[nodiscard]] size_t RetainedSize() const { return retained_size_; }
61
62 private:
64 size_t retained_size_ = 0;
65 bool saturated_ = false;
66 };
67
68 // Type-erased bridge for one compiled STDIO call.
69 // 一次编译格式 STDIO 调用的类型擦除桥接函数。
70 using CompiledWriteFun = ErrorCode (*)(void* context, CompiledSink& sink);
71
77 template <typename CompiledFormat, typename... Args>
79 {
80 const CompiledFormat& format;
81 std::tuple<Args&&...> args;
82
89 [[nodiscard]] static ErrorCode Write(void* context, CompiledSink& sink)
90 {
91 auto& compiled_call = *static_cast<CompiledCall*>(context);
92 return std::apply(
93 [&](auto&&... unpacked) -> ErrorCode
94 {
95 return Print::Write(sink, compiled_call.format,
96 std::forward<decltype(unpacked)>(unpacked)...);
97 },
98 compiled_call.args);
99 }
100 };
101
114 [[nodiscard]] static int WriteCompiledToStream(WritePort::Stream& stream,
115 void* context,
116 CompiledWriteFun write_fun);
117
129 [[nodiscard]] static int WriteCompiledSession(void* context, CompiledWriteFun write_fun);
130
142 template <typename Call>
143 [[nodiscard]] static int RunCompiledSession(Call& call)
144 {
145 if (!BeginWriteSession())
146 {
147 return -1;
148 }
149
150 return WriteCompiledSession(&call, &Call::Write);
151 }
152
161 template <typename CompiledFormat, typename... Args>
162 [[nodiscard]] static int RunCompiled(const CompiledFormat& format, Args&&... args)
163 {
164 CompiledCall<CompiledFormat, Args...> call{
165 format, std::forward_as_tuple(std::forward<Args>(args)...)};
166 return RunCompiledSession(call);
167 }
168
173 [[nodiscard]] static bool BeginWriteSession();
174
182 [[nodiscard]] static int FinishWriteSession(WritePort::Stream& stream,
183 size_t retained_size,
184 ErrorCode format_result);
185
186 public:
187
195 template <Print::Text Source, typename... Args>
196 static int Print(Args&&... args)
197 {
198 constexpr LibXR::Format<Source> format{};
199 return RunCompiled(format, std::forward<Args>(args)...);
200 }
201
209 template <Print::Text Source, typename... Args>
210 static int Printf(Args&&... args)
211 {
212 constexpr auto format = Print::Printf::Build<Source>();
213 return RunCompiled(format, std::forward<Args>(args)...);
214 }
215};
216
217} // namespace LibXR
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
static consteval Compiled< Source > Build()
在编译期解析并校验 printf 格式串 / Parse and validate a printf format at compile time
Definition printf.hpp:101
ReadPort class for handling read operations.
Definition read_port.hpp:18
STDIO 编译格式会话使用的流式截断输出端 / Stream-backed truncating sink used by one STDIO compiled-format session.
Definition stdio.hpp:41
WritePort::Stream & stream_
Active stream session receiving retained bytes. 接收保留字节的活动流会话。
Definition stdio.hpp:63
ErrorCode Write(std::string_view chunk)
追加一个文本片段;必要时按会话剩余空间直接截断 / Append one text chunk and truncate directly to the remaining session capaci...
Definition stdio.cpp:11
bool saturated_
No more bytes should be retained in this session. 当前会话不再继续保留输出。
Definition stdio.hpp:65
size_t retained_size_
Bytes retained so far. 当前已保留的字节数。
Definition stdio.hpp:64
size_t RetainedSize() const
返回当前会话最终保留下来的字节数 / Return the retained byte count of the current session
Definition stdio.hpp:60
CompiledSink(WritePort::Stream &stream)
构造一个绑定到指定流的编译格式输出端 / Construct one compiled-format sink bound to the given stream
Definition stdio.cpp:9
提供静态全局的输入输出接口绑定与写会话管理 / STDIO interface for read and write ports
Definition stdio.hpp:20
static int Printf(Args &&... args)
将一个编译期 printf 字面量打印到当前 STDIO 输出 / Print one compile-time printf literal to the active STDIO output
Definition stdio.hpp:210
static int RunCompiledSession(Call &call)
执行一次模板已知的 STDIO 编译格式会话 / Run one STDIO compiled-format session whose format and argument types are al...
Definition stdio.hpp:143
static int Print(Args &&... args)
将一个编译期 brace 字面量打印到当前 STDIO 输出 / Print one compile-time brace literal to the active STDIO output
Definition stdio.hpp:196
static int RunCompiled(const CompiledFormat &format, Args &&... args)
用一份已编译格式和一组运行时参数执行一次完整的 STDIO 写会话 / Run one complete STDIO write session with one compiled format and...
Definition stdio.hpp:162
static LibXR::WritePort::Stream * write_stream_
Optional externally owned write stream. 可选的外部托管写流。
Definition stdio.hpp:29
static LibXR::Mutex * write_mutex_
Write port mutex. 写入端口互斥锁。
Definition stdio.hpp:27
static int WriteCompiledToStream(WritePort::Stream &stream, void *context, CompiledWriteFun write_fun)
在指定 Stream 上执行一次完整的 STDIO 编译格式写入与收尾 / Run one complete STDIO compiled-format write and finalize pass ...
Definition stdio.cpp:66
static ReadPort * read_
Read port instance. 读取端口。
Definition stdio.hpp:25
static int FinishWriteSession(WritePort::Stream &stream, size_t retained_size, ErrorCode format_result)
提交当前编译格式会话的写入流并释放共享会话 / Commit the current compiled-format session stream and release the shared sess...
Definition stdio.cpp:89
static int WriteCompiledSession(void *context, CompiledWriteFun write_fun)
执行一次完整的 STDIO 编译格式流会话选择、写入与收尾 / Run one complete STDIO compiled-format stream session: stream selecti...
Definition stdio.cpp:74
static WritePort * write_
Write port instance. 写入端口。
Definition stdio.hpp:26
static bool BeginWriteSession()
获取一个共享的 STDIO 写入会话 / Acquire one shared STDIO write session
Definition stdio.cpp:51
WritePort class for handling write operations.
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
Definition printf.hpp:16
一次编译格式 STDIO 调用的模板上下文 / Template context for one compiled-format STDIO call
Definition stdio.hpp:79
std::tuple< Args &&... > args
Forwarded runtime arguments. 转发保存的运行时参数。
Definition stdio.hpp:81
static ErrorCode Write(void *context, CompiledSink &sink)
将当前模板上下文桥接到编译格式前端写入入口 / Bridge the current template context into the compiled-format write entry
Definition stdio.hpp:89
const CompiledFormat & format
Compile-time compiled format object. 编译期已编译的格式对象。
Definition stdio.hpp:80