AuroraRuntime/Include/auROXTL/auOptionalEx.hpp
2022-03-25 12:00:44 +00:00

91 lines
1.5 KiB
C++

/***
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<class T>
struct AuOptionalEx
{
bool hasValue;
T type;
AuOptionalEx() : hasValue({})
{}
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<class U>
constexpr T ValueOr(U &&defaultValue) const &
{
return bool(*this) ? value() : static_cast<T>(AuForward<U>(defaultValue));
}
template<class U>
constexpr T ValueOr(U &&defaultValue) &&
{
return bool(*this) ? AuMove(value()) : static_cast<T>(AuForward<U>(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);
}
};