Mir
optional_value.h
Go to the documentation of this file.
1/*
2 * Copyright © 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 or 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Alberto Aguirre <alberto.aguirre@canonical.com>
17 */
18
19#ifndef MIR_OPTIONAL_VALUE_H_
20#define MIR_OPTIONAL_VALUE_H_
21
22#include "mir/fatal.h"
23#include <utility>
24
25namespace mir
26{
27template<typename T>
29{
30public:
31 optional_value() = default;
32 optional_value(T const& value) : value_(value), is_set_{true} {}
33
35 {
36 value_ = value;
37 is_set_ = true;
38 return *this;
39 }
40
41 bool is_set() const { return is_set_; }
42
43 T const& value() const
44 {
45 die_if_unset();
46 return value_;
47 }
48
49 T& value()
50 {
51 die_if_unset();
52 return value_;
53 }
54
55 T&& consume()
56 {
57 die_if_unset();
58 is_set_ = false;
59 return std::move(value_);
60 }
61
62 operator bool() const
63 {
64 return is_set();
65 }
66
67private:
68 void die_if_unset() const
69 {
70 if (!is_set())
71 {
72 (*fatal_error)("Accessing value of unset optional");
73 }
74 }
75
76 T value_;
77 bool is_set_{false};
78};
79
80
81template<typename T>
82inline bool operator == (optional_value<T> const& lhs, optional_value<T> const& rhs)
83{
84 return lhs.is_set() == rhs.is_set() &&
85 (!lhs.is_set() || lhs.value() == rhs.value());
86}
87
88template<typename T>
89inline bool operator != (optional_value<T> const& lhs, optional_value<T> const& rhs)
90{
91 return !(lhs == rhs);
92}
93
94template<typename T>
95inline bool operator == (optional_value<T> const& lhs, T const& rhs)
96{
97 return lhs.is_set() && (lhs.value() == rhs);
98}
99
100template<typename T>
101inline bool operator != (optional_value<T> const& lhs, T const& rhs)
102{
103 return !(lhs == rhs);
104}
105
106template<typename T>
107inline bool operator == (T const& lhs, optional_value<T> const& rhs)
108{
109 return rhs == lhs;
110}
111
112template<typename T>
113inline bool operator != (T const& lhs, optional_value<T> const& rhs)
114{
115 return rhs != lhs;
116}
117}
118
119#endif /* MIR_OPTIONAL_VALUE_H_ */
Definition: optional_value.h:29
T && consume()
Definition: optional_value.h:55
optional_value(T const &value)
Definition: optional_value.h:32
optional_value & operator=(T const &value)
Definition: optional_value.h:34
optional_value()=default
bool is_set() const
Definition: optional_value.h:41
T & value()
Definition: optional_value.h:49
T const & value() const
Definition: optional_value.h:43
Definition: splash_session.h:24
constexpr bool operator!=(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:53
constexpr bool operator==(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:47

Copyright © 2012-2022 Canonical Ltd.
Generated on Sun Oct 9 06:13:38 UTC 2022
This documentation is licensed under the GPL version 2 or 3.