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 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  T value() const
43  {
44  die_if_unset();
45  return value_;
46  }
47 
48  T&& consume()
49  {
50  die_if_unset();
51  is_set_ = false;
52  return std::move(value_);
53  }
54 
55 private:
56  void die_if_unset() const
57  {
58  if (!is_set())
59  {
60  (*fatal_error)("Accessing value of unset optional");
61  }
62  }
63 
64  T value_;
65  bool is_set_{false};
66 };
67 
68 
69 template<typename T>
70 inline bool operator == (optional_value<T> const& lhs, optional_value<T> const& rhs)
71 {
72  return lhs.is_set() == rhs.is_set() &&
73  (!lhs.is_set() || lhs.value() == rhs.value());
74 }
75 
76 template<typename T>
77 inline bool operator != (optional_value<T> const& lhs, optional_value<T> const& rhs)
78 {
79  return !(lhs == rhs);
80 }
81 
82 template<typename T>
83 inline bool operator == (optional_value<T> const& lhs, T const& rhs)
84 {
85  return lhs.is_set() && (lhs.value() == rhs);
86 }
87 
88 template<typename T>
89 inline bool operator != (optional_value<T> const& lhs, T const& rhs)
90 {
91  return !(lhs == rhs);
92 }
93 
94 template<typename T>
95 inline bool operator == (T const& lhs, optional_value<T> const& rhs)
96 {
97  return rhs == lhs;
98 }
99 
100 template<typename T>
101 inline bool operator != (T const& lhs, optional_value<T> const& rhs)
102 {
103  return rhs != lhs;
104 }
105 }
106 
107 #endif /* MIR_OPTIONAL_VALUE_H_ */
All things Mir.
Definition: atomic_callback.h:25
constexpr bool operator==(Flags< Enum > flags, Enum e) noexcept
Definition: flags.h:120
optional_value & operator=(T const &value)
Definition: optional_value.h:34
optional_value()=default
optional_value(T const &value)
Definition: optional_value.h:32
bool is_set() const
Definition: optional_value.h:41
Definition: optional_value.h:28
bool operator!=(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:53
T value() const
Definition: optional_value.h:42
T && consume()
Definition: optional_value.h:48

Copyright © 2012-2015 Canonical Ltd.
Generated on Wed Mar 30 00:29:56 UTC 2016