ReXGlue SDK
Loading...
Searching...
No Matches
object_pin.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <utility>
14#include <variant>
15
23#include <rex/types.h>
24#include <rex/utility.h>
25
26namespace rex::kernel {
27
29class ObjectPin : public rex::MoveOnly {
30 public:
31 ObjectPin() = default;
37 ObjectPin(KernelObject* object, X_HANDLE handle) noexcept : object_(object), handle_(handle) {}
38
39 ObjectPin(ObjectPin&& other) noexcept : object_(other.object_), handle_(other.handle_) {
40 other.object_ = nullptr;
41 }
42 ObjectPin& operator=(ObjectPin&& other) noexcept {
43 if (this != &other) {
44 Reset();
45 object_ = other.object_;
46 handle_ = other.handle_;
47 other.object_ = nullptr;
48 }
49 return *this;
50 }
51 ~ObjectPin() { Reset(); }
52
53 explicit operator bool() const noexcept { return object_ != nullptr; }
54
55 [[nodiscard]] ObjectType GetType() const { return object_->GetType(); }
56
57 [[nodiscard]] ObjectData& GetData() const { return object_->data; }
58
59 [[nodiscard]] KernelObject& GetKernelObject() const { return *object_; }
60
66 [[nodiscard]] X_HANDLE GetHandle() const noexcept { return handle_; }
67
69 [[nodiscard]] u32 GetGuestAddress() const { return object_->guest_ptr; }
70
71 void Reset() noexcept {
72 if (object_ != nullptr) {
73 if (object_->pins.Unpin()) {
74 object_->destroy(object_);
75 }
76 object_ = nullptr;
77 }
78 }
79
80 private:
81 KernelObject* object_ = nullptr;
82 X_HANDLE handle_ = 0;
83};
84
90template <typename T>
92 using Type = T;
93};
94
95template <>
97 using Type = EventObject;
98};
99
100template <>
102 using Type = FileObject;
103};
104
105template <>
109
110template <>
112 using Type = ModuleObject;
113};
114
121template <typename T>
122class Pinned {
123 public:
124 Pinned() = default;
130 explicit Pinned(ObjectPin pin) : slot_(std::move(pin)) {
131 if (slot_) {
132 if constexpr (XAMObjectClass<T>) {
133 auto* xam = std::get_if<XAMObject>(&slot_.GetData());
134 if (xam != nullptr && xam->body && xam->body->GetTag() == T::kTag) {
135 value_ = static_cast<T*>(xam->body.get());
136 }
137 } else {
138 value_ = std::get_if<typename ObjectViewOf<T>::Type>(&slot_.GetData());
139 }
140 if (value_ == nullptr) {
141 slot_.Reset();
142 }
143 }
144 }
145
146 explicit operator bool() const noexcept { return value_ != nullptr; }
147
148 T* operator->() const noexcept { return value_; }
149
150 T& operator*() const noexcept { return *value_; }
151
152 [[nodiscard]] X_HANDLE GetHandle() const noexcept { return slot_.GetHandle(); }
153
154 [[nodiscard]] u32 GetGuestAddress() const { return slot_.GetGuestAddress(); }
155
156 [[nodiscard]] const ObjectPin& GetSlot() const noexcept { return slot_; }
157
158 private:
159 ObjectPin slot_;
160 T* value_ = nullptr;
161};
162
163} // namespace rex::kernel
Definition utility.h:28
Definition event_object.h:44
Definition module_object.h:129
Definition notify_listener_object.h:43
Definition file_object.h:104
Definition object_pin.h:29
ObjectPin(KernelObject *object, X_HANDLE handle) noexcept
Definition object_pin.h:37
X_HANDLE GetHandle() const noexcept
Definition object_pin.h:66
u32 GetGuestAddress() const
Definition object_pin.h:69
bool Unpin() noexcept
Definition object.h:159
Pinned(ObjectPin pin)
Definition object_pin.h:130
Definition xam_object.h:40
Definition export.h:24
std::variant< EventObject, SemaphoreObject, MutantObject, TimerObject, FileObject, NotifyListenerObject, IOCompletionObject, SymbolicLinkObject, ModuleObject, ThreadRef, XAMObject > ObjectData
Definition kernel_object.h:39
Definition event_object.h:59
Definition file_object.h:161
Definition kernel_object.h:44
void(*) destroy(KernelObject *object) noexcept
Definition kernel_object.h:72
PinCount pins
Definition kernel_object.h:70
Definition module_object.h:189
Definition notify_listener_object.h:68
Definition object_pin.h:91
T Type
Definition object_pin.h:92
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.