2017-01-30 16:14:48 -08:00
|
|
|
#pragma once
|
2017-10-13 18:37:41 -07:00
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
#include <vcpkg/base/lineinfo.h>
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
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
|
|
|
};
|
|
|
|
|
2017-04-28 12:55:50 -07:00
|
|
|
const static constexpr NullOpt nullopt{0};
|
2017-03-28 15:05:55 -07:00
|
|
|
|
2017-11-11 21:45:37 -08:00
|
|
|
namespace details
|
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class T, bool B = std::is_copy_constructible<T>::value>
|
2017-11-11 21:45:37 -08:00
|
|
|
struct OptionalStorage
|
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
constexpr OptionalStorage() noexcept : m_is_present(false), m_inactive() {}
|
2017-11-11 21:45:37 -08:00
|
|
|
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)) {}
|
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
~OptionalStorage() noexcept
|
|
|
|
{
|
|
|
|
if (m_is_present) m_t.~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage(const OptionalStorage& o) : m_is_present(o.m_is_present), m_inactive()
|
|
|
|
{
|
|
|
|
if (m_is_present) new (&m_t) T(o.m_t);
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage(OptionalStorage&& o) : m_is_present(o.m_is_present), m_inactive()
|
|
|
|
{
|
|
|
|
if (m_is_present)
|
|
|
|
{
|
|
|
|
new (&m_t) T(std::move(o.m_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage& operator=(const OptionalStorage& o)
|
|
|
|
{
|
|
|
|
if (m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_t = o.m_t;
|
|
|
|
}
|
|
|
|
else if (!m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_is_present = true;
|
|
|
|
new (&m_t) T(o.m_t);
|
|
|
|
}
|
|
|
|
else if (m_is_present && !o.m_is_present)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage& operator=(OptionalStorage&& o)
|
|
|
|
{
|
|
|
|
if (m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_t = std::move(o.m_t);
|
|
|
|
}
|
|
|
|
else if (!m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_is_present = true;
|
|
|
|
new (&m_t) T(std::move(o.m_t));
|
|
|
|
}
|
|
|
|
else if (m_is_present && !o.m_is_present)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-11-11 21:45:37 -08:00
|
|
|
constexpr bool has_value() const { return m_is_present; }
|
|
|
|
|
|
|
|
const T& value() const { return this->m_t; }
|
|
|
|
T& value() { return this->m_t; }
|
|
|
|
|
|
|
|
private:
|
2019-04-08 23:26:18 -07:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_is_present = false;
|
|
|
|
m_t.~T();
|
|
|
|
m_inactive = '\0';
|
|
|
|
}
|
|
|
|
|
2017-11-11 21:45:37 -08:00
|
|
|
bool m_is_present;
|
2019-04-08 23:26:18 -07:00
|
|
|
union {
|
|
|
|
char m_inactive;
|
|
|
|
T m_t;
|
|
|
|
};
|
2017-11-11 21:45:37 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
2019-04-08 23:26:18 -07:00
|
|
|
struct OptionalStorage<T, false>
|
|
|
|
{
|
|
|
|
constexpr OptionalStorage() noexcept : m_is_present(false), m_inactive() {}
|
|
|
|
constexpr OptionalStorage(T&& t) : m_is_present(true), m_t(std::move(t)) {}
|
|
|
|
|
|
|
|
~OptionalStorage() noexcept
|
|
|
|
{
|
|
|
|
if (m_is_present) m_t.~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage(OptionalStorage&& o) : m_is_present(o.m_is_present), m_inactive()
|
|
|
|
{
|
|
|
|
if (m_is_present)
|
|
|
|
{
|
|
|
|
new (&m_t) T(std::move(o.m_t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OptionalStorage& operator=(OptionalStorage&& o)
|
|
|
|
{
|
|
|
|
if (m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_t = std::move(o.m_t);
|
|
|
|
}
|
|
|
|
else if (!m_is_present && o.m_is_present)
|
|
|
|
{
|
|
|
|
m_is_present = true;
|
|
|
|
new (&m_t) T(std::move(o.m_t));
|
|
|
|
}
|
|
|
|
else if (m_is_present && !o.m_is_present)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool has_value() const { return m_is_present; }
|
|
|
|
|
|
|
|
const T& value() const { return this->m_t; }
|
|
|
|
T& value() { return this->m_t; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_is_present = false;
|
|
|
|
m_t.~T();
|
|
|
|
m_inactive = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool m_is_present;
|
|
|
|
union {
|
|
|
|
char m_inactive;
|
|
|
|
T m_t;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, bool B>
|
|
|
|
struct OptionalStorage<T&, B>
|
2017-11-11 21:45:37 -08:00
|
|
|
{
|
2018-04-12 00:47:17 -07:00
|
|
|
constexpr OptionalStorage() noexcept : m_t(nullptr) {}
|
2017-11-11 21:45:37 -08:00
|
|
|
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;
|
|
|
|
};
|
2019-04-08 23:26:18 -07:00
|
|
|
|
|
|
|
// Note: implemented in checks.cpp to cut the header dependency
|
|
|
|
void exit_if_null(bool b, const LineInfo& line_info);
|
2017-11-11 21:45:37 -08:00
|
|
|
}
|
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
template<class T>
|
2017-11-11 21:45:37 -08:00
|
|
|
struct Optional
|
2017-03-28 15:05:55 -07:00
|
|
|
{
|
2018-04-12 00:47:17 -07:00
|
|
|
constexpr Optional() noexcept {}
|
2017-04-12 16:11:31 -07:00
|
|
|
|
2017-03-28 15:05:55 -07:00
|
|
|
// Constructors are intentionally implicit
|
2017-11-11 21:45:37 -08:00
|
|
|
constexpr Optional(NullOpt) {}
|
2017-03-28 15:05:55 -07:00
|
|
|
|
2019-04-08 23:26:18 -07:00
|
|
|
template<class U, class = std::enable_if_t<!std::is_same<std::decay_t<U>, Optional>::value>>
|
|
|
|
constexpr Optional(U&& t) : m_base(std::forward<U>(t))
|
2017-11-11 21:45:37 -08:00
|
|
|
{
|
|
|
|
}
|
2017-03-28 15:05:55 -07:00
|
|
|
|
2017-03-31 16:29:04 -07:00
|
|
|
T&& value_or_exit(const LineInfo& line_info) &&
|
2017-03-28 15:05:55 -07:00
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
details::exit_if_null(this->m_base.has_value(), line_info);
|
2017-11-11 21:45:37 -08:00
|
|
|
return std::move(this->m_base.value());
|
2017-03-28 15:05:55 -07:00
|
|
|
}
|
|
|
|
|
2018-04-10 04:48:02 -07:00
|
|
|
T& value_or_exit(const LineInfo& line_info) &
|
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
details::exit_if_null(this->m_base.has_value(), line_info);
|
2018-04-10 04:48:02 -07:00
|
|
|
return this->m_base.value();
|
|
|
|
}
|
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
const T& value_or_exit(const LineInfo& line_info) const&
|
2017-03-28 15:05:55 -07:00
|
|
|
{
|
2019-04-08 23:26:18 -07:00
|
|
|
details::exit_if_null(this->m_base.has_value(), line_info);
|
2017-11-11 21:45:37 -08:00
|
|
|
return this->m_base.value();
|
2017-03-28 15:05:55 -07:00
|
|
|
}
|
|
|
|
|
2017-11-11 21:45:37 -08:00
|
|
|
constexpr explicit operator bool() const { return this->m_base.has_value(); }
|
2017-03-28 15:05:55 -07:00
|
|
|
|
2017-11-11 21:45:37 -08:00
|
|
|
constexpr bool has_value() const { return this->m_base.has_value(); }
|
2017-03-28 15:05:55 -07:00
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
template<class U>
|
2017-10-13 18:37:41 -07:00
|
|
|
T value_or(U&& default_value) const&
|
2017-03-28 15:05:55 -07:00
|
|
|
{
|
2017-11-11 21:45:37 -08: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
|
|
|
}
|
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
template<class U>
|
2017-03-28 15:05:55 -07:00
|
|
|
T value_or(U&& default_value) &&
|
|
|
|
{
|
2017-11-11 21:45:37 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-11 21:45:37 -08: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
|
|
|
|
2017-11-11 21:45:37 -08: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:
|
2017-11-11 21:45:37 -08:00
|
|
|
details::OptionalStorage<T> m_base;
|
2017-03-28 15:05:55 -07:00
|
|
|
};
|
2017-04-28 17:27:07 -07:00
|
|
|
|
2017-07-26 16:25:24 -07:00
|
|
|
template<class U>
|
|
|
|
Optional<std::decay_t<U>> make_optional(U&& u)
|
|
|
|
{
|
|
|
|
return Optional<std::decay_t<U>>(std::forward<U>(u));
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:27:07 -07:00
|
|
|
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
|
|
|
}
|