SHA256
39 lines
1.1 KiB
C++
39 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <optional>
|
|
#include <functional>
|
|
|
|
namespace flog
|
|
{
|
|
template <typename T>
|
|
class PushResult
|
|
{
|
|
public:
|
|
NODISCARD static PushResult success() noexcept { return PushResult(); }
|
|
NODISCARD static PushResult failWithValue(T value) noexcept
|
|
{
|
|
return PushResult(std::move(std::optional<T>(std::move(value))));
|
|
}
|
|
|
|
NODISCARD bool isSuccessful() const noexcept { return !m_value.has_value(); }
|
|
NODISCARD bool isFail() const noexcept { return m_value.has_value(); }
|
|
|
|
void getValueIfFail(const std::function<void(const T& value)> func) const
|
|
{
|
|
if (isFail()) {
|
|
func(m_value.value());
|
|
}
|
|
}
|
|
|
|
NODISCARD T getValue() const { return m_value.value(); }
|
|
|
|
PushResult(const PushResult &other) noexcept { m_value = other.m_value; }
|
|
|
|
private:
|
|
PushResult() noexcept = default;
|
|
explicit PushResult(std::optional<T> value) noexcept : m_value(std::move(value)) {}
|
|
|
|
const std::optional<T> m_value = std::nullopt;
|
|
};
|
|
}
|