Skip to content

Commit

Permalink
backup: add result<T> class
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsura committed Nov 6, 2023
1 parent ae70dea commit c9e6cf4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions core/src/filed/backup.cc
Expand Up @@ -51,8 +51,52 @@
#include "lib/serial.h"
#include "lib/compression.h"

#include <variant>

namespace filedaemon {

// fixme(ssura): replace by std::expected<T, PoolMem> (C++23)
// or std::expected<T, std::string>
template <typename T> class result {
public:
constexpr result() : data{1, "Not Initialized"} {}

template <typename Arg0, typename... Args>
constexpr result(Arg0 arg0, Args... args)
: data(std::forward<Arg0>(arg0), std::forward<Args>(args)...)
{
}

bool holds_error() const { return data.index() == 1; }
PoolMem* error()
{
if (holds_error()) {
return &std::get<1>(data);
} else {
return nullptr;
}
}

T* value()
{
if (!holds_error()) {
return &std::get<0>(data);
} else {
return nullptr;
}
}

PoolMem& error_unchecked() { return std::get<1>(data); }

T& value_unchecked() { return std::get<0>(data); }

private:
// we are using PoolMem here, since it is easier to format into a PoolMem.
// Once we have access to std::format everywhere, this should change
// to std::string.
std::variant<T, PoolMem> data;
};

#ifdef HAVE_DARWIN_OS
const bool have_darwin_os = true;
#else
Expand Down

0 comments on commit c9e6cf4

Please sign in to comment.