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 namespace mir
23 {
24 template<typename T>
26 {
27 public:
29  {
30  value_ = value;
31  is_set_ = true;
32  return *this;
33  }
34 
35  bool is_set() const { return is_set_; }
36  T value() const { return value_; }
37 
38 private:
39  T value_;
40  bool is_set_{false};
41 };
42 
43 
44 template<typename T>
45 inline bool operator == (optional_value<T> const& lhs, optional_value<T> const& rhs)
46 {
47  return lhs.is_set() == rhs.is_set() &&
48  (lhs.is_set() ? lhs.value() == rhs.value() : true);
49 }
50 
51 template<typename T>
52 inline bool operator != (optional_value<T> const& lhs, optional_value<T> const& rhs)
53 {
54  return !(lhs == rhs);
55 }
56 
57 template<typename T>
58 inline bool operator == (optional_value<T> const& lhs, T const& rhs)
59 {
60  return lhs.is_set() && (lhs.value() == rhs);
61 }
62 
63 template<typename T>
64 inline bool operator != (optional_value<T> const& lhs, T const& rhs)
65 {
66  return !(lhs == rhs);
67 }
68 
69 template<typename T>
70 inline bool operator == (T const& lhs, optional_value<T> const& rhs)
71 {
72  return rhs == lhs;
73 }
74 
75 template<typename T>
76 inline bool operator != (T const& lhs, optional_value<T> const& rhs)
77 {
78  return rhs != lhs;
79 }
80 }
81 
82 #endif /* MIR_OPTIONAL_VALUE_H_ */
All things Mir.
Definition: buffer_stream.h:37
bool operator==(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:47
optional_value & operator=(T const &value)
Definition: optional_value.h:28
bool is_set() const
Definition: optional_value.h:35
Definition: optional_value.h:25
bool operator!=(IntWrapper< Tag, ValueType > const &lhs, IntWrapper< Tag, ValueType > const &rhs)
Definition: int_wrapper.h:53
T value() const
Definition: optional_value.h:36

Copyright © 2012,2013 Canonical Ltd.
Generated on Tue Mar 24 16:15:19 UTC 2015