summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/containers/sentinel_optional-inl.h
blob: a5676afa7f398a447ae77493fd60d65ce1cce2c5 (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef SENTINEL_OPTIONAL_INL_H_
#error "Direct inclusion of this file is not allowed, include sentinel_optional.h"
// For the sake of sane code completion.
#include "sentinel_optional.h"
#endif

#include <library/cpp/yt/assert/assert.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>::TSentinelOptional(std::nullopt_t) noexcept
    : Value_(TSentinel::Sentinel)
{ }

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>::TSentinelOptional(T value) noexcept
    : Value_(value)
{ }

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>::TSentinelOptional(std::optional<T> opt) noexcept
    : Value_(opt ? *opt : TSentinel::Sentinel)
{ }

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>& TSentinelOptional<T, TSentinel>::operator=(std::nullopt_t) noexcept
{
    Value_ = TSentinel::Sentinel;
    return *this;
}

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>& TSentinelOptional<T, TSentinel>::operator=(T value) noexcept
{
    Value_ = value;
    return *this;
}

template <class T, class TSentinel>
constexpr bool TSentinelOptional<T, TSentinel>::has_value() const noexcept
{
    return Value_ != TSentinel::Sentinel;
}

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>::operator bool() const noexcept
{
    return has_value();
}

template <class T, class TSentinel>
constexpr T& TSentinelOptional<T, TSentinel>::operator*() noexcept
{
    return Value_;
}

template <class T, class TSentinel>
constexpr const T& TSentinelOptional<T, TSentinel>::operator*() const noexcept
{
    return Value_;
}

template <class T, class TSentinel>
constexpr T* TSentinelOptional<T, TSentinel>::operator->() noexcept
{
    return &Value_;
}

template <class T, class TSentinel>
constexpr const T* TSentinelOptional<T, TSentinel>::operator->() const noexcept
{
    return &Value_;
}

template <class T, class TSentinel>
T& TSentinelOptional<T, TSentinel>::value() noexcept
{
    YT_ASSERT(has_value());
    return Value_;
}

template <class T, class TSentinel>
const T& TSentinelOptional<T, TSentinel>::value() const noexcept
{
    YT_ASSERT(has_value());
    return Value_;
}

template <class T, class TSentinel>
constexpr T TSentinelOptional<T, TSentinel>::value_or(T default_value) const noexcept
{
    return has_value() ? Value_ : default_value;
}

template <class T, class TSentinel>
constexpr void TSentinelOptional<T, TSentinel>::reset() noexcept
{
    Value_ = TSentinel::Sentinel;
}

template <class T, class TSentinel>
constexpr TSentinelOptional<T, TSentinel>::operator std::optional<T>() const noexcept
{
    return has_value() ? std::optional<T>(Value_) : std::nullopt;
}

template <class T, class TSentinel>
constexpr bool TSentinelOptional<T, TSentinel>::operator==(std::nullopt_t) const noexcept
{
    return !has_value();
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT