1#ifndef ORIGINAL_OPTIONAL_H
2#define ORIGINAL_OPTIONAL_H
44 template<
typename TYPE>
62 storage(
const storage& other)
noexcept =
default;
65 storage& operator=(
const storage& other)
noexcept =
default;
78 void destroy()
noexcept;
93 template<
typename... Args>
158 const TYPE*
get()
const;
170 void reset()
noexcept;
178 template<
typename... Args>
186 void set(
const TYPE& t);
192 explicit operator bool()
const;
198 [[nodiscard]]
bool hasValue()
const;
216 bool has_value_ =
false;
249 void reset() noexcept;
255 [[nodiscard]]
bool hasValue() const;
261 explicit operator
bool() const;
280template<typename TYPE>
283 new(&this->none_)
none{};
286template<
typename TYPE>
289template<
typename TYPE>
291 if (this->non_none_type_){
292 this->val_.type_.~TYPE();
293 this->non_none_type_ =
false;
295 this->val_.none_.
~none();
296 this->non_none_type_ =
true;
300template<
typename TYPE>
302 : non_none_type_(false), val_() {}
304template<
typename TYPE>
305template<
typename... Args>
307 : non_none_type_(true), val_() {
308 new (&this->val_.type_) TYPE{ std::forward<Args>(args)... };
311template<
typename TYPE>
313 this->non_none_type_ = other.non_none_type_;
314 if (other.non_none_type_) {
315 new (&val_.type_) TYPE{ other.val_.type_ };
317 new (&val_.none_)
none{};
321template<
typename TYPE>
328 this->non_none_type_ = other.non_none_type_;
329 if (other.non_none_type_) {
330 new (&val_.type_) TYPE{ other.val_.type_ };
332 new (&val_.none_)
none{};
337template<
typename TYPE>
339 this->non_none_type_ = other.non_none_type_;
340 if (this->non_none_type_){
341 new (&val_.type_) TYPE{ std::move(other.val_.type_) };
343 new (&val_.none_)
none{};
347template<
typename TYPE>
354 this->non_none_type_ = other.non_none_type_;
355 if (this->non_none_type_){
356 new (&val_.type_) TYPE{ std::move(other.val_.type_) };
358 new (&val_.none_)
none{};
363template<
typename TYPE>
366 if (!this->non_none_type_)
367 throw valueError(
"Dereferencing a original::none value");
369 return this->val_.type_;
372template<
typename TYPE>
375 if (!this->non_none_type_)
376 throw valueError(
"Dereferencing a original::none value");
378 return this->val_.type_;
381template<
typename TYPE>
384 if (!this->non_none_type_)
385 throw valueError(
"Accessing member of a original::none value");
387 return &this->val_.type_;
390template<
typename TYPE>
393 if (!this->non_none_type_)
394 throw valueError(
"Accessing member of a original::none value");
396 return &this->val_.type_;
399template<
typename TYPE>
401 return this->non_none_type_ ? &this->val_.type_ :
nullptr;
404template<
typename TYPE>
406 return this->non_none_type_ ? &this->val_.type_ :
nullptr;
409template<
typename TYPE>
414template<
typename TYPE>
415template<
typename... Args>
418 this->non_none_type_ =
true;
419 new (&val_.type_) TYPE{ std::forward<Args>(args)... };
422template<
typename TYPE>
425 this->non_none_type_ =
true;
426 new (&val_.type_) TYPE{ t };
429template<
typename TYPE>
431 return this->non_none_type_;
434template <
typename TYPE>
437 return this->
operator bool();
440template<
typename TYPE>
453 this->has_value_ =
true;
458 this->has_value_ =
false;
463 return this->has_value_;
468 return this->has_value_;
alternative()
Constructs an empty alternative<void>
A type-safe container that may or may not contain a value.
Definition optional.h:45
void set(const TYPE &t)
Sets value by copy.
Definition optional.h:423
alternative()
Constructs an empty alternative.
Definition optional.h:301
void emplace(Args &&... args)
Constructs value in-place.
Definition optional.h:416
bool hasValue() const
Checks if contains a value.
Definition optional.h:435
void reset() noexcept
Resets to empty state.
Definition optional.h:410
alternative & operator=(const alternative &other)
Copy assignment.
Definition optional.h:323
const TYPE * get() const
Gets const pointer to value.
Definition optional.h:400
~alternative()
Destructor.
Definition optional.h:441
const TYPE & operator*() const
Const value access.
Definition optional.h:365
const TYPE * operator->() const
Const member access.
Definition optional.h:383
A placeholder type representing the absence of a value.
Definition types.h:32
constexpr ~none()=default
Default destructor (constexpr)
Exception for invalid parameter values.
Definition error.h:150
Custom exception classes and callback validation utilities.
Main namespace for the project Original.
Definition algorithms.h:21
Core type system foundations and concept definitions.