libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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 // 共享的全局 stdio 绑定状态 / Shared global stdio binding state.
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 // brace 与 printf 两个前端共用的编译格式桥接层。
35 // Compiled-format bridge shared by the brace and printf frontends.
36
41 class CompiledSink
42 {
43 public:
49 explicit CompiledSink(WritePort::Stream& stream);
50
58 [[nodiscard]] ErrorCode Write(std::string_view chunk);
59
66 [[nodiscard]] size_t RetainedSize() const { return retained_size_; }
67
68 private:
69 WritePort::Stream& stream_;
71 size_t retained_size_ = 0;
72 bool saturated_ = false;
74 };
75
76 // 一次编译格式 STDIO 调用的类型擦除桥接函数。
77 // Type-erased bridge for one compiled STDIO call.
78 using CompiledWriteFun = ErrorCode (*)(void* context, CompiledSink& sink);
79
86 template <typename CompiledFormat, typename... Args>
87 struct CompiledCall
88 {
89 const CompiledFormat&
90 format;
91 std::tuple<Args&&...> args;
92
102 [[nodiscard]] static ErrorCode Write(void* context, CompiledSink& sink)
103 {
104 auto& compiled_call = *static_cast<CompiledCall*>(context);
105 return std::apply(
106 [&](auto&&... unpacked) -> ErrorCode
107 {
108 return Print::Write(sink, compiled_call.format,
109 std::forward<decltype(unpacked)>(unpacked)...);
110 },
111 compiled_call.args);
112 }
113 };
114
130 [[nodiscard]] static int WriteCompiledToStream(WritePort::Stream& stream, void* context,
131 CompiledWriteFun write_fun);
132
147 [[nodiscard]] static int WriteCompiledSession(void* context,
148 CompiledWriteFun write_fun);
149
163 template <typename Call>
164 [[nodiscard]] static int RunCompiledSession(Call& call)
165 {
166 if (!BeginWriteSession())
167 {
168 return -1;
169 }
170
171 return WriteCompiledSession(&call, &Call::Write);
172 }
173
185 template <typename CompiledFormat, typename... Args>
186 [[nodiscard]] static int RunCompiled(const CompiledFormat& format, Args&&... args)
187 {
188 CompiledCall<CompiledFormat, Args...> call{
189 format, std::forward_as_tuple(std::forward<Args>(args)...)};
190 return RunCompiledSession(call);
191 }
192
198 [[nodiscard]] static bool BeginWriteSession();
199
211 [[nodiscard]] static int FinishWriteSession(WritePort::Stream& stream,
212 size_t retained_size,
213 ErrorCode format_result);
214
215 public:
226 template <Print::Text Source, typename... Args>
227 static int Print(Args&&... args)
228 {
229 constexpr LibXR::Format<Source> format{};
230 return RunCompiled(format, std::forward<Args>(args)...);
231 }
232
243 template <Print::Text Source, typename... Args>
244 static int Printf(Args&&... args)
245 {
246 constexpr auto format = Print::Printf::Build<Source>();
247 return RunCompiled(format, std::forward<Args>(args)...);
248 }
249};
250
251} // 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:124
基于 SPSC 字节队列的读端口 / Read endpoint backed by an SPSC byte queue.
Definition read_port.hpp:30
提供静态全局的输入输出接口绑定与写会话管理 / 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:244
static int Print(Args &&... args)
将一个编译期 brace 字面量打印到当前 STDIO 输出 / Print one compile-time brace literal to the active STDIO output
Definition stdio.hpp:227
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 ReadPort * read_
读取端口 / Read port instance.
Definition stdio.hpp:25
static WritePort * write_
写入端口 / Write port instance.
Definition stdio.hpp:26
合并多次写入的流接口 / Stream interface for batched writes.
基于 SPSC 字节队列的写端口 / Write endpoint backed by an SPSC byte queue.
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
作为 printf 格式源的结构化字符串字面量包装 / Structural literal wrapper used as the NTTP source for printf formats
Definition printf.hpp:17