2016-09-18 20:50:08 -07:00
|
|
|
#pragma once
|
|
|
|
|
2016-11-15 17:53:23 -08:00
|
|
|
#include <system_error>
|
2016-09-18 20:50:08 -07:00
|
|
|
#include "vcpkg_Checks.h"
|
|
|
|
|
|
|
|
namespace vcpkg
|
|
|
|
{
|
|
|
|
template <class T>
|
2017-04-03 16:26:54 -07:00
|
|
|
class Expected
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
public:
|
2017-03-31 16:33:10 -07:00
|
|
|
// Constructors are intentionally implicit
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected(const std::error_code& ec) : m_error_code(ec), m_t()
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected(std::errc ec) : Expected(std::make_error_code(ec))
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected(const T& t) : m_error_code(), m_t(t)
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected(T&& t) : m_error_code(), m_t(std::move(t))
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected() : Expected(std::error_code(), T())
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-03 16:26:54 -07:00
|
|
|
Expected(const Expected&) = default;
|
|
|
|
Expected(Expected&&) = default;
|
|
|
|
Expected& operator=(const Expected&) = default;
|
|
|
|
Expected& operator=(Expected&&) = default;
|
2016-09-18 20:50:08 -07:00
|
|
|
|
|
|
|
std::error_code error_code() const
|
|
|
|
{
|
|
|
|
return this->m_error_code;
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:23:48 -07:00
|
|
|
T&& value_or_exit(const LineInfo& line_info) &&
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-03-28 12:57:00 -07:00
|
|
|
exit_if_error(line_info);
|
2016-09-18 20:50:08 -07:00
|
|
|
return std::move(this->m_t);
|
|
|
|
}
|
|
|
|
|
2017-03-31 16:23:48 -07:00
|
|
|
const T& value_or_exit(const LineInfo& line_info) const &
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-03-28 12:57:00 -07:00
|
|
|
exit_if_error(line_info);
|
2016-09-18 20:50:08 -07:00
|
|
|
return this->m_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* get() const
|
|
|
|
{
|
|
|
|
if (m_error_code)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &this->m_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* get()
|
|
|
|
{
|
|
|
|
if (m_error_code)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &this->m_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-28 12:57:00 -07:00
|
|
|
void exit_if_error(const LineInfo& line_info) const
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-03-28 12:57:00 -07:00
|
|
|
Checks::check_exit(line_info, !this->m_error_code, this->m_error_code.message());
|
2016-09-18 20:50:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code m_error_code;
|
|
|
|
T m_t;
|
|
|
|
};
|
|
|
|
}
|