ReXGlue SDK
Loading...
Searching...
No Matches
object_table.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <memory>
14#include <optional>
15#include <span>
16#include <string_view>
17#include <utility>
18#include <vector>
19
26#include <rex/module.h>
27#include <rex/platform.h>
28#include <rex/result.h>
29#include <rex/types.h>
30
31namespace rex::kernel {
32
33struct AddOptions {
34 std::string_view name;
35 ObjectBlock block;
36};
37
43 virtual ~IObjectTable() = default;
44
50 [[nodiscard]] virtual Result<X_HANDLE> Add(std::unique_ptr<KernelObject> object,
51 const AddOptions& options) = 0;
52
61 [[nodiscard]] virtual Result<X_HANDLE> AddWithBlock(std::unique_ptr<KernelObject> object,
62 u32 attributes_ptr, u32 block_size) = 0;
63
72 [[nodiscard]] virtual ObjectBlock AllocateObjectBlock(u32 size, ObjectType type,
73 ObjectBlockHeap heap) = 0;
74
82 [[nodiscard]] virtual Result<X_HANDLE> AddEvent(bool manual_reset, bool initial_state) = 0;
83
90 [[nodiscard]] virtual Result<X_HANDLE> AddXAMObject(std::unique_ptr<IXAMObject> body,
91 const AddOptions& options) = 0;
92
98 [[nodiscard]] virtual Result<X_HANDLE> AddFile(std::unique_ptr<filesystem::IFile> file,
99 bool synchronous) = 0;
100
107 [[nodiscard]] virtual Result<X_HANDLE> Duplicate(X_HANDLE handle) = 0;
108
115 [[nodiscard]] virtual Result<X_HANDLE> Open(const ObjectPin& object) = 0;
116
123 [[nodiscard]] virtual Result<void> Close(X_HANDLE handle) = 0;
124
131 [[nodiscard]] virtual Result<void> Reference(const ObjectPin& object) = 0;
132
139 [[nodiscard]] virtual Result<void> Dereference(const ObjectPin& object) = 0;
140
147 [[nodiscard]] virtual ObjectPin GetByAddress(u32 address) = 0;
148
155 [[nodiscard]] virtual ObjectPin Get(X_HANDLE handle) = 0;
156
158 [[nodiscard]] virtual std::vector<ObjectPin> GetAll() = 0;
159
166 [[nodiscard]] std::optional<std::vector<ObjectPin>> GetEach(std::span<const X_HANDLE> handles) {
167 std::vector<ObjectPin> pins;
168 pins.reserve(handles.size());
169 for (const X_HANDLE handle : handles) {
170 ObjectPin pin = Get(handle);
171 if (!pin) {
172 return std::nullopt;
173 }
174 pins.push_back(std::move(pin));
175 }
176 return pins;
177 }
178
185 [[nodiscard]] virtual Result<void> AddName(std::string_view name, X_HANDLE handle) = 0;
186
192 [[nodiscard]] virtual Result<X_HANDLE> OpenByName(std::string_view name) = 0;
193
204 [[nodiscard]] virtual ObjectPin ResolveObject(u32 address, i32 as_type) = 0;
205
212 virtual void ForgetAddress(u32 address) = 0;
213
219 template <typename T, typename... Args>
220 [[nodiscard]] Result<X_HANDLE> Emplace(const AddOptions& options, Args&&... args) {
221 if constexpr (XAMObjectClass<T>) {
222 return AddXAMObject(std::make_unique<T>(std::forward<Args>(args)...), options);
223 } else {
224 return Add(MakeObject<T>(std::forward<Args>(args)...), options);
225 }
226 }
227
235 template <typename T, typename... Args>
236 [[nodiscard]] Result<X_HANDLE> CreateObject(u32 attributes_ptr, u32 block_size, Args&&... args) {
237 return AddWithBlock(MakeObject<T>(std::forward<Args>(args)...), attributes_ptr, block_size);
238 }
239
246 template <typename T>
247 [[nodiscard]] Pinned<T> Get(X_HANDLE handle) {
248 return Pinned<T>(Get(handle));
249 }
250
252 template <typename T>
253 [[nodiscard]] std::vector<Pinned<T>> All() {
254 std::vector<Pinned<T>> results;
255 for (ObjectPin& slot : GetAll()) {
256 Pinned<T> pin(std::move(slot));
257 if (pin) {
258 results.push_back(std::move(pin));
259 }
260 }
261 return results;
262 }
263
271 template <typename T>
272 [[nodiscard]] Pinned<T> Resolve(u32 address, i32 as_type = -1) {
273 return Pinned<T>(ResolveObject(address, as_type));
274 }
275};
276
282inline void CloseOwned(IObjectTable& table, X_HANDLE handle) noexcept {
283 log::NoThrow(log::system, [&] {
284 if (auto closed = table.Close(handle); !closed) {
285 REXSYS_WARN("Handle {:08X} did not close, {}", handle, closed.error().message);
286 }
287 });
288}
289
290} // namespace rex::kernel
Definition object_table.h:42
Definition xam_object.h:20
Definition object_pin.h:29
Definition object_pin.h:122
Definition xam_object.h:40
Interfaces, factory and module declaration.
#define REX_DECLARE_INTERFACE(Name, version)
Definition module.h:114
Definition export.h:24
void CloseOwned(IObjectTable &table, X_HANDLE handle) noexcept
Definition object_table.h:282
std::unique_ptr< KernelObject > MakeObject(Args &&... args)
Definition kernel_object.h:81
void NoThrow(const LogCategory &category, Fn &&fn) noexcept
Definition log_category.h:217
std::expected< T, Error > Result
Definition result.h:98
Platform flags, packing macros and export markers.
Definition object_table.h:33
std::string_view name
Definition object_table.h:34
Definition kernel_object.h:44
Definition object_block.h:21
Integer types, byte order wrappers and size literals.