|
| 1 | +// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project |
| 2 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <memory> |
| 7 | +#include <span> |
| 8 | + |
| 9 | +#include "yuzu_common/common_types.h" |
| 10 | + |
| 11 | +namespace Service { |
| 12 | + |
| 13 | +// clang-format off |
| 14 | +template <typename T> |
| 15 | +struct AutoOut { |
| 16 | + T raw; |
| 17 | +}; |
| 18 | + |
| 19 | +template <typename T> |
| 20 | +class Out { |
| 21 | +public: |
| 22 | + using Type = T; |
| 23 | + |
| 24 | + /* implicit */ Out(const Out& t) : raw(t.raw) {} |
| 25 | + /* implicit */ Out(AutoOut<Type>& t) : raw(&t.raw) {} |
| 26 | + /* implicit */ Out(Type* t) : raw(t) {} |
| 27 | + Out& operator=(const Out&) = delete; |
| 28 | + |
| 29 | + Type* Get() const { |
| 30 | + return raw; |
| 31 | + } |
| 32 | + |
| 33 | + Type& operator*() const { |
| 34 | + return *raw; |
| 35 | + } |
| 36 | + |
| 37 | + Type* operator->() const { |
| 38 | + return raw; |
| 39 | + } |
| 40 | + |
| 41 | + operator Type*() const { |
| 42 | + return raw; |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + Type* raw; |
| 47 | +}; |
| 48 | + |
| 49 | +template <typename T> |
| 50 | +using SharedPointer = std::shared_ptr<T>; |
| 51 | + |
| 52 | +template <typename T> |
| 53 | +using OutInterface = Out<SharedPointer<T>>; |
| 54 | + |
| 55 | +struct ClientProcessId { |
| 56 | + explicit operator bool() const { |
| 57 | + return pid != 0; |
| 58 | + } |
| 59 | + |
| 60 | + const u64& operator*() const { |
| 61 | + return pid; |
| 62 | + } |
| 63 | + |
| 64 | + u64 pid; |
| 65 | +}; |
| 66 | + |
| 67 | +struct ProcessId { |
| 68 | + explicit ProcessId() : pid() {} |
| 69 | + explicit ProcessId(u64 p) : pid(p) {} |
| 70 | + /* implicit */ ProcessId(const ClientProcessId& c) : pid(c.pid) {} |
| 71 | + |
| 72 | + bool operator==(const ProcessId& rhs) const { |
| 73 | + return pid == rhs.pid; |
| 74 | + } |
| 75 | + |
| 76 | + explicit operator bool() const { |
| 77 | + return pid != 0; |
| 78 | + } |
| 79 | + |
| 80 | + const u64& operator*() const { |
| 81 | + return pid; |
| 82 | + } |
| 83 | + |
| 84 | + u64 pid; |
| 85 | +}; |
| 86 | + |
| 87 | +using ClientAppletResourceUserId = ClientProcessId; |
| 88 | +using AppletResourceUserId = ProcessId; |
| 89 | + |
| 90 | +template <typename T> |
| 91 | +class InCopyHandle { |
| 92 | +public: |
| 93 | + using Type = T; |
| 94 | + |
| 95 | + /* implicit */ InCopyHandle(Type* t) : raw(t) {} |
| 96 | + /* implicit */ InCopyHandle() : raw() {} |
| 97 | + ~InCopyHandle() = default; |
| 98 | + |
| 99 | + InCopyHandle& operator=(Type* rhs) { |
| 100 | + raw = rhs; |
| 101 | + return *this; |
| 102 | + } |
| 103 | + |
| 104 | + Type* Get() const { |
| 105 | + return raw; |
| 106 | + } |
| 107 | + |
| 108 | + Type& operator*() const { |
| 109 | + return *raw; |
| 110 | + } |
| 111 | + |
| 112 | + Type* operator->() const { |
| 113 | + return raw; |
| 114 | + } |
| 115 | + |
| 116 | + explicit operator bool() const { |
| 117 | + return raw != nullptr; |
| 118 | + } |
| 119 | + |
| 120 | +private: |
| 121 | + Type* raw; |
| 122 | +}; |
| 123 | + |
| 124 | +template <typename T> |
| 125 | +class OutCopyHandle { |
| 126 | +public: |
| 127 | + using Type = T*; |
| 128 | + |
| 129 | + /* implicit */ OutCopyHandle(const OutCopyHandle& t) : raw(t.raw) {} |
| 130 | + /* implicit */ OutCopyHandle(AutoOut<Type>& t) : raw(&t.raw) {} |
| 131 | + /* implicit */ OutCopyHandle(Type* t) : raw(t) {} |
| 132 | + OutCopyHandle& operator=(const OutCopyHandle&) = delete; |
| 133 | + |
| 134 | + Type* Get() const { |
| 135 | + return raw; |
| 136 | + } |
| 137 | + |
| 138 | + Type& operator*() const { |
| 139 | + return *raw; |
| 140 | + } |
| 141 | + |
| 142 | + Type* operator->() const { |
| 143 | + return raw; |
| 144 | + } |
| 145 | + |
| 146 | + operator Type*() const { |
| 147 | + return raw; |
| 148 | + } |
| 149 | + |
| 150 | +private: |
| 151 | + Type* raw; |
| 152 | +}; |
| 153 | + |
| 154 | +template <typename T> |
| 155 | +class OutMoveHandle { |
| 156 | +public: |
| 157 | + using Type = T*; |
| 158 | + |
| 159 | + /* implicit */ OutMoveHandle(const OutMoveHandle& t) : raw(t.raw) {} |
| 160 | + /* implicit */ OutMoveHandle(AutoOut<Type>& t) : raw(&t.raw) {} |
| 161 | + /* implicit */ OutMoveHandle(Type* t) : raw(t) {} |
| 162 | + OutMoveHandle& operator=(const OutMoveHandle&) = delete; |
| 163 | + |
| 164 | + Type* Get() const { |
| 165 | + return raw; |
| 166 | + } |
| 167 | + |
| 168 | + Type& operator*() const { |
| 169 | + return *raw; |
| 170 | + } |
| 171 | + |
| 172 | + Type* operator->() const { |
| 173 | + return raw; |
| 174 | + } |
| 175 | + |
| 176 | + operator Type*() const { |
| 177 | + return raw; |
| 178 | + } |
| 179 | + |
| 180 | +private: |
| 181 | + Type* raw; |
| 182 | +}; |
| 183 | + |
| 184 | +enum BufferAttr : int { |
| 185 | + /* 0x01 */ BufferAttr_In = (1U << 0), |
| 186 | + /* 0x02 */ BufferAttr_Out = (1U << 1), |
| 187 | + /* 0x04 */ BufferAttr_HipcMapAlias = (1U << 2), |
| 188 | + /* 0x08 */ BufferAttr_HipcPointer = (1U << 3), |
| 189 | + /* 0x10 */ BufferAttr_FixedSize = (1U << 4), |
| 190 | + /* 0x20 */ BufferAttr_HipcAutoSelect = (1U << 5), |
| 191 | + /* 0x40 */ BufferAttr_HipcMapTransferAllowsNonSecure = (1U << 6), |
| 192 | + /* 0x80 */ BufferAttr_HipcMapTransferAllowsNonDevice = (1U << 7), |
| 193 | +}; |
| 194 | + |
| 195 | +template <typename T, int A> |
| 196 | +struct Buffer : public std::span<T> { |
| 197 | + static_assert(std::is_trivially_copyable_v<T>, "Buffer type must be trivially copyable"); |
| 198 | + static_assert((A & BufferAttr_FixedSize) == 0, "Buffer attr must not contain FixedSize"); |
| 199 | + static_assert(((A & BufferAttr_In) == 0) ^ ((A & BufferAttr_Out) == 0), "Buffer attr must be In or Out"); |
| 200 | + static constexpr BufferAttr Attr = static_cast<BufferAttr>(A); |
| 201 | + using Type = T; |
| 202 | + |
| 203 | + /* implicit */ Buffer(const std::span<T>& rhs) : std::span<T>(rhs) {} |
| 204 | + /* implicit */ Buffer() = default; |
| 205 | + |
| 206 | + Buffer& operator=(const std::span<T>& rhs) { |
| 207 | + std::span<T>::operator=(rhs); |
| 208 | + return *this; |
| 209 | + } |
| 210 | + |
| 211 | + T& operator*() const { |
| 212 | + return *this->data(); |
| 213 | + } |
| 214 | + |
| 215 | + explicit operator bool() const { |
| 216 | + return this->size() > 0; |
| 217 | + } |
| 218 | +}; |
| 219 | + |
| 220 | +template <int A> |
| 221 | +using InBuffer = Buffer<const u8, BufferAttr_In | A>; |
| 222 | + |
| 223 | +template <typename T, int A> |
| 224 | +using InArray = Buffer<T, BufferAttr_In | A>; |
| 225 | + |
| 226 | +template <int A> |
| 227 | +using OutBuffer = Buffer<u8, BufferAttr_Out | A>; |
| 228 | + |
| 229 | +template <typename T, int A> |
| 230 | +using OutArray = Buffer<T, BufferAttr_Out | A>; |
| 231 | + |
| 232 | +template <typename T, int A> |
| 233 | +class InLargeData { |
| 234 | +public: |
| 235 | + static_assert(std::is_trivially_copyable_v<T>, "LargeData type must be trivially copyable"); |
| 236 | + static_assert((A & BufferAttr_Out) == 0, "InLargeData attr must not be Out"); |
| 237 | + static constexpr BufferAttr Attr = static_cast<BufferAttr>(A | BufferAttr_In | BufferAttr_FixedSize); |
| 238 | + using Type = const T; |
| 239 | + |
| 240 | + /* implicit */ InLargeData(Type& t) : raw(&t) {} |
| 241 | + ~InLargeData() = default; |
| 242 | + |
| 243 | + InLargeData& operator=(Type* rhs) { |
| 244 | + raw = rhs; |
| 245 | + return *this; |
| 246 | + } |
| 247 | + |
| 248 | + Type* Get() const { |
| 249 | + return raw; |
| 250 | + } |
| 251 | + |
| 252 | + Type& operator*() const { |
| 253 | + return *raw; |
| 254 | + } |
| 255 | + |
| 256 | + Type* operator->() const { |
| 257 | + return raw; |
| 258 | + } |
| 259 | + |
| 260 | + explicit operator bool() const { |
| 261 | + return raw != nullptr; |
| 262 | + } |
| 263 | + |
| 264 | +private: |
| 265 | + Type* raw; |
| 266 | +}; |
| 267 | + |
| 268 | +template <typename T, int A> |
| 269 | +class OutLargeData { |
| 270 | +public: |
| 271 | + static_assert(std::is_trivially_copyable_v<T>, "LargeData type must be trivially copyable"); |
| 272 | + static_assert((A & BufferAttr_In) == 0, "OutLargeData attr must not be In"); |
| 273 | + static constexpr BufferAttr Attr = static_cast<BufferAttr>(A | BufferAttr_Out | BufferAttr_FixedSize); |
| 274 | + using Type = T; |
| 275 | + |
| 276 | + /* implicit */ OutLargeData(const OutLargeData& t) : raw(t.raw) {} |
| 277 | + /* implicit */ OutLargeData(Type* t) : raw(t) {} |
| 278 | + /* implicit */ OutLargeData(AutoOut<T>& t) : raw(&t.raw) {} |
| 279 | + OutLargeData& operator=(const OutLargeData&) = delete; |
| 280 | + |
| 281 | + Type* Get() const { |
| 282 | + return raw; |
| 283 | + } |
| 284 | + |
| 285 | + Type& operator*() const { |
| 286 | + return *raw; |
| 287 | + } |
| 288 | + |
| 289 | + Type* operator->() const { |
| 290 | + return raw; |
| 291 | + } |
| 292 | + |
| 293 | + operator Type*() const { |
| 294 | + return raw; |
| 295 | + } |
| 296 | + |
| 297 | +private: |
| 298 | + Type* raw; |
| 299 | +}; |
| 300 | +// clang-format on |
| 301 | + |
| 302 | +} // namespace Service |
0 commit comments