gdpm/include/result.hpp
David J. Allen 807aa8e5b2 Implemented parallel downloads through CURL multi interface and added purge command
- Update `README.md` file with examples
- Fixed error messages showing no or wrong message
- Changed the prompt message when installing, removing, etc.
- Changed how http::request works
- Added `http::multi` class for parallel downloads
- Removed separate `concepts.hpp` file
TODO: Fix ZIP not extracting after running the `install` command
2023-07-10 20:26:15 -06:00

73 lines
No EOL
1.5 KiB
C++

#pragma once
#include "log.hpp"
#include "error.hpp"
#include <functional>
#include <type_traits>
namespace gdpm{
template <class T, concepts::error_t U = error>
class result_t {
public:
result_t() = delete;
result_t(
std::tuple<T, U> tuple,
std::function<T()> ok = []() -> T{},
std::function<void()> error = [](){}
): data(tuple), fn_ok(ok), fn_error(error)
{ }
result_t(
T target,
U error,
std::function<std::unique_ptr<T>()> _fn_ok = []() -> std::unique_ptr<T>{ return nullptr; },
std::function<void()> _fn_error = [](){}
): data(std::make_tuple(target, error)), fn_ok(_fn_ok), fn_error(_fn_error)
{}
void define(
std::function<T()> ok,
std::function<U()> error
){
fn_ok = ok;
fn_error = error;
}
constexpr U get_error() const{
return std::get<U>(data);
}
constexpr std::unique_ptr<T> unwrap() const {
/* First, check if ok() and error() are defined. */
if(!fn_error || !fn_ok){
log::error(error(ec::NOT_DEFINED));
return nullptr;
}
/* Then, attempt unwrap the data. */
U err = std::get<U>(data);
if (err.has_occurred())
if(fn_error){
fn_error();
return nullptr;
}
return fn_ok();
}
constexpr T unwrap_or(T default_value) const {
U err = std::get<U>(data);
if(err.has_occurred())
return default_value;
return fn_ok();
}
constexpr T unwrap_unsafe() const {
return std::get<T>(data);
}
private:
std::tuple<T, U> data;
std::function<std::unique_ptr<T>()> fn_ok;
std::function<void()> fn_error;
};
}