AuROXTL/Include/auROXTL/auResult.hpp

72 lines
1.0 KiB
C++

/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auResult.hpp
Date: 2022-3-23
Author: Reece
***/
#pragma once
#include "auCopyMoveUtils.hpp"
template <class T>
struct AuResult
{
bool success;
T type;
AuResult() : success(false)
{}
AuResult(bool success) : success(success)
{}
AuResult(T &&value) : type(value), success(true)
{}
// Forced return value optimization.
T &GetResult()
{
return type;
}
constexpr const T *operator->() const
{
return &type;
}
constexpr T *operator->()
{
return &type;
}
const T &GetResult() const
{
return type;
}
void Complete()
{
this->success = true;
}
operator bool() const
{
return this->success;
}
bool has_value() const
{
return this->success;
}
const T &value() const
{
return type;
}
T &value()
{
return type;
}
};