|
| 1 | +// Native (zero-serialization, same-process) C++ example for the timer library. |
| 2 | +// |
| 3 | +// This is the in-process path: the program links libmy_timer and calls the |
| 4 | +// native `<name>` entry points, passing `{.ffi.}` types as plain C structs by |
| 5 | +// value and receiving struct returns as a typed `const <Type>*` on the |
| 6 | +// callback. No CBOR — the CBOR ABI (see ../cpp_bindings) is for crossing a |
| 7 | +// process/machine boundary, where serialization is unavoidable. |
| 8 | +// |
| 9 | +// Each call is dispatched on the library's FFI thread; an idiomatic RAII |
| 10 | +// wrapper blocks on a std::future until the result callback fires. |
| 11 | +#include "my_timer.h" // native C ABI (../c_bindings) |
| 12 | + |
| 13 | +#include <cstdint> |
| 14 | +#include <future> |
| 15 | +#include <iostream> |
| 16 | +#include <stdexcept> |
| 17 | +#include <string> |
| 18 | + |
| 19 | +namespace mytimer { |
| 20 | + |
| 21 | +struct EchoResult { |
| 22 | + std::string echoed; |
| 23 | + std::string timerName; |
| 24 | +}; |
| 25 | + |
| 26 | +namespace detail { |
| 27 | +// One-shot capture shared with a C callback via `userData`. |
| 28 | +struct Capture { |
| 29 | + int ret = RET_ERR; |
| 30 | + std::string text; // string return / error text |
| 31 | + EchoResult echo; // typed EchoResponse return |
| 32 | + std::promise<void> done; |
| 33 | +}; |
| 34 | + |
| 35 | +inline std::string rawText(const char *msg, std::size_t len) { |
| 36 | + return (msg && len) ? std::string(msg, len) : std::string(); |
| 37 | +} |
| 38 | + |
| 39 | +// `{.ffi.}`-callback shaped free functions (non-capturing => usable as C fn ptrs) |
| 40 | +extern "C" inline void ackCb(int ret, const char *msg, std::size_t len, void *ud) { |
| 41 | + auto *c = static_cast<Capture *>(ud); |
| 42 | + c->ret = ret; |
| 43 | + if (ret == RET_ERR) c->text = rawText(msg, len); |
| 44 | + c->done.set_value(); |
| 45 | +} |
| 46 | +extern "C" inline void stringCb(int ret, const char *msg, std::size_t len, void *ud) { |
| 47 | + auto *c = static_cast<Capture *>(ud); |
| 48 | + c->ret = ret; |
| 49 | + c->text = rawText(msg, len); |
| 50 | + c->done.set_value(); |
| 51 | +} |
| 52 | +extern "C" inline void echoCb(int ret, const char *msg, std::size_t len, void *ud) { |
| 53 | + auto *c = static_cast<Capture *>(ud); |
| 54 | + c->ret = ret; |
| 55 | + if (ret == RET_OK) { |
| 56 | + const auto *r = reinterpret_cast<const EchoResponse *>(msg); // typed return |
| 57 | + c->echo.echoed = r->echoed ? r->echoed : ""; |
| 58 | + c->echo.timerName = r->timerName ? r->timerName : ""; |
| 59 | + } else { |
| 60 | + c->text = rawText(msg, len); |
| 61 | + } |
| 62 | + c->done.set_value(); |
| 63 | +} |
| 64 | +} // namespace detail |
| 65 | + |
| 66 | +class TimerNode { |
| 67 | +public: |
| 68 | + explicit TimerNode(const std::string &name) { |
| 69 | + detail::Capture cap; |
| 70 | + auto fut = cap.done.get_future(); |
| 71 | + TimerConfig cfg{}; |
| 72 | + cfg.name = name.c_str(); |
| 73 | + ctx_ = my_timer_create(cfg, detail::ackCb, &cap); |
| 74 | + if (!ctx_) throw std::runtime_error("my_timer_create returned null"); |
| 75 | + fut.wait(); |
| 76 | + if (cap.ret != RET_OK) throw std::runtime_error("create failed: " + cap.text); |
| 77 | + } |
| 78 | + |
| 79 | + std::string version() { |
| 80 | + detail::Capture cap; |
| 81 | + auto fut = cap.done.get_future(); |
| 82 | + if (my_timer_version(ctx_, detail::stringCb, &cap) != RET_OK) |
| 83 | + throw std::runtime_error("version dispatch failed"); |
| 84 | + fut.wait(); |
| 85 | + if (cap.ret != RET_OK) throw std::runtime_error(cap.text); |
| 86 | + return cap.text; |
| 87 | + } |
| 88 | + |
| 89 | + EchoResult echo(const std::string &message, std::int64_t delayMs = 0) { |
| 90 | + detail::Capture cap; |
| 91 | + auto fut = cap.done.get_future(); |
| 92 | + EchoRequest req{}; |
| 93 | + req.message = message.c_str(); |
| 94 | + req.delayMs = delayMs; |
| 95 | + if (my_timer_echo(ctx_, detail::echoCb, &cap, req) != RET_OK) |
| 96 | + throw std::runtime_error("echo dispatch failed"); |
| 97 | + fut.wait(); |
| 98 | + if (cap.ret != RET_OK) throw std::runtime_error(cap.text); |
| 99 | + return cap.echo; |
| 100 | + } |
| 101 | + |
| 102 | + ~TimerNode() { |
| 103 | + if (ctx_) my_timer_destroy(ctx_); |
| 104 | + } |
| 105 | + |
| 106 | + TimerNode(const TimerNode &) = delete; |
| 107 | + TimerNode &operator=(const TimerNode &) = delete; |
| 108 | + |
| 109 | +private: |
| 110 | + void *ctx_ = nullptr; |
| 111 | +}; |
| 112 | + |
| 113 | +} // namespace mytimer |
| 114 | + |
| 115 | +int main() { |
| 116 | + try { |
| 117 | + mytimer::TimerNode node("cpp-native-demo"); |
| 118 | + std::cout << "version: " << node.version() << "\n"; |
| 119 | + |
| 120 | + auto r = node.echo("hello from C++", /*delayMs=*/5); |
| 121 | + std::cout << "echo: echoed=" << r.echoed << " timerName=" << r.timerName << "\n"; |
| 122 | + |
| 123 | + std::cout << "done.\n"; |
| 124 | + return 0; |
| 125 | + } catch (const std::exception &e) { |
| 126 | + std::cerr << "error: " << e.what() << "\n"; |
| 127 | + return 1; |
| 128 | + } |
| 129 | +} |
0 commit comments