gucoordinates
OptionalCoordinate.hpp
Go to the documentation of this file.
1/*
2 * OptionalCoordinate.hpp
3 * gucoordinates
4 *
5 * Created by Callum McColl on 21/02/2021.
6 * Copyright © 2021 Callum McColl. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Callum McColl.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58
59#ifndef GUCOORDINATES_OPTIONALCOORDINATE_HPP
60#define GUCOORDINATES_OPTIONALCOORDINATE_HPP
61
62#if __cplusplus >= 201703L
63#include <optional>
64#endif
65
66namespace GU {
67
68 template <class Raw, class Wrapped>
69 struct OptionalCoordinate: public Raw {
70
71 OptionalCoordinate() NOEXCEPT {}
72
73 OptionalCoordinate(const Wrapped t_value) NOEXCEPT
74 {
75 set_has_value(true);
76 set_value(t_value);
77 }
78
79 OptionalCoordinate(const bool t_has_value, const Wrapped t_value) NOEXCEPT
80 {
81 set_has_value(t_has_value);
82 set_value(t_value);
83 }
84
86 {
87 set_has_value(other.has_value());
88 set_value(other.value());
89 }
90
91 OptionalCoordinate(const Raw& other) NOEXCEPT
92 {
93 set_has_value(other.has_value);
94 set_value(other.value);
95 }
96
97#if __cplusplus >= 201703L
98 OptionalCoordinate(const std::optional<Wrapped>& other) NOEXCEPT
99 {
100 set_has_value(other.has_value());
101 if (!other.has_value())
102 {
103 set_value(Wrapped());
104 return;
105 }
106 set_value(other.value());
107 }
108#endif
109
110#if __cplusplus >= 201103L
111#pragma clang diagnostic push
112#pragma clang diagnostic ignored "-Wc++98-compat"
114 {
115 set_has_value(other.has_value());
116 other.set_has_value(0.0f);
117 set_value(other.value());
118 other.set_value(Wrapped());
119 }
120#pragma clang diagnostic pop
121#endif
122
124
126 {
127 if (&other == this)
128 {
129 return *this;
130 }
131 set_has_value(other.has_value());
132 set_value(other.value());
133 return *this;
134 }
135
136 OptionalCoordinate<Raw, Wrapped>& operator=(const Raw& other) NOEXCEPT
137 {
138 if (&other == this)
139 {
140 return *this;
141 }
142 set_has_value(other.has_value);
143 set_value(other.value);
144 return *this;
145 }
146
147#if __cplusplus >= 201703L
148 OptionalCoordinate<Raw, Wrapped>& operator=(const std::optional<Wrapped>& other) NOEXCEPT
149 {
150 set_has_value(other.has_value());
151 if (!other.has_value())
152 {
153 set_value(Wrapped());
154 return *this;
155 }
156 set_value(other.value());
157 return *this;
158 }
159#endif
160
161#if __cplusplus >= 201103L
162#pragma clang diagnostic push
163#pragma clang diagnostic ignored "-Wc++98-compat"
165 {
166 if (&other == this)
167 {
168 return *this;
169 }
170 set_has_value(other.has_value());
171 other.set_has_value(false);
172 set_value(other.value());
173 other.set_value(Wrapped());
174 return *this;
175 }
176#pragma clang diagnostic pop
177#endif
178
179 bool has_value() const NOEXCEPT
180 {
181 return Raw::has_value;
182 }
183
184 void set_has_value(const bool newValue) NOEXCEPT
185 {
186 Raw::has_value = newValue;
187 }
188
189 Wrapped value() const NOEXCEPT
190 {
191 return Raw::value;
192 }
193
194 void set_value(const Wrapped newValue) NOEXCEPT
195 {
196 Raw::value = newValue;
197 }
198
199#if __cplusplus >= 201703L
200 std::optional<Wrapped> asOptional() const NOEXCEPT
201 {
202 if (has_value())
203 return std::optional<Wrapped>(value());
204 return std::nullopt;
205 }
206#endif
207
208 };
209
210}
211
212#endif /* GUCOORDINATES_OPTIONALCOORDINATE_HPP */
Definition: Camera.hpp:66
OptionalCoordinate< Raw, Wrapped > & operator=(const OptionalCoordinate< Raw, Wrapped > &other) NOEXCEPT
void set_value(const Wrapped newValue) NOEXCEPT
OptionalCoordinate(const Wrapped t_value) NOEXCEPT
OptionalCoordinate(const Raw &other) NOEXCEPT
OptionalCoordinate(const std::optional< Wrapped > &other) NOEXCEPT
OptionalCoordinate< Raw, Wrapped > & operator=(const Raw &other) NOEXCEPT
OptionalCoordinate< Raw, Wrapped > & operator=(OptionalCoordinate< Raw, Wrapped > &&other) NOEXCEPT
bool has_value() const NOEXCEPT
OptionalCoordinate(const OptionalCoordinate< Raw, Wrapped > &other) NOEXCEPT
Wrapped value() const NOEXCEPT
OptionalCoordinate(const bool t_has_value, const Wrapped t_value) NOEXCEPT
void set_has_value(const bool newValue) NOEXCEPT
OptionalCoordinate(OptionalCoordinate< Raw, Wrapped > &&other) NOEXCEPT
OptionalCoordinate< Raw, Wrapped > & operator=(const std::optional< Wrapped > &other) NOEXCEPT
std::optional< Wrapped > asOptional() const NOEXCEPT