/*** Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: auOptionalEx.hpp Date: 2022-3-23 Author: Reece ***/ #pragma once #include "auCopyMoveUtils.hpp" #include "auHashUtils.hpp" template struct AuOptionalEx { bool hasValue; T type; AuOptionalEx() : hasValue(false) {} AuOptionalEx(T &value) : type(value), hasValue(true) {} AuOptionalEx(T &&value) : type(value), hasValue(true) {} T &Value() { return type; } const T &Value() const { return type; } template constexpr T ValueOr(U &&defaultValue) const & { return bool(*this) ? value() : static_cast(AuForward(defaultValue)); } template constexpr T ValueOr(U &&defaultValue) && { return bool(*this) ? AuMove(value()) : static_cast(AuForward(defaultValue)); } bool HasValue() const { return this->hasValue; } operator bool() const { return this->hasValue; } T &value() { return this->type; } const T &value() const { return this->type; } bool has_value() const { return this->hasValue; } AuUInt HashCode() const { return AuHashCode(type); } void Reset() { this->type = {}; this->hasValue = {}; } void Swap(AuOptionalEx &&ex) { AuSwap(this->type, ex.type); AuSwap(this->hasValue, ex.hasValue); } };