astrotypes  0.0
Optional.h
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 #ifndef PSS_ASTROTYPES_UTILS_OPTIONAL_H
25 #define PSS_ASTROTYPES_UTILS_OPTIONAL_H
26 
27 
28 namespace pss {
29 namespace astrotypes {
30 namespace utils {
31 
37 template<typename T>
38 class Optional
39 {
40  public:
41  Optional();
42  Optional(T const&);
43  Optional(T&&);
44  ~Optional();
45 
46  inline operator T&() { return _val; }
47 
51  bool is_set() const;
52 
57  T const& operator*() const;
58  T& operator*();
59 
66  bool operator==(Optional const&) const;
67  bool operator!=(Optional const&) const;
68  bool operator==(T const&) const;
69  bool operator!=(T const&) const;
70 
74  void reset();
75 
76  private:
77  bool _is_set;
78  T _val;
79 };
80 
86 template<>
87 class Optional<bool>
88 {
89  public:
90  Optional();
91  Optional(bool);
92  ~Optional();
93 
94  inline operator bool&() { return _val; }
95  inline operator bool const&() const { return _val; }
96 
100  bool is_set() const;
101 
109  bool operator==(Optional const&) const;
110  bool operator!=(Optional const&) const;
111  bool operator==(bool) const;
112  bool operator!=(bool) const;
113 
117  void reset();
118 
119  private:
120  bool _is_set;
121  bool _val;
122 };
123 
124 } // namespace utils
125 } // namespace astrotypes
126 } // namespace pss
127 #include "detail/Optional.cpp"
128 
129 #endif // PSS_ASTROTYPES_UTILS_OPTIONAL_H
void reset()
return to unset state
Definition: Optional.cpp:101
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