AnCH Framework 0.1
Another C++ Hack Framework
 
Loading...
Searching...
No Matches
optional.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++11 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <http://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <optional>
23#include <functional>
24
25namespace anch {
26
33 template<typename T>
34 void applyIfValue(std::optional<T>& value, std::function<void(const T&)> func);
35
43 template<typename T>
44 void applyIfValueOrElse(std::optional<T>& value, std::function<void(const T&)> func, std::function<void()> fallback);
45
55 template<typename T>
56 class Optional: std::optional<T> {
57 // Constructors +
58 public:
62 constexpr Optional() noexcept;
63
69 constexpr Optional(std::nullopt_t null) noexcept;
70
76 constexpr Optional(const std::optional<T>& other);
77
83 constexpr Optional(std::optional<T>&& other) noexcept;
84
90 template<typename U>
91 Optional(const std::optional<U>& other);
92
98 template<typename U>
99 Optional(std::optional<U>&& other);
100
108 template<typename... Args>
109 constexpr explicit Optional(std::in_place_t inplace, Args&&... args);
110
119 template<typename U, typename... Args>
120 constexpr explicit Optional(std::in_place_t inplace, std::initializer_list<U> ilist, Args&&... args);
121
127 template<typename U = T>
128 constexpr Optional(U&& value);
129 // Constructors -
130
131
132 // Destructor +
133 public:
137 virtual ~Optional();
138 // Destructor -
139
140
141 // Methods +
142 public:
148 void ifValue(std::function<void(const T&)> func);
149
156 void ifValueOrElse(std::function<void(const T&)> func, std::function<void()> fallback);
157
166 template<typename U>
167 Optional<U> map(std::function<U(const T&)> mapper) const;
168 // Methods -
169
170 };
171
172 template<typename T>
173 inline void
174 applyIfValue(std::optional<T>& value, std::function<void(const T&)> func) {
175 if(value) {
176 func(*value);
177 }
178 }
179
180 template<typename T>
181 inline void
182 applyIfValueOrElse(std::optional<T>& value, std::function<void(const T&)> func, std::function<void()> fallback) {
183 if(value) {
184 func(*value);
185 } else {
186 fallback();
187 }
188 }
189
190 template<typename T>
191 constexpr Optional<T>::Optional() noexcept: std::optional<T>() {
192 // Nothing to do
193 }
194
195 template<typename T>
196 constexpr Optional<T>::Optional(std::nullopt_t null) noexcept: std::optional<T>(null) {
197 // Nothing to do
198 }
199
200 template<typename T>
201 constexpr Optional<T>::Optional(const std::optional<T>& other): std::optional<T>(other) {
202 // Nothing to do
203 }
204
205 template<typename T>
206 constexpr Optional<T>::Optional(std::optional<T>&& other) noexcept: std::optional<T>(other) {
207 // Nothing to do
208 }
209
210 template<typename T>
211 template<typename U>
212 Optional<T>::Optional(const std::optional<U>& other): std::optional<T>(other) {
213 // Nothing to do
214 }
215
216 template<typename T>
217 template<typename U>
218 Optional<T>::Optional(std::optional<U>&& other): std::optional<T>(other) {
219 // Nothing to do
220 }
221
222 template<typename T>
223 template<typename... Args>
224 constexpr Optional<T>::Optional(std::in_place_t inplace, Args&&... args): std::optional<T>(inplace, args...) {
225 // Nothing to do
226 }
227
228 template<typename T>
229 template<typename U, typename... Args>
230 constexpr Optional<T>::Optional(std::in_place_t inplace, std::initializer_list<U> ilist, Args&&... args)
231 : std::optional<T>(inplace, ilist, args...) {
232 // Nothing to do
233 }
234
235 template<typename T>
236 template<typename U>
237 constexpr Optional<T>::Optional(U&& value): std::optional<T>(value) {
238 // Nothing to do
239 }
240
241 template<typename T>
243 // Nothing to do
244 }
245
246 template<typename T>
247 inline void
248 Optional<T>::ifValue(std::function<void(const T&)> func) {
249 if(this->has_value()) {
250 func(this->value());
251 }
252 }
253
254 template<typename T>
255 inline void
256 Optional<T>::ifValueOrElse(std::function<void(const T&)> func, std::function<void()> fallback) {
257 if(this->has_value()) {
258 func(this->value());
259 } else {
260 fallback();
261 }
262 }
263
264 template<typename T>
265 template<typename U>
266 inline Optional<U>
267 Optional<T>::map(std::function<U(const T&)> mapper) const {
268 Optional<U> val;
269 if(this->has_value()) {
270 val = mapper(this->value());
271 }
272 return val;
273 }
274
275}
std::optional usefull methods.
Definition optional.hpp:56
constexpr Optional() noexcept
Definition optional.hpp:191
virtual ~Optional()
Definition optional.hpp:242
void ifValue(std::function< void(const T &)> func)
Definition optional.hpp:248
void ifValueOrElse(std::function< void(const T &)> func, std::function< void()> fallback)
Definition optional.hpp:256
Optional< U > map(std::function< U(const T &)> mapper) const
Definition optional.hpp:267
AnCH framework base namespace.
Definition app.hpp:28
void applyIfValue(std::optional< T > &value, std::function< void(const T &)> func)
Definition optional.hpp:174
void applyIfValueOrElse(std::optional< T > &value, std::function< void(const T &)> func, std::function< void()> fallback)
Definition optional.hpp:182