finish refactor, add docs and CI
This commit is contained in:
@@ -4,14 +4,13 @@
|
||||
|
||||
namespace nxst {
|
||||
|
||||
template <class T, class E = std::string>
|
||||
class Result {
|
||||
template <class T, class E = std::string> class Result {
|
||||
bool ok;
|
||||
alignas(T) alignas(E) unsigned char storage[sizeof(T) > sizeof(E) ? sizeof(T) : sizeof(E)];
|
||||
|
||||
Result() = default;
|
||||
|
||||
public:
|
||||
public:
|
||||
static Result success(T val) {
|
||||
Result res;
|
||||
res.ok = true;
|
||||
@@ -27,37 +26,52 @@ public:
|
||||
}
|
||||
|
||||
~Result() {
|
||||
if (ok) reinterpret_cast<T*>(storage)->~T();
|
||||
else reinterpret_cast<E*>(storage)->~E();
|
||||
if (ok)
|
||||
reinterpret_cast<T*>(storage)->~T();
|
||||
else
|
||||
reinterpret_cast<E*>(storage)->~E();
|
||||
}
|
||||
|
||||
Result(const Result& other) : ok(other.ok) {
|
||||
if (ok) new (storage) T(*reinterpret_cast<const T*>(other.storage));
|
||||
else new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
if (ok)
|
||||
new (storage) T(*reinterpret_cast<const T*>(other.storage));
|
||||
else
|
||||
new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
}
|
||||
|
||||
Result(Result&& other) : ok(other.ok) {
|
||||
if (ok) new (storage) T(std::move(*reinterpret_cast<T*>(other.storage)));
|
||||
else new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
if (ok)
|
||||
new (storage) T(std::move(*reinterpret_cast<T*>(other.storage)));
|
||||
else
|
||||
new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
}
|
||||
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
bool isOk() const noexcept { return ok; }
|
||||
const T& value() const { return *reinterpret_cast<const T*>(storage); }
|
||||
const E& error() const { return *reinterpret_cast<const E*>(storage); }
|
||||
bool isOk() const noexcept {
|
||||
return ok;
|
||||
}
|
||||
const T& value() const {
|
||||
return *reinterpret_cast<const T*>(storage);
|
||||
}
|
||||
const E& error() const {
|
||||
return *reinterpret_cast<const E*>(storage);
|
||||
}
|
||||
};
|
||||
|
||||
// Specialisation for Result<void>
|
||||
template <class E>
|
||||
class Result<void, E> {
|
||||
template <class E> class Result<void, E> {
|
||||
bool ok;
|
||||
alignas(E) unsigned char storage[sizeof(E)];
|
||||
|
||||
Result() = default;
|
||||
|
||||
public:
|
||||
static Result success() { Result res; res.ok = true; return res; }
|
||||
public:
|
||||
static Result success() {
|
||||
Result res;
|
||||
res.ok = true;
|
||||
return res;
|
||||
}
|
||||
|
||||
static Result failure(E err) {
|
||||
Result res;
|
||||
@@ -66,20 +80,29 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
~Result() { if (!ok) reinterpret_cast<E*>(storage)->~E(); }
|
||||
~Result() {
|
||||
if (!ok)
|
||||
reinterpret_cast<E*>(storage)->~E();
|
||||
}
|
||||
|
||||
Result(const Result& other) : ok(other.ok) {
|
||||
if (!ok) new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
if (!ok)
|
||||
new (storage) E(*reinterpret_cast<const E*>(other.storage));
|
||||
}
|
||||
|
||||
Result(Result&& other) : ok(other.ok) {
|
||||
if (!ok) new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
if (!ok)
|
||||
new (storage) E(std::move(*reinterpret_cast<E*>(other.storage)));
|
||||
}
|
||||
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
bool isOk() const noexcept { return ok; }
|
||||
const E& error() const { return *reinterpret_cast<const E*>(storage); }
|
||||
bool isOk() const noexcept {
|
||||
return ok;
|
||||
}
|
||||
const E& error() const {
|
||||
return *reinterpret_cast<const E*>(storage);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nxst
|
||||
} // namespace nxst
|
||||
|
||||
Reference in New Issue
Block a user