StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
optional.txx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <new>
43#include <utility>
44
45namespace StormByte {
46 namespace Safe {
47 template<Type::SafeComponent T>
48 Optional<T>::Optional() noexcept: m_value(nullptr) {}
49
50 template<Type::SafeComponent T>
51 Optional<T>::Optional(std::nullopt_t) noexcept: Optional() {}
52
53 template<Type::SafeComponent T>
54 Optional<T>::Optional(const T& value): m_value(nullptr) {
55 m_value = Make(value);
56 }
57
58 template<Type::SafeComponent T>
59 Optional<T>::Optional(T&& value): m_value(nullptr) {
60 m_value = Make(std::move(value));
61 }
62
63 template<Type::SafeComponent T>
64 Optional<T>::Optional(const std::optional<T>& value): m_value(nullptr) {
65 if (value)
66 m_value = Make(*value);
67 }
68
69 template<Type::SafeComponent T>
70 Optional<T>::Optional(std::optional<T>&& value): m_value(nullptr) {
71 if (value)
72 m_value = Make(std::move(*value));
73 }
74
75 template<Type::SafeComponent T>
76 Optional<T>::Optional(const Optional& other): m_value(nullptr) {
77 if (other.m_value != nullptr)
78 m_value = Make(*other.m_value);
79 }
80
81 template<Type::SafeComponent T>
82 Optional<T>::Optional(Optional&& other) noexcept: m_value(other.m_value) {
83 other.m_value = nullptr;
84 }
85
86 template<Type::SafeComponent T>
87 Optional<T>::~Optional() noexcept {
88 Release();
89 }
90
91 template<Type::SafeComponent T>
92 Optional<T>& Optional<T>::operator=(const Optional& other) {
93 if (this == &other)
94 return *this;
95 T* created = other.m_value == nullptr ? nullptr : Make(*other.m_value);
96 Release();
97 m_value = created;
98 return *this;
99 }
100
101 template<Type::SafeComponent T>
102 Optional<T>& Optional<T>::operator=(Optional&& other) noexcept {
103 if (this == &other)
104 return *this;
105 Release();
106 m_value = other.m_value;
107 other.m_value = nullptr;
108 return *this;
109 }
110
111 template<Type::SafeComponent T>
112 Optional<T>& Optional<T>::operator=(const T& value) {
113 T* created = Make(value);
114 Release();
115 m_value = created;
116 return *this;
117 }
118
119 template<Type::SafeComponent T>
120 Optional<T>& Optional<T>::operator=(T&& value) {
121 T* created = Make(std::move(value));
122 Release();
123 m_value = created;
124 return *this;
125 }
126
127 template<Type::SafeComponent T>
128 Optional<T>& Optional<T>::operator=(std::nullopt_t) noexcept {
129 reset();
130 return *this;
131 }
132
133 template<Type::SafeComponent T>
134 Optional<T>& Optional<T>::operator=(const std::optional<T>& value) {
135 if (!value) {
136 reset();
137 return *this;
138 }
139 return *this = *value;
140 }
141
142 template<Type::SafeComponent T>
143 Optional<T>& Optional<T>::operator=(std::optional<T>&& value) {
144 if (!value) {
145 reset();
146 return *this;
147 }
148 return *this = std::move(*value);
149 }
150
151 template<Type::SafeComponent T>
152 void Optional<T>::swap(Optional& other) noexcept {
153 T* temporary = m_value;
154 m_value = other.m_value;
155 other.m_value = temporary;
156 }
157
158 template<Type::SafeComponent T>
159 T& Optional<T>::value() & {
160 if (m_value == nullptr)
161 throw BadOptionalAccess();
162 return *m_value;
163 }
164
165 template<Type::SafeComponent T>
166 const T& Optional<T>::value() const & {
167 if (m_value == nullptr)
168 throw BadOptionalAccess();
169 return *m_value;
170 }
171
172 template<Type::SafeComponent T>
173 T&& Optional<T>::value() && {
174 if (m_value == nullptr)
175 throw BadOptionalAccess();
176 return std::move(*m_value);
177 }
178
179 template<Type::SafeComponent T>
180 const T&& Optional<T>::value() const && {
181 if (m_value == nullptr)
182 throw BadOptionalAccess();
183 return std::move(*m_value);
184 }
185
186 template<Type::SafeComponent T>
187 T& Optional<T>::operator*() & {
188 return *m_value;
189 }
190
191 template<Type::SafeComponent T>
192 const T& Optional<T>::operator*() const & {
193 return *m_value;
194 }
195
196 template<Type::SafeComponent T>
197 T&& Optional<T>::operator*() && {
198 return std::move(*m_value);
199 }
200
201 template<Type::SafeComponent T>
202 const T&& Optional<T>::operator*() const && {
203 return std::move(*m_value);
204 }
205
206 template<Type::SafeComponent T>
207 T* Optional<T>::operator->() {
208 return m_value;
209 }
210
211 template<Type::SafeComponent T>
212 const T* Optional<T>::operator->() const {
213 return m_value;
214 }
215
216 template<Type::SafeComponent T>
217 void Optional<T>::reset() noexcept {
218 Release();
219 }
220
221 template<Type::SafeComponent T>
222 void Optional<T>::Release() noexcept {
223 if (m_value == nullptr)
224 return;
225 m_value->~T();
226 Heap::Free(m_value);
227 m_value = nullptr;
228 }
229 }
230}