-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptional.hpp
More file actions
42 lines (33 loc) · 735 Bytes
/
Copy pathOptional.hpp
File metadata and controls
42 lines (33 loc) · 735 Bytes
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
#pragma once
namespace mik {
/**
* @brief Super basic implementation of std::optional, limited by default Arduino toolchain
* settings
*/
template <class T>
class Optional {
private:
T value_;
bool has_value_;
public:
constexpr Optional() noexcept : has_value_(false) {}
explicit Optional(const T& value) {
value_ = value;
has_value_ = true;
}
explicit Optional(T&& value) {
value_ = value;
has_value_ = true;
}
const T& value() const { return value_; }
T& value() { return value_; }
constexpr bool has_value() const noexcept { return has_value_; }
T value_or(T other) const {
if (has_value_) {
return value_;
} else {
return other;
}
}
};
} // namespace mik