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 
25 namespace mir
26 {
27 template<typename T>
29 {
30 public:
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 private:
63  void die_if_unset() const
64  {
65  if (!is_set())
66  {
67  (*fatal_error)("Accessing value of unset optional");
68  }
69  }
70 
71  T value_;
72  bool is_set_{false};
73 };
74 
75 
76 template<typename T>
77 inline bool operator == (optional_value<T> const& lhs, optional_value<T> const& rhs)
78 {
79  return lhs.is_set() == rhs.is_set() &&
80  (!lhs.is_set() || lhs.value() == rhs.value());
81 }
82 
83 template<typename T>
84 inline bool operator != (optional_value<T> const& lhs, optional_value<T> const& rhs)
85 {
86  return !(lhs == rhs);
87 }
88 
89 template<typename T>
90 inline bool operator == (optional_value<T> const& lhs, T const& rhs)
91 {
92  return lhs.is_set() && (lhs.value() == rhs);
93 }
94 
95 template<typename T>
96 inline bool operator != (optional_value<T> const& lhs, T const& rhs)
97 {
98  return !(lhs == rhs);
99 }
100 
101 template<typename T>
102 inline bool operator == (T const& lhs, optional_value<T> const& rhs)
103 {
104  return rhs == lhs;
105 }
106 
107 template<typename T>
108 inline bool operator != (T const& lhs, optional_value<T> const& rhs)
109 {
110  return rhs != lhs;
111 }
112 }
113 
114 #endif /* MIR_OPTIONAL_VALUE_H_ */
AutoUnblockThread is a helper thread class that can gracefully shutdown at destruction time...
Definition: sw_splash.h:26
bool is_set() const
Definition: optional_value.h:41
constexpr bool operator==(Flags< Enum > flags, Enum e) noexcept
Definition: flags.h:125
optional_value & operator=(T const &value)
Definition: optional_value.h:34
optional_value()=default
optional_value(T const &value)
Definition: optional_value.h:32
Definition: optional_value.h:28
T const & value() const
Definition: optional_value.h:43
T & value()
Definition: optional_value.h:49
T && consume()
Definition: optional_value.h:55
constexpr bool operator!=(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:53

Copyright © 2012-2017 Canonical Ltd.
Generated on Wed Oct 11 15:14:10 UTC 2017