oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/ove.h>
17#include <cstdint>
18#include <cstddef>
19#include <chrono>
20#include <concepts>
21#include <type_traits>
22#include <utility>
23
40namespace ove
41{
42
43/* ------------------------------------------------------------------ */
44/* C++20 Concepts */
45/* ------------------------------------------------------------------ */
46
55template <typename F>
56concept ThreadEntry = std::convertible_to<F, void (*)(void *)>;
57
58/* ------------------------------------------------------------------ */
59/* Compile-time C-ABI shape check */
60/* ------------------------------------------------------------------ */
61/*
62 * Pin the OVE_ERR_* numeric values at compile time. Mirrors Rust's
63 * `_assert_codes_match()` (bindings/rust/ove/src/error.rs:127-150)
64 * and Zig's `comptime { std.debug.assert(...) }` block
65 * (bindings/zig/ove/src/error.zig:88-102). If a future C-header
66 * rename or renumber drifts an OVE_ERR_* value, every C++ TU that
67 * includes <ove/types.hpp> (i.e. every TU in apps/cpp and the C++
68 * binding) fails to build with a clear message — the silent-drift
69 * surface that bit Rust's mangled-name audit is closed at the C++
70 * boundary too.
71 */
72static_assert(OVE_ERR_NOT_REGISTERED == -1, "OVE_ERR_NOT_REGISTERED drifted");
73static_assert(OVE_ERR_INVALID_PARAM == -2, "OVE_ERR_INVALID_PARAM drifted");
74static_assert(OVE_ERR_NO_MEMORY == -3, "OVE_ERR_NO_MEMORY drifted");
75static_assert(OVE_ERR_TIMEOUT == -4, "OVE_ERR_TIMEOUT drifted");
76static_assert(OVE_ERR_NOT_SUPPORTED == -5, "OVE_ERR_NOT_SUPPORTED drifted");
77static_assert(OVE_ERR_QUEUE_FULL == -6, "OVE_ERR_QUEUE_FULL drifted");
78static_assert(OVE_ERR_ML_FAILED == -7, "OVE_ERR_ML_FAILED drifted");
79static_assert(OVE_ERR_NET_REFUSED == -8, "OVE_ERR_NET_REFUSED drifted");
80static_assert(OVE_ERR_NET_UNREACHABLE == -9, "OVE_ERR_NET_UNREACHABLE drifted");
81static_assert(OVE_ERR_NET_ADDR_IN_USE == -10, "OVE_ERR_NET_ADDR_IN_USE drifted");
82static_assert(OVE_ERR_NET_RESET == -11, "OVE_ERR_NET_RESET drifted");
83static_assert(OVE_ERR_NET_DNS_FAIL == -12, "OVE_ERR_NET_DNS_FAIL drifted");
84static_assert(OVE_ERR_NET_CLOSED == -13, "OVE_ERR_NET_CLOSED drifted");
85static_assert(OVE_ERR_BUS_NACK == -14, "OVE_ERR_BUS_NACK drifted");
86static_assert(OVE_ERR_BUS_BUSY == -15, "OVE_ERR_BUS_BUSY drifted");
87static_assert(OVE_ERR_BUS_ERROR == -16, "OVE_ERR_BUS_ERROR drifted");
88static_assert(OVE_ERR_QUEUE_EMPTY == -17, "OVE_ERR_QUEUE_EMPTY drifted");
89static_assert(OVE_ERR_WOULD_BLOCK == -18, "OVE_ERR_WOULD_BLOCK drifted");
90static_assert(OVE_ERR_EOF == -19, "OVE_ERR_EOF drifted");
91static_assert(OVE_ERR_INVAL == -20, "OVE_ERR_INVAL drifted");
92static_assert(OVE_ERR_NOT_FOUND == -21, "OVE_ERR_NOT_FOUND drifted");
93
94/* ------------------------------------------------------------------ */
95/* Duration / timeout types */
96/* ------------------------------------------------------------------ */
97
119inline constexpr std::chrono::nanoseconds wait_forever = std::chrono::nanoseconds::max();
120
128template <typename Rep, typename Period>
129inline constexpr uint64_t to_timeout_ns(std::chrono::duration<Rep, Period> d) noexcept
130{
131 const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(d);
132 if (ns.count() < 0)
133 return 0u;
134 if (ns == std::chrono::nanoseconds::max())
135 return OVE_WAIT_FOREVER;
136 return static_cast<uint64_t>(ns.count());
137}
138
156 using duration = std::chrono::nanoseconds;
157 using rep = duration::rep;
158 using period = duration::period;
159 using time_point = std::chrono::time_point<steady_clock>;
160 static constexpr bool is_steady = true;
161
162 static time_point now() noexcept
163 {
164 return time_point(duration(ove_time_now_steady_ns()));
165 }
166};
167
176inline constexpr uint64_t to_deadline_ns(steady_clock::time_point tp) noexcept
177{
178 const auto since_epoch = tp.time_since_epoch();
179 if (since_epoch == std::chrono::nanoseconds::max())
180 return OVE_WAIT_FOREVER;
181 if (since_epoch.count() < 0)
182 return 0u;
183 return static_cast<uint64_t>(since_epoch.count());
184}
185
186} // namespace ove
Concept satisfied by any callable convertible to void(*)(void*).
Definition types.hpp:56
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
constexpr uint64_t to_timeout_ns(std::chrono::duration< Rep, Period > d) noexcept
Convert a chrono duration to uint64_t nanoseconds for the C API.
Definition types.hpp:129
constexpr uint64_t to_deadline_ns(steady_clock::time_point tp) noexcept
Convert an ove::steady_clock::time_point to uint64_t nanoseconds for the substrate's _until APIs.
Definition types.hpp:176
constexpr std::chrono::nanoseconds wait_forever
Sentinel duration meaning "block indefinitely".
Definition types.hpp:119
Steady-clock wrapping the substrate's monotonic time source.
Definition types.hpp:155