2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
4 * This is the base for StormByte Suite libraries.
6 * StormByte original source is dual-licensed:
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)
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>).
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.
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.
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.
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>.
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
47 template<Type::SafeComponent T>
48 Optional<T>::Optional() noexcept: m_value(nullptr) {}
50 template<Type::SafeComponent T>
51 Optional<T>::Optional(std::nullopt_t) noexcept: Optional() {}
53 template<Type::SafeComponent T>
54 Optional<T>::Optional(const T& value): m_value(nullptr) {
55 m_value = Make(value);
58 template<Type::SafeComponent T>
59 Optional<T>::Optional(T&& value): m_value(nullptr) {
60 m_value = Make(std::move(value));
63 template<Type::SafeComponent T>
64 Optional<T>::Optional(const std::optional<T>& value): m_value(nullptr) {
66 m_value = Make(*value);
69 template<Type::SafeComponent T>
70 Optional<T>::Optional(std::optional<T>&& value): m_value(nullptr) {
72 m_value = Make(std::move(*value));
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);
81 template<Type::SafeComponent T>
82 Optional<T>::Optional(Optional&& other) noexcept: m_value(other.m_value) {
83 other.m_value = nullptr;
86 template<Type::SafeComponent T>
87 Optional<T>::~Optional() noexcept {
91 template<Type::SafeComponent T>
92 Optional<T>& Optional<T>::operator=(const Optional& other) {
95 T* created = other.m_value == nullptr ? nullptr : Make(*other.m_value);
101 template<Type::SafeComponent T>
102 Optional<T>& Optional<T>::operator=(Optional&& other) noexcept {
106 m_value = other.m_value;
107 other.m_value = nullptr;
111 template<Type::SafeComponent T>
112 Optional<T>& Optional<T>::operator=(const T& value) {
113 T* created = Make(value);
119 template<Type::SafeComponent T>
120 Optional<T>& Optional<T>::operator=(T&& value) {
121 T* created = Make(std::move(value));
127 template<Type::SafeComponent T>
128 Optional<T>& Optional<T>::operator=(std::nullopt_t) noexcept {
133 template<Type::SafeComponent T>
134 Optional<T>& Optional<T>::operator=(const std::optional<T>& value) {
139 return *this = *value;
142 template<Type::SafeComponent T>
143 Optional<T>& Optional<T>::operator=(std::optional<T>&& value) {
148 return *this = std::move(*value);
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;
158 template<Type::SafeComponent T>
159 T& Optional<T>::value() & {
160 if (m_value == nullptr)
161 throw BadOptionalAccess();
165 template<Type::SafeComponent T>
166 const T& Optional<T>::value() const & {
167 if (m_value == nullptr)
168 throw BadOptionalAccess();
172 template<Type::SafeComponent T>
173 T&& Optional<T>::value() && {
174 if (m_value == nullptr)
175 throw BadOptionalAccess();
176 return std::move(*m_value);
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);
186 template<Type::SafeComponent T>
187 T& Optional<T>::operator*() & {
191 template<Type::SafeComponent T>
192 const T& Optional<T>::operator*() const & {
196 template<Type::SafeComponent T>
197 T&& Optional<T>::operator*() && {
198 return std::move(*m_value);
201 template<Type::SafeComponent T>
202 const T&& Optional<T>::operator*() const && {
203 return std::move(*m_value);
206 template<Type::SafeComponent T>
207 T* Optional<T>::operator->() {
211 template<Type::SafeComponent T>
212 const T* Optional<T>::operator->() const {
216 template<Type::SafeComponent T>
217 void Optional<T>::reset() noexcept {
221 template<Type::SafeComponent T>
222 void Optional<T>::Release() noexcept {
223 if (m_value == nullptr)