-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptional_ext.h
More file actions
129 lines (110 loc) · 4.33 KB
/
Copy pathoptional_ext.h
File metadata and controls
129 lines (110 loc) · 4.33 KB
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
119
120
121
122
123
124
125
126
127
128
129
#ifndef OPTIONAL_EXT_H
#define OPTIONAL_EXT_H
#include <optional>
namespace knatten {
template <class T>
class optional {
public:
//The most necessary constructors for the example
optional() noexcept = default;
optional(const optional<T>& rhs) = default;
optional(optional<T>&& rhs) noexcept = default;
optional(T val) : o_(std::move(val)) { }
//Demonstration of the proposed methods
template <class UnaryOperation>
constexpr decltype(auto) transform(UnaryOperation op) & {
using OptionalReturnType = optional<decltype(op(*o_))>;
return has_value() ?
OptionalReturnType(op(*o_)) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform(UnaryOperation op) const& {
using OptionalReturnType = optional<decltype(op(*o_))>;
return has_value() ?
OptionalReturnType(op(*o_)) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform(UnaryOperation op) &&{
using OptionalReturnType = optional<decltype(op(std::move(*o_)))>;
return has_value() ?
OptionalReturnType(op(std::move(*o_))) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform(UnaryOperation op) const&&{
using OptionalReturnType = optional<decltype(op(std::move(*o_)))>;
return has_value() ?
OptionalReturnType(op(std::move(*o_))) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform_optional(UnaryOperation op) & {
using OptionalReturnType = decltype(op(*o_));
return has_value() ?
op(*o_) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform_optional(UnaryOperation op) const& {
using OptionalReturnType = decltype(op(*o_));
return has_value() ?
op(*o_) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform_optional(UnaryOperation op) && {
using OptionalReturnType = decltype(op(std::move(*o_)));
return has_value() ?
op(std::move(*o_)) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr decltype(auto) transform_optional(UnaryOperation op) const&& {
using OptionalReturnType = decltype(op(std::move(*o_)));
return has_value() ?
op(std::move(*o_)) :
OptionalReturnType();
}
template <class UnaryOperation>
constexpr void call(UnaryOperation op) & {
if (has_value()) {
op(*o_);
}
}
template <class UnaryOperation>
constexpr void call(UnaryOperation op) const& {
if (has_value()) {
op(*o_);
}
}
template <class UnaryOperation>
constexpr void call(UnaryOperation op) && {
if (has_value()) {
op(std::move(*o_));
}
}
template <class UnaryOperation>
constexpr void call(UnaryOperation op) const&& {
if (has_value()) {
op(std::move(*o_));
}
}
// Forward observers
constexpr bool has_value() const noexcept { return o_.has_value(); }
constexpr const T& value() const& { return o_.value(); }
constexpr T& value() & { return o_.value(); }
constexpr T&& value() && { return std::move(o_).value(); }
constexpr const T&& value() const&& { return std::move(o_).value(); }
constexpr const T& operator*() const& { return *o_; }
constexpr T& operator*() & { return *o_; }
constexpr const T&& operator*() const&& { return *std::move(o_); }
constexpr T&& operator*() && { return *std::move(o_); }
constexpr const T* operator->() const { return o_.operator->(); }
constexpr T* operator->() { return o_.operator->(); }
private:
std::optional<T> o_;
};
}
#endif