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
20class STDIO
21{
22 public:
23 // Shared global stdio binding state.
24 // 共享的全局 stdio 绑定状态。
25 // NOLINTBEGIN
26 static inline ReadPort* read_ = nullptr;
27 static inline WritePort* write_ = nullptr;
28 static inline LibXR::Mutex* write_mutex_ =
29 nullptr;
31 nullptr;
32 // NOLINTEND
33
34 private:
35 // Compiled-format bridge shared by the brace and printf frontends.
36 // brace 与 printf 两个前端共用的编译格式桥接层。
37
43 {
44 public:
50 explicit CompiledSink(WritePort::Stream& stream);
51
59 [[nodiscard]] ErrorCode Write(std::string_view chunk);
60
67 [[nodiscard]] size_t RetainedSize() const { return retained_size_; }
68
69 private:
72 size_t retained_size_ = 0;
73 bool saturated_ = false;
75 };
76
77 // Type-erased bridge for one compiled STDIO call.
78 // 一次编译格式 STDIO 调用的类型擦除桥接函数。
79 using CompiledWriteFun = ErrorCode (*)(void* context, CompiledSink& sink);
80
87 template <typename CompiledFormat, typename... Args>
89 {
90 const CompiledFormat&
92 std::tuple<Args&&...> args;
93
103 [[nodiscard]] static ErrorCode Write(void* context, CompiledSink& sink)
104 {
105 auto& compiled_call = *static_cast<CompiledCall*>(context);
106 return std::apply(
107 [&](auto&&... unpacked) -> ErrorCode
108 {
109 return Print::Write(sink, compiled_call.format,
110 std::forward<decltype(unpacked)>(unpacked)...);
111 },
112 compiled_call.args);
113 }
114 };
115
131 [[nodiscard]] static int WriteCompiledToStream(WritePort::Stream& stream, void* context,
132 CompiledWriteFun write_fun);
133
148 [[nodiscard]] static int WriteCompiledSession(void* context,
149 CompiledWriteFun write_fun);
150
164 template <typename Call>
165 [[nodiscard]] static int RunCompiledSession(Call& call)
166 {
167 if (!BeginWriteSession())
168 {
169 return -1;
170 }
171
172 return WriteCompiledSession(&call, &Call::Write);
173 }
174
186 template <typename CompiledFormat, typename... Args>
187 [[nodiscard]] static int RunCompiled(const CompiledFormat& format, Args&&... args)
188 {
189 CompiledCall<CompiledFormat, Args...> call{
190 format, std::forward_as_tuple(std::forward<Args>(args)...)};
191 return RunCompiledSession(call);
192 }
193
199 [[nodiscard]] static bool BeginWriteSession();
200
212 [[nodiscard]] static int FinishWriteSession(WritePort::Stream& stream,
213 size_t retained_size,
214 ErrorCode format_result);
215
216 public:
227 template <Print::Text Source, typename... Args>
228 static int Print(Args&&... args)
229 {
230 constexpr LibXR::Format<Source> format{};
231 return RunCompiled(format, std::forward<Args>(args)...);
232 }
233
244 template <Print::Text Source, typename... Args>
245 static int Printf(Args&&... args)
246 {
247 constexpr auto format = Print::Printf::Build<Source>();
248 return RunCompiled(format, std::forward<Args>(args)...);
249 }
250};
251
252} // 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:123
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:43
WritePort::Stream & stream_
Definition stdio.hpp:70
ErrorCode Write(std::string_view chunk)
追加一个文本片段;必要时按会话剩余空间直接截断 / Append one text chunk and truncate directly to the remaining session capaci...
Definition stdio.cpp:11
size_t retained_size_
Bytes retained so far. 当前已保留的字节数。
Definition stdio.hpp:72
size_t RetainedSize() const
返回当前会话最终保留下来的字节数 / Return the retained byte count of the current session
Definition stdio.hpp:67
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:21
static int Printf(Args &&... args)
将一个编译期 printf 字面量打印到当前 STDIO 输出 / Print one compile-time printf literal to the active STDIO output
Definition stdio.hpp:245
static int RunCompiledSession(Call &call)
执行一次模板已知的 STDIO 编译格式会话 / Run one STDIO compiled-format session whose format and argument types are al...
Definition stdio.hpp:165
static int Print(Args &&... args)
将一个编译期 brace 字面量打印到当前 STDIO 输出 / Print one compile-time brace literal to the active STDIO output
Definition stdio.hpp:228
static int RunCompiled(const CompiledFormat &format, Args &&... args)
用一份已编译格式和一组运行时参数执行一次完整的 STDIO 写会话 / Run one complete STDIO write session with one compiled format and...
Definition stdio.hpp:187
static LibXR::WritePort::Stream * write_stream_
Optional externally owned write stream. 可选的外部托管写流。
Definition stdio.hpp:30
static LibXR::Mutex * write_mutex_
Write port mutex. 写入端口互斥锁。
Definition stdio.hpp:28
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:26
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:27
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:17
一次编译格式 STDIO 调用的模板上下文 / Template context for one compiled-format STDIO call
Definition stdio.hpp:89
std::tuple< Args &&... > args
Forwarded runtime arguments. 转发保存的运行时参数。
Definition stdio.hpp:92
static ErrorCode Write(void *context, CompiledSink &sink)
将当前模板上下文桥接到编译格式前端写入入口 / Bridge the current template context into the compiled-format write entry
Definition stdio.hpp:103
const CompiledFormat & format
Compile-time compiled format object. 编译期已编译的格式对象。
Definition stdio.hpp:91