-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptional.hh
67 lines (49 loc) · 1.36 KB
/
optional.hh
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
// Copyright (c) 2022 Mikael Simonsson <https://mikaelsimonsson.com>.
// SPDX-License-Identifier: BSL-1.0
// # Optional (`result` without error code)
#pragma once
#include "snn-core/result.hh"
#include "snn-core/generic/error.hh"
namespace snn
{
// ## Constants
// ### nullopt
inline constexpr auto nullopt = make_error_code(generic::error::no_value);
// ## Classes
// ### optional_error_storage
class optional_error_storage final
{
public:
constexpr explicit optional_error_storage() noexcept
: has_error_{false}
{
}
constexpr explicit optional_error_storage(error_code) noexcept
: has_error_{true}
{
}
constexpr explicit operator bool() const noexcept
{
return has_error_;
}
constexpr operator error_code() const noexcept
{
snn_should(has_error_);
return nullopt;
}
[[nodiscard]] static constexpr error_code default_error_code() noexcept
{
return nullopt;
}
[[nodiscard]] static constexpr bool has_boolean_state() noexcept
{
return true;
}
private:
bool has_error_;
};
// ## Aliases
// ### optional
template <typename T>
using optional = result<T, optional_error_storage>;
}