RADPx-OS Kernel (Radical Posix OS)  Crimson 0.1.4
POSIX-inspired kernel API with embedded and desktop VM targets
rad_cpp.h
Go to the documentation of this file.
1 
6 #ifndef RADEMBEDDEDKERNEL_RAD_CPP_H
7 #define RADEMBEDDEDKERNEL_RAD_CPP_H
8 
9 #include <radkernel/radkernel.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 namespace rad {
14 
16 enum class Status : int32_t {
17  ok = RAD_STATUS_OK,
26 };
27 
29 inline constexpr bool succeeded(rad_status_t status) { return status == RAD_STATUS_OK; }
31 inline constexpr bool failed(rad_status_t status) { return status != RAD_STATUS_OK; }
32 
34 template <typename T>
35 class Span {
36 public:
38  constexpr Span() = default;
40  constexpr Span(T *data, size_t size) : data_(data), size_(size) {}
42  constexpr T *data() const { return data_; }
44  constexpr size_t size() const { return size_; }
46  constexpr bool empty() const { return size_ == 0; }
48  constexpr T& operator[](size_t index) const { return data_[index]; }
50  constexpr T *begin() const { return data_; }
52  constexpr T *end() const { return data_ + size_; }
53 private:
54  T *data_ = nullptr;
55  size_t size_ = 0;
56 };
57 
59 template <typename Handle, void (*CloseFn)(Handle)>
60 class UniqueHandle {
61 public:
63  constexpr UniqueHandle() = default;
65  explicit constexpr UniqueHandle(Handle handle) : handle_(handle) {}
68  UniqueHandle(const UniqueHandle&) = delete;
69  UniqueHandle& operator=(const UniqueHandle&) = delete;
71  UniqueHandle(UniqueHandle&& other) noexcept : handle_(other.release()) {}
73  UniqueHandle& operator=(UniqueHandle&& other) noexcept {
74  if (this != &other) reset(other.release());
75  return *this;
76  }
78  constexpr Handle get() const { return handle_; }
80  constexpr explicit operator bool() const { return handle_ != nullptr; }
82  Handle release() {
83  Handle handle = handle_;
84  handle_ = nullptr;
85  return handle;
86  }
88  void reset(Handle handle = nullptr) {
89  if (handle_) CloseFn(handle_);
90  handle_ = handle;
91  }
92 private:
93  Handle handle_ = nullptr;
94 };
95 
104 
106 class Mutex {
107 public:
109  Mutex() { status_ = rad_mutex_create(&handle_); }
111  ~Mutex() { if (handle_) rad_mutex_destroy(handle_); }
112  Mutex(const Mutex&) = delete;
113  Mutex& operator=(const Mutex&) = delete;
115  rad_status_t status() const { return status_; }
117  rad_status_t lock() { return handle_ ? rad_mutex_lock(handle_) : RAD_STATUS_NOT_INITIALIZED; }
121  rad_mutex_t native() const { return handle_; }
122 private:
123  rad_mutex_t handle_ = nullptr;
125 };
126 
128 class Event {
129 public:
131  explicit Event(bool signaled = false) { status_ = rad_event_create(&handle_, signaled ? 1 : 0); }
133  ~Event() { if (handle_) rad_event_destroy(handle_); }
134  Event(const Event&) = delete;
135  Event& operator=(const Event&) = delete;
137  rad_status_t status() const { return status_; }
139  rad_status_t wait(uint32_t timeout_ms) { return handle_ ? rad_event_wait(handle_, timeout_ms) : RAD_STATUS_NOT_INITIALIZED; }
144 private:
145  rad_event_t handle_ = nullptr;
147 };
148 
150 class Module {
151 public:
153  explicit Module(const rad_module_descriptor_t& descriptor) : name_(descriptor.name) {
154  status_ = rad_module_register(&descriptor);
155  }
157  ~Module() { if (name_ && status_ == RAD_STATUS_OK) rad_module_unregister(name_); }
158  Module(const Module&) = delete;
159  Module& operator=(const Module&) = delete;
161  rad_status_t status() const { return status_; }
162 private:
163  const char *name_ = nullptr;
165 };
166 
167 } // namespace rad
168 
169 #endif
RAII wrapper for rad_event_t.
Definition: rad_cpp.h:128
Event(bool signaled=false)
Create a kernel event with an optional initial signal state.
Definition: rad_cpp.h:131
rad_status_t status() const
Return construction status.
Definition: rad_cpp.h:137
rad_status_t reset()
Reset the event to unsignaled.
Definition: rad_cpp.h:143
~Event()
Destroy the event when it was created successfully.
Definition: rad_cpp.h:133
rad_status_t signal()
Signal the event.
Definition: rad_cpp.h:141
rad_status_t wait(uint32_t timeout_ms)
Wait for the event or timeout.
Definition: rad_cpp.h:139
RAII registration wrapper for rad_module_descriptor_t.
Definition: rad_cpp.h:150
Module(const rad_module_descriptor_t &descriptor)
Register a module descriptor and unregister it on destruction.
Definition: rad_cpp.h:153
rad_status_t status() const
Return registration status.
Definition: rad_cpp.h:161
~Module()
Unregister the module when registration succeeded.
Definition: rad_cpp.h:157
RAII wrapper for rad_mutex_t.
Definition: rad_cpp.h:106
~Mutex()
Destroy the kernel mutex when it was created successfully.
Definition: rad_cpp.h:111
Mutex()
Create a kernel mutex.
Definition: rad_cpp.h:109
rad_mutex_t native() const
Return the raw mutex handle.
Definition: rad_cpp.h:121
rad_status_t lock()
Lock the mutex.
Definition: rad_cpp.h:117
rad_status_t status() const
Return construction status.
Definition: rad_cpp.h:115
rad_status_t unlock()
Unlock the mutex.
Definition: rad_cpp.h:119
Non-owning contiguous span used by lightweight C++ integrations.
Definition: rad_cpp.h:35
constexpr Span(T *data, size_t size)
Create a span over data and size elements.
Definition: rad_cpp.h:40
constexpr size_t size() const
Return the number of elements in the span.
Definition: rad_cpp.h:44
constexpr Span()=default
Create an empty span.
constexpr bool empty() const
Return true when the span has no elements.
Definition: rad_cpp.h:46
constexpr T & operator[](size_t index) const
Return the element at index without bounds checks.
Definition: rad_cpp.h:48
constexpr T * data() const
Return the first element pointer, or nullptr for an empty span.
Definition: rad_cpp.h:42
constexpr T * end() const
Return an iterator pointer one past the final element.
Definition: rad_cpp.h:52
constexpr T * begin() const
Return an iterator pointer to the first element.
Definition: rad_cpp.h:50
Move-only RAII owner for nullable RADPx-OS handles.
Definition: rad_cpp.h:60
void reset(Handle handle=nullptr)
Close any current handle and optionally take a replacement.
Definition: rad_cpp.h:88
constexpr Handle get() const
Return the raw handle without releasing ownership.
Definition: rad_cpp.h:78
Handle release()
Release ownership and return the raw handle.
Definition: rad_cpp.h:82
constexpr UniqueHandle()=default
Create an empty owner.
UniqueHandle(UniqueHandle &&other) noexcept
Move ownership from another handle owner.
Definition: rad_cpp.h:71
UniqueHandle & operator=(UniqueHandle &&other) noexcept
Replace this owner with another owner's handle.
Definition: rad_cpp.h:73
~UniqueHandle()
Close the owned handle if present.
Definition: rad_cpp.h:67
constexpr UniqueHandle(Handle handle)
Take ownership of handle.
Definition: rad_cpp.h:65
constexpr bool failed(rad_status_t status)
Return true when a C API status is not RAD_STATUS_OK.
Definition: rad_cpp.h:31
constexpr bool succeeded(rad_status_t status)
Return true when a C API status is RAD_STATUS_OK.
Definition: rad_cpp.h:29
Status
C++ status enum mirroring rad_status_t values.
Definition: rad_cpp.h:16
@ not_initialized
Kernel or handle is not initialized.
@ ok
Operation completed successfully.
@ not_found
Requested object was not found.
@ already_exists
Object already exists.
@ timeout
Operation timed out.
@ not_supported
Backend does not support the operation.
@ no_memory
Allocation failed.
@ error
Generic failure.
@ invalid_argument
Caller supplied an invalid argument.
Portable RADPx-OS kernel service ABI for RADLib and RADPx-OS targets.
rad_status_t rad_module_unregister(const char *name)
Public RADPx-OS kernel API entry point.
rad_status_t rad_mutex_unlock(rad_mutex_t mutex)
Public RADPx-OS kernel API entry point.
struct rad_mutex_handle * rad_mutex_t
Opaque handle type rad_mutex_t backed by rad_mutex_handle.
Definition: radkernel.h:1349
rad_status_t rad_event_create(rad_event_t *event, int initially_signaled)
Public RADPx-OS kernel API entry point.
rad_status_t rad_mutex_create(rad_mutex_t *mutex)
Public RADPx-OS kernel API entry point.
rad_status_t rad_module_register(const rad_module_descriptor_t *descriptor)
Public RADPx-OS kernel API entry point.
rad_status_t rad_mutex_lock(rad_mutex_t mutex)
Public RADPx-OS kernel API entry point.
rad_status_t rad_event_wait(rad_event_t event, uint32_t timeout_ms)
Public RADPx-OS kernel API entry point.
@ RAD_STATUS_NOT_INITIALIZED
RAD_STATUS_NOT_INITIALIZED.
Definition: radkernel.h:151
@ RAD_STATUS_ERROR
RAD_STATUS_ERROR.
Definition: radkernel.h:144
@ RAD_STATUS_NO_MEMORY
RAD_STATUS_NO_MEMORY.
Definition: radkernel.h:147
@ RAD_STATUS_NOT_SUPPORTED
RAD_STATUS_NOT_SUPPORTED.
Definition: radkernel.h:149
@ RAD_STATUS_NOT_FOUND
RAD_STATUS_NOT_FOUND.
Definition: radkernel.h:146
@ RAD_STATUS_ALREADY_EXISTS
RAD_STATUS_ALREADY_EXISTS.
Definition: radkernel.h:150
@ RAD_STATUS_INVALID_ARGUMENT
RAD_STATUS_INVALID_ARGUMENT.
Definition: radkernel.h:145
@ RAD_STATUS_TIMEOUT
RAD_STATUS_TIMEOUT.
Definition: radkernel.h:148
@ RAD_STATUS_OK
RAD_STATUS_OK.
Definition: radkernel.h:143
rad_status_t rad_event_signal(rad_event_t event)
Public RADPx-OS kernel API entry point.
rad_status_t rad_event_reset(rad_event_t event)
Public RADPx-OS kernel API entry point.
void rad_event_destroy(rad_event_t event)
Public RADPx-OS kernel API entry point.
struct rad_event_handle * rad_event_t
Opaque handle type rad_event_t backed by rad_event_handle.
Definition: radkernel.h:1351
void rad_mutex_destroy(rad_mutex_t mutex)
Public RADPx-OS kernel API entry point.
enum rad_status rad_status_t
Public enumeration for rad_status.
Public data structure for rad_module_descriptor.
Definition: radkernel.h:1143