astrotypes  0.0
Optional.cpp
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018 PulsarSearchSoft
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 namespace pss {
26 namespace astrotypes {
27 namespace utils {
28 
29 
30 template<typename T>
32  : _is_set(false)
33 {
34 }
35 
36 template<typename T>
37 inline Optional<T>::Optional(T const& t)
38  : _is_set(true)
39  , _val(t)
40 {
41 }
42 
43 template<typename T>
44 inline Optional<T>::Optional(T&& t)
45  : _is_set(true)
46  , _val(std::move(t))
47 {
48 }
49 
50 template<typename T>
52 {
53 }
54 
55 template<typename T>
56 inline bool Optional<T>::is_set() const
57 {
58  return _is_set;
59 }
60 
61 template<typename T>
62 inline bool Optional<T>::operator==(Optional const& opt) const
63 {
64  if(!_is_set) return !opt._is_set;
65  if(!opt._is_set) return false;
66  return _val == opt._val;
67 }
68 
69 template<typename T>
70 inline bool Optional<T>::operator!=(Optional const& opt) const
71 {
72  return !(*this == opt);
73 }
74 
75 template<typename T>
76 inline bool Optional<T>::operator==(T const& opt) const
77 {
78  if(!_is_set) return false;
79  return _val == opt;
80 }
81 
82 template<typename T>
83 inline bool Optional<T>::operator!=(T const& opt) const
84 {
85  return !(*this == opt);
86 }
87 
88 template<typename T>
89 T const& Optional<T>::operator*() const
90 {
91  return _val;
92 }
93 
94 template<typename T>
96 {
97  return _val;
98 }
99 
100 template<typename T>
101 inline void Optional<T>::reset()
102 {
103  _is_set = false;
104 }
105 
106 // ------- bool specialisation -------------
108  : _is_set(false)
109 {
110 }
111 
113  : _is_set(true)
114  , _val(t)
115 {
116 }
117 
119 {
120 }
121 
122 inline bool Optional<bool>::is_set() const
123 {
124  return _is_set;
125 }
126 
127 inline bool Optional<bool>::operator==(Optional const& opt) const
128 {
129  if(!_is_set) return !opt._is_set;
130  if(!opt._is_set) return false;
131  return _val == opt._val;
132 }
133 
134 inline bool Optional<bool>::operator!=(Optional const& opt) const
135 {
136  return !(*this == opt);
137 }
138 
139 inline bool Optional<bool>::operator==(bool opt) const
140 {
141  if(!_is_set) return false;
142  return _val == opt;
143 }
144 
145 inline bool Optional<bool>::operator!=(bool opt) const
146 {
147  return !(*this == opt);
148 }
149 
151 {
152  _is_set = false;
153 }
154 
155 } // namespace utils
156 } // namespace astrotypes
157 } // namespace pss
void reset()
return to unset state
Definition: Optional.cpp:101
STL namespace.
T const & operator*() const
return the value
Definition: Optional.cpp:89
A simplified, faster, but less secure and less versatile version of boost::optional.
Definition: Optional.h:38
bool is_set() const
if the value has been explicitly set then return true
Definition: Optional.cpp:56
bool operator!=(Optional const &) const
Definition: Optional.cpp:70
bool operator==(Optional const &) const
compare two Optional wrapped parameters
Definition: Optional.cpp:62