pwn++  0.1.4
A (toy) Windows & Linux pwn library to play with modern C++.
Error.hpp
1 #pragma once
12 
13 #include <cstdint>
14 #include <iomanip>
15 #include <iostream>
16 #include <string_view>
17 #include <variant>
18 
22 enum class ErrorCode : uint32_t
23 {
25  UnknownError = 0,
26 
28  GenericError,
29 
31  RuntimeError,
32 
34  InvalidProcess,
35 
37  InvalidThread,
38 
40  InvalidObject,
41 
43  InvalidInput,
44 
46  InvalidParameter,
47 
49  InvalidState,
50 
52  PermissionDenied,
53 
55  InsufficientPrivilegeError,
56 
58  UnexpectedType,
59 
61  ArithmeticError,
62 
64  OverflowError,
65 
67  UnderflowError,
68 
70  IllegalValue,
71 
73  NotImplementedError,
74 
76  PendingIoError,
77 
79  ConnectionError,
80 
82  TerminationError,
83 
85  AllocationError,
86 
88  ParsingError,
89 
91  BufferTooBig,
92 
94  BufferTooSmall,
95 
97  NotInitialized,
98 
100  InitializationFailed,
101 
103  ServiceError,
104 
106  FilesystemError,
107 
109  AlpcError,
110 
112  ExternalError,
113 
115  ExternalApiCallFailed,
116 
118  NoMoreData,
119 
121  PartialResult,
122 
124  BadVersion,
125 
127  BadSignature,
128 
130  NotFound,
131 
133  NotConnected,
134 
136  AlreadyExists,
137 
139  SizeMismatch,
140 
142  MalformedFile,
143 };
144 
145 
151 template<typename T>
152 constexpr auto
153 Ok(T&& arg)
154 {
155  return std::forward<T>(arg);
156 }
157 
161 struct Err
162 {
163  ErrorCode Code {ErrorCode::UnknownError};
164  uint32_t LastError {0};
165 
166  bool
167  operator==(const Err& rhs) const
168  {
169  return rhs.Code == this->Code && rhs.LastError == this->LastError;
170  }
171 
172  bool
173  operator==(ErrorCode Code) const
174  {
175  return Code == this->Code;
176  }
177 };
178 
179 
185 template<typename T>
186 using Result = std::variant<T, Err>;
187 
188 
197 template<typename T>
198 constexpr bool
199 Failed(Result<T> const& Res) noexcept
200 {
201  return std::get_if<Err>(&Res) != nullptr;
202 }
203 
204 
213 template<class T>
214 constexpr bool
215 Success(Result<T> const& Result) noexcept
216 {
217  return !Failed(Result);
218 }
219 
220 
229 template<typename T>
230 T
231 Value(Result<T>&& SuccessResult)
232 {
233  return std::move(std::get<T>(SuccessResult));
234 }
235 
236 template<typename T>
237 T
238 Value(Result<T>& SuccessResult)
239 {
240  T copy = std::get<T>(SuccessResult);
241  return copy;
242 }
243 
251 template<typename T>
252 const Err&
253 Error(Result<T> const& ErrorResult)
254 {
255  return std::get<Err>(ErrorResult);
256 }
257 
258 
267 template<typename T>
268 T
269 ValueOr(Result<T>&& Result, T AlternativeValue)
270 {
271  return Success(Result) ? std::move(Value(Result)) : AlternativeValue;
272 }
273 
274 template<typename T>
275 T
276 ValueOr(Result<T>& Result, T AlternativeValue)
277 {
278  return Success(Result) ? Value(Result) : AlternativeValue;
279 }
Templated return value for failure cases.
Definition: Error.hpp:162