Files
FeatherLog/include/featherlog/push_result.hpp
T
2026-08-17 18:27:39 +08:00

42 lines
1.2 KiB
C++

#ifndef FEATHERLOG_PUSH_RESULT_HPP
#define FEATHERLOG_PUSH_RESULT_HPP
#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)) {}
std::optional<T> m_value = std::nullopt;
};
}
#endif