vcpkg/toolsrc/include/vcpkg_expected.h

115 lines
2.8 KiB
C
Raw Normal View History

2016-09-19 11:50:08 +08:00
#pragma once
#include "vcpkg_Checks.h"
#include <system_error>
2016-09-19 11:50:08 +08:00
namespace vcpkg
{
2017-06-06 06:58:47 +08:00
template<class Err>
struct ErrorHolder
2016-09-19 11:50:08 +08:00
{
2017-06-06 06:58:47 +08:00
ErrorHolder() : m_is_error(false) {}
template<class U>
ErrorHolder(U&& err) : m_is_error(true), m_err(std::forward<U>(err))
{
}
2017-06-06 06:58:47 +08:00
constexpr bool has_error() const { return m_is_error; }
const Err& error() const { return m_err; }
Err& error() { return m_err; }
CStringView to_string() const { return "value was error"; }
private:
bool m_is_error;
Err m_err;
};
template<>
struct ErrorHolder<std::error_code>
{
ErrorHolder() = default;
ErrorHolder(const std::error_code& err) : m_err(err) {}
constexpr bool has_error() const { return bool(m_err); }
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
const std::error_code& error() const { return m_err; }
std::error_code& error() { return m_err; }
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
CStringView to_string() const { return "value was error"; }
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
private:
std::error_code m_err;
};
template<class T, class S>
class ExpectedT
{
public:
constexpr ExpectedT() = default;
// Constructors are intentionally implicit
ExpectedT(const S& s) : m_s(s) {}
ExpectedT(S&& s) : m_s(std::move(s)) {}
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
ExpectedT(const T& t) : m_t(t) {}
ExpectedT(T&& t) : m_t(std::move(t)) {}
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
ExpectedT(const ExpectedT&) = default;
ExpectedT(ExpectedT&&) = default;
ExpectedT& operator=(const ExpectedT&) = default;
ExpectedT& operator=(ExpectedT&&) = default;
2016-09-19 11:50:08 +08:00
2017-06-06 06:58:47 +08:00
explicit constexpr operator bool() const noexcept { return !m_s.has_error(); }
constexpr bool has_value() const noexcept { return !m_s.has_error(); }
2016-09-19 11:50:08 +08:00
T&& value_or_exit(const LineInfo& line_info) &&
2016-09-19 11:50:08 +08:00
{
exit_if_error(line_info);
2016-09-19 11:50:08 +08:00
return std::move(this->m_t);
}
const T& value_or_exit(const LineInfo& line_info) const &
2016-09-19 11:50:08 +08:00
{
exit_if_error(line_info);
2016-09-19 11:50:08 +08:00
return this->m_t;
}
2017-06-06 06:58:47 +08:00
const S& error() const & { return this->m_s.error(); }
S&& error() && { return std::move(this->m_s.error()); }
2016-09-19 11:50:08 +08:00
const T* get() const
{
2017-06-06 06:58:47 +08:00
if (!this->has_value())
2016-09-19 11:50:08 +08:00
{
return nullptr;
}
return &this->m_t;
}
T* get()
{
2017-06-06 06:58:47 +08:00
if (!this->has_value())
2016-09-19 11:50:08 +08:00
{
return nullptr;
}
return &this->m_t;
}
private:
void exit_if_error(const LineInfo& line_info) const
2016-09-19 11:50:08 +08:00
{
2017-06-06 06:58:47 +08:00
Checks::check_exit(line_info, !m_s.has_error(), m_s.to_string());
2016-09-19 11:50:08 +08:00
}
2017-06-06 06:58:47 +08:00
ErrorHolder<S> m_s;
2016-09-19 11:50:08 +08:00
T m_t;
};
2017-06-06 06:58:47 +08:00
template<class T>
using Expected = ExpectedT<T, std::error_code>;
2016-09-19 11:50:08 +08:00
}