blob: 380e54f1cfe0083e9cdec3015998bd566127fbfa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
#pragma once
#include "vcpkg_Checks.h"
namespace vcpkg
{
struct NullOpt
{
explicit constexpr NullOpt(int) {}
};
const static constexpr NullOpt nullopt{ 0 };
template <class T>
class Optional
{
public:
constexpr Optional() : m_is_present(false), m_t() { }
// Constructors are intentionally implicit
constexpr Optional(NullOpt) : m_is_present(false), m_t() { }
Optional(const T& t) : m_is_present(true), m_t(t) { }
Optional(T&& t) : m_is_present(true), m_t(std::move(t)) { }
T&& value_or_exit(const LineInfo& line_info) &&
{
this->exit_if_null(line_info);
return std::move(this->m_t);
}
const T& value_or_exit(const LineInfo& line_info) const &
{
this->exit_if_null(line_info);
return this->m_t;
}
constexpr explicit operator bool() const
{
return this->m_is_present;
}
constexpr bool has_value() const
{
return m_is_present;
}
template <class U>
T value_or(U&& default_value) const &
{
return bool(*this) ? this->m_t : static_cast<T>(std::forward<U>(default_value));
}
template <class U>
T value_or(U&& default_value) &&
{
return bool(*this) ? std::move(this->m_t) : static_cast<T>(std::forward<U>(default_value));
}
const T* get() const
{
return bool(*this) ? &this->m_t : nullptr;
}
T* get()
{
return bool(*this) ? &this->m_t : nullptr;
}
private:
void exit_if_null(const LineInfo& line_info) const
{
Checks::check_exit(line_info, this->m_is_present, "Value was null");
}
bool m_is_present;
T m_t;
};
}
|