137 lines
3.6 KiB
C
Raw Normal View History

2017-01-30 16:14:48 -08:00
#pragma once
#include <vcpkg/base/checks.h>
2017-01-30 16:14:48 -08:00
2017-03-28 15:05:55 -07:00
namespace vcpkg
{
2017-04-03 16:27:51 -07:00
struct NullOpt
2017-03-28 15:05:55 -07:00
{
2017-04-03 16:27:51 -07:00
explicit constexpr NullOpt(int) {}
2017-03-28 15:05:55 -07:00
};
const static constexpr NullOpt nullopt{0};
2017-03-28 15:05:55 -07:00
namespace details
{
template<class T>
struct OptionalStorage
{
constexpr OptionalStorage() : m_is_present(false), m_t() {}
constexpr OptionalStorage(const T& t) : m_is_present(true), m_t(t) {}
constexpr OptionalStorage(T&& t) : m_is_present(true), m_t(std::move(t)) {}
constexpr bool has_value() const { return m_is_present; }
const T& value() const { return this->m_t; }
T& value() { return this->m_t; }
private:
bool m_is_present;
T m_t;
};
template<class T>
struct OptionalStorage<T&>
{
constexpr OptionalStorage() : m_t(nullptr) {}
constexpr OptionalStorage(T& t) : m_t(&t) {}
constexpr bool has_value() const { return m_t != nullptr; }
T& value() const { return *this->m_t; }
private:
T* m_t;
};
}
template<class T>
struct Optional
2017-03-28 15:05:55 -07:00
{
constexpr Optional() {}
2017-04-12 16:11:31 -07:00
2017-03-28 15:05:55 -07:00
// Constructors are intentionally implicit
constexpr Optional(NullOpt) {}
2017-03-28 15:05:55 -07:00
2017-12-14 14:31:16 -08:00
template<class U>
Optional(U&& t) : m_base(std::forward<U>(t))
{
}
2017-03-28 15:05:55 -07:00
T&& value_or_exit(const LineInfo& line_info) &&
2017-03-28 15:05:55 -07:00
{
this->exit_if_null(line_info);
return std::move(this->m_base.value());
2017-03-28 15:05:55 -07:00
}
const T& value_or_exit(const LineInfo& line_info) const&
2017-03-28 15:05:55 -07:00
{
this->exit_if_null(line_info);
return this->m_base.value();
2017-03-28 15:05:55 -07:00
}
constexpr explicit operator bool() const { return this->m_base.has_value(); }
2017-03-28 15:05:55 -07:00
constexpr bool has_value() const { return this->m_base.has_value(); }
2017-03-28 15:05:55 -07:00
template<class U>
T value_or(U&& default_value) const&
2017-03-28 15:05:55 -07:00
{
return this->m_base.has_value() ? this->m_base.value() : static_cast<T>(std::forward<U>(default_value));
2017-03-28 15:05:55 -07:00
}
template<class U>
2017-03-28 15:05:55 -07:00
T value_or(U&& default_value) &&
{
return this->m_base.has_value() ? std::move(this->m_base.value())
: static_cast<T>(std::forward<U>(default_value));
2017-03-28 15:05:55 -07:00
}
typename std::add_pointer<const T>::type get() const
{
return this->m_base.has_value() ? &this->m_base.value() : nullptr;
}
2017-03-28 15:05:55 -07:00
typename std::add_pointer<T>::type get() { return this->m_base.has_value() ? &this->m_base.value() : nullptr; }
2017-03-28 15:05:55 -07:00
private:
void exit_if_null(const LineInfo& line_info) const
{
Checks::check_exit(line_info, this->m_base.has_value(), "Value was null");
2017-03-28 15:05:55 -07:00
}
details::OptionalStorage<T> m_base;
2017-03-28 15:05:55 -07:00
};
template<class U>
Optional<std::decay_t<U>> make_optional(U&& u)
{
return Optional<std::decay_t<U>>(std::forward<U>(u));
}
template<class T>
bool operator==(const Optional<T>& o, const T& t)
{
if (auto p = o.get()) return *p == t;
return false;
}
template<class T>
bool operator==(const T& t, const Optional<T>& o)
{
if (auto p = o.get()) return t == *p;
return false;
}
template<class T>
bool operator!=(const Optional<T>& o, const T& t)
{
if (auto p = o.get()) return *p != t;
return true;
}
template<class T>
bool operator!=(const T& t, const Optional<T>& o)
{
if (auto p = o.get()) return t != *p;
return true;
}
2017-03-28 15:05:55 -07:00
}