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 
24 namespace mir
25 {
26 template<typename T>
28 {
29 public:
30  optional_value() = default;
31  optional_value(T const& value) : value_{value}, is_set_{true} {}
32 
34  {
35  value_ = value;
36  is_set_ = true;
37  return *this;
38  }
39 
40  bool is_set() const { return is_set_; }
41  T value() const
42  {
43  if (!is_set())
44  {
45  (*fatal_error)("Accessing value of unset optional");
46  }
47  return value_;
48  }
49 
50 private:
51  T value_;
52  bool is_set_{false};
53 };
54 
55 
56 template<typename T>
57 inline bool operator == (optional_value<T> const& lhs, optional_value<T> const& rhs)
58 {
59  return lhs.is_set() == rhs.is_set() &&
60  (!lhs.is_set() || lhs.value() == rhs.value());
61 }
62 
63 template<typename T>
64 inline bool operator != (optional_value<T> const& lhs, optional_value<T> const& rhs)
65 {
66  return !(lhs == rhs);
67 }
68 
69 template<typename T>
70 inline bool operator == (optional_value<T> const& lhs, T const& rhs)
71 {
72  return lhs.is_set() && (lhs.value() == rhs);
73 }
74 
75 template<typename T>
76 inline bool operator != (optional_value<T> const& lhs, T const& rhs)
77 {
78  return !(lhs == rhs);
79 }
80 
81 template<typename T>
82 inline bool operator == (T const& lhs, optional_value<T> const& rhs)
83 {
84  return rhs == lhs;
85 }
86 
87 template<typename T>
88 inline bool operator != (T const& lhs, optional_value<T> const& rhs)
89 {
90  return rhs != lhs;
91 }
92 }
93 
94 #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:33
optional_value()=default
optional_value(T const &value)
Definition: optional_value.h:31
bool is_set() const
Definition: optional_value.h:40
Definition: optional_value.h:27
bool operator!=(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:53
T value() const
Definition: optional_value.h:41

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Oct 8 16:20:16 UTC 2015