libtransistor
A userland library for the Nintendo Switch
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
types.hpp
1 #pragma once
2 
3 #include<libtransistor/types.h>
4 #include<libtransistor/err.h>
5 #include<expected.hpp>
6 
7 #include<stdexcept>
8 #include<string>
9 #include<optional>
10 
11 namespace trn {
12 
13 struct ResultCode {
14  public:
15  static tl::expected<std::nullopt_t, ResultCode> ExpectOk(result_t code);
16  static void AssertOk(result_t code);
17  template<typename T> static T&& AssertOk(tl::expected<T, ResultCode> &&monad);
18 
19  ResultCode(result_t code);
20  inline bool IsOk() {
21  return code == RESULT_OK;
22  }
23 
24  inline bool operator==(const ResultCode &other) const {
25  return code == other.code;
26  }
27 
28  trn_result_description_t Lookup();
29 
30  result_t code;
31 };
32 
33 template<typename T>
34 using Result = tl::expected<T, ResultCode>;
35 
36 class ResultError : public std::runtime_error {
37  public:
38  ResultError(ResultCode code);
39  ResultError(result_t code);
40 
41  virtual const char *what() const noexcept override;
42 
43  const ResultCode code;
44  private:
45  std::string description;
46 };
47 
48 template<typename T> T&& ResultCode::AssertOk(tl::expected<T, ResultCode> &&monad) {
49  if(monad) {
50  return std::move(*monad);
51  } else {
52  throw ResultError(monad.error());
53  }
54 }
55 
56 class KObject {
57  public:
58  KObject() = default;
59  KObject(handle_t handle);
60  KObject(const KObject &) = delete;
61  KObject &operator=(const KObject &) = delete;
62  KObject(KObject &&other);
63  KObject &operator=(KObject &&other);
64  ~KObject();
65 
66  handle_t Claim();
67 
68  handle_t handle = 0;
69 };
70 
71 class KWaitable : public KObject {
72  public:
73  KWaitable() = default;
74  KWaitable(handle_t handle);
75 };
76 
77 class KSharedMemory : public KObject {
78  public:
79  KSharedMemory() = default;
80 
81  // foreign shared memory
82  KSharedMemory(shared_memory_h handle, size_t size, uint32_t foreign_permission);
83 
84  size_t size;
85  uint32_t foreign_permission;
86 };
87 
88 class KTransferMemory : public KObject {
89  public:
90  KTransferMemory() = default;
91 
92  // foreign transfer memory
93  KTransferMemory(transfer_memory_h handle, size_t size, uint32_t permissions);
94 
95  // owned transfer memory
96  KTransferMemory(transfer_memory_h handle, void *backing_buffer, size_t size, uint32_t permissions);
97  KTransferMemory(size_t size, uint32_t permissions);
98  KTransferMemory(void *backing_buffer, size_t size, uint32_t permissions, bool owns_buffer=false);
99 
100  // misc
102  KTransferMemory &operator=(KTransferMemory &&other);
103  ~KTransferMemory();
104 
105  uint8_t *buffer = nullptr;
106  size_t size = 0;
107  uint32_t permissions = 0;
108  private:
109  bool owns_buffer = false;
110 };
111 
112 class KPort : public KWaitable {
113  public:
114  KPort() = default;
115  KPort(port_h handle);
116 };
117 
118 class KProcess : public KWaitable {
119  public:
120  KProcess() = default;
121  KProcess(process_h handle);
122  Result<std::nullopt_t> ResetSignal();
123  Result<std::nullopt_t> WaitSignal(uint64_t timeout);
124 };
125 
126 class KEvent : public KWaitable {
127  public:
128  KEvent() = default;
129  KEvent(revent_h handle);
130  Result<std::nullopt_t> ResetSignal();
131  Result<std::nullopt_t> WaitSignal(uint64_t timeout);
132 };
133 
134 // Writable event
135 class KWEvent : public KObject {
136  public:
137  KWEvent() = default;
138  KWEvent(KEvent &read_end); // creates a new event
139  KWEvent(wevent_h handle);
140  Result<std::nullopt_t> Signal();
141 };
142 
143 class KDebug : public KWaitable {
144  public:
145  KDebug() = default;
146  KDebug(debug_h handle);
147 };
148 
149 class KResourceLimit : public KObject {
150  public:
151  KResourceLimit() = default;
153 };
154 
155 }
Various system types.
handle_t debug_h
Debug handle.
Definition: types.h:46
Definition: types.hpp:56
Definition: types.hpp:149
handle_t shared_memory_h
Shared Memory handle.
Definition: types.h:40
uint32_t result_t
Function result.
Definition: types.h:51
handle_t revent_h
revent handle
Definition: types.h:47
handle_t process_h
Process handle.
Definition: types.h:45
Definition: types.hpp:126
Definition: types.hpp:118
Definition: types.hpp:77
handle_t wevent_h
wevent handle
Definition: types.h:48
Error definitions.
handle_t port_h
Port handle.
Definition: types.h:43
Definition: types.hpp:13
uint32_t handle_t
Resource handle.
Definition: types.h:38
handle_t resource_limit_h
Resource limit handle.
Definition: types.h:50
Definition: types.hpp:36
Definition: types.hpp:143
Definition: types.hpp:112
Definition: err.h:15
Definition: types.hpp:135
Definition: types.hpp:88
Definition: types.hpp:71
handle_t transfer_memory_h
Transfer Memory handle.
Definition: types.h:41