AuroraRuntime/Include/auROXTL/auOptionalEx.hpp
2022-03-23 16:32:21 +00:00

79 lines
1.3 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
{
const 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) ? **this : static_cast<T>(AuForward<U>(defaultValue));
}
template<class U>
constexpr T ValueOr(U &&defaultValue) &&
{
return bool(*this) ? AuMove(**this) : static_cast<T>(AuForward<U>(defaultValue));
}
bool HasValue() const
{
return this->hasValue;
}
operator bool() const
{
return this->hasValue;
}
T &value()
{
return type;
}
const T &value() const
{
return type;
}
bool has_value() const
{
return this->hasValue;
}
AuUInt HashCode() const
{
return AuHashCode(type);
}
};