基础(v2)
dart_cpp_bridge 是一个 Dart ↔ C++20 桥接库,灵感来自
Flutter Rust Bridge。业务代码只需
写普通 C++ 函数或 stdexec::task<T> / sender 流水线,桥接层负责编解码、
调度、生命周期和 Dart API 生成。
- 同步 —
BRIDGE_SYNC直接返回值; - 异步 —
BRIDGE_ASYNC使用stdexec::task<T>或受支持 sender, Dart 侧得到Future<T>; - Normal —
BRIDGE_NORMAL把阻塞工作放到线程池; - Stream —
StreamSink<T>生成 DartStream<T>; - DartFn — C++ 可以在等待 Dart 闭包回复时挂起;
- Channel —
co::oneshot和co::mpsc连接 worker 与协程; - Opaque / data class — 生成 handle 和值类型编解码;
- 跨运行时调度 — 外部事件循环提供普通 stdexec scheduler。
| 层 | 技术 |
|---|---|
| C++ | C++20 |
| 异步模型 | stdexec sender / stdexec::task |
| 内置事件循环 | Asio io_context,默认一个 io runner |
| 阻塞工作 | Asio thread pool |
| 取消 | stop token |
| Codegen | Python 3.13 + libclang-ng,固定版本并校验 hash |
| 构建 | CMake 3.25+ 和 Native Assets hook |
- 版本文档 — 选择 v1 或 v2;
- 快速开始 — 创建项目;
- 架构 — 调用流程;
- 标记选择 — 选择 sync、async、normal 或 Stream;
- v2 stdexec 异步 C++ — sender 和 task 规则;
- Channel 通道 — oneshot、mpsc、背压和取消;
- 线程与阻塞任务 — 线程池配置和自定义 scheduler;
- 内置运行时 — Runtime 和 scheduler;
- 生命周期管理 — session 和 finalizer;
- Native Assets 构建 hook — 原生构建接入。