Add status_or

This commit is contained in:
2025-01-05 18:53:16 +01:00
parent 11894bef04
commit e65fe1b936
7 changed files with 264 additions and 4 deletions

View File

@ -31,3 +31,4 @@ Checks:
- "-readability-use-anyofallof"
- "-readability-function-cognitive-complexity"
- "-readability-math-missing-parentheses"
- "-*-rvalue-reference-param-not-moved"

View File

@ -21,6 +21,7 @@ cc_library(
"print.hpp",
"span.hpp",
"status.hpp",
"status_or.hpp",
"string.hpp",
"string_view.hpp",
"utility.hpp",
@ -61,6 +62,7 @@ cc_library(
"option",
"span",
"status",
"status_or",
"string",
"string_view",
"utility",

View File

@ -35,7 +35,6 @@ concept not_assignable_from_option =
!assignable_from<T&, option<U>&&> &&
!assignable_from<T&, const option<U>&&>;
template<typename T, typename U>
concept not_constructible_assignable_from_option =
not_constructible_from_option<T, U> &&

View File

@ -75,7 +75,7 @@ public:
}
constexpr status(status&& other)
: m_payload{exchange(other.m_payload, nullptr)}
: m_payload{exchange(other.m_payload, status_to_payload(other.code()))}
{}
constexpr status& operator=(const status& other)
@ -93,7 +93,8 @@ public:
{
if (&other != this)
{
swap(m_payload, other.m_payload);
if (!is_inline()) { unref(); }
m_payload = exchange(other.m_payload, status_to_payload(other.code()));
}
return *this;
}

170
asl/status_or.hpp Normal file
View File

@ -0,0 +1,170 @@
#pragma once
#include "asl/status.hpp"
#include "asl/maybe_uninit.hpp"
namespace asl
{
template<is_object T>
class status_or
{
status m_status;
maybe_uninit<T> m_value{};
public:
// @Todo Convert copy
// @Todo Convert move
constexpr status_or(const status_or& other)
requires copy_constructible<T>
: m_status{other.m_status}
{
if (other.ok())
{
m_value.construct_unsafe(other.m_value.as_init_unsafe());
}
}
constexpr status_or(status_or&& other)
requires move_constructible<T>
: m_status{ASL_MOVE(other.m_status)}
{
if (other.ok())
{
m_value.construct_unsafe(ASL_MOVE(other.m_value.as_init_unsafe()));
}
}
constexpr status_or& operator=(const status_or& other)
requires copyable<T>
{
if (&other != this)
{
if (ok())
{
if (other.ok())
{
m_value.assign_unsafe(other.m_value.as_init_unsafe());
}
else
{
m_value.destroy_unsafe();
}
}
else if (other.ok())
{
m_value.construct_unsafe(other.m_value.as_init_unsafe());
}
m_status = other.m_status;
}
return *this;
}
constexpr status_or& operator=(status_or&& other)
requires moveable<T>
{
if (&other != this)
{
if (ok())
{
if (other.ok())
{
m_value.assign_unsafe(ASL_MOVE(other.m_value.as_init_unsafe()));
}
else
{
m_value.destroy_unsafe();
}
}
else if (other.ok())
{
m_value.construct_unsafe(ASL_MOVE(other.m_value.as_init_unsafe()));
}
m_status = ASL_MOVE(other.m_status);
}
return *this;
}
constexpr ~status_or()
{
if (m_status.ok())
{
m_value.destroy_unsafe();
}
}
// NOLINTNEXTLINE(*-explicit-conversions)
constexpr status_or(const status& status) : m_status{status}
{
ASL_ASSERT_RELEASE(!m_status.ok());
}
// NOLINTNEXTLINE(*-explicit-conversions)
constexpr status_or(status&& status) : m_status{ASL_MOVE(status)}
{
ASL_ASSERT_RELEASE(!m_status.ok());
}
status_or& operator=(status status) = delete;
template<typename U = T>
constexpr explicit (!convertible_from<T, U&&>)
status_or(U&& value)
requires (
constructible_from<T, U&&> &&
!same_as<un_cvref_t<U>, status_or> &&
!same_as<un_cvref_t<U>, status>
)
: m_status{status_code::ok}
, m_value{in_place, ASL_FWD(value)}
{}
constexpr bool ok() const { return m_status.ok(); }
// NOLINTNEXTLINE(*-explicit-conversions)
constexpr operator bool() const { return m_status; }
constexpr status_code code() const { return m_status.code(); }
constexpr string_view message() const { return m_status.message(); }
// @Todo(C++23) Deducing this
constexpr const T& value() const&
{
ASL_ASSERT_RELEASE(ok());
return m_value.as_init_unsafe();
}
constexpr T& value() &
{
ASL_ASSERT_RELEASE(ok());
return m_value.as_init_unsafe();
}
constexpr T&& value() &&
{
ASL_ASSERT_RELEASE(ok());
return ASL_MOVE(m_value.as_init_unsafe());
}
template<typename U>
constexpr T value_or(U&& other_value) const&
requires copy_constructible<T> && convertible_from<T, U&&>
{
return ok() ? value() : static_cast<T>(ASL_FWD(other_value));
}
template<typename U>
constexpr T value_or(U&& other_value) &&
requires move_constructible<T> && convertible_from<T, U&&>
{
return ok() ? ASL_MOVE(value()) : static_cast<T>(ASL_FWD(other_value));
}
};
template<typename T>
status_or(T) -> status_or<T>;
} // namespace asl

View File

@ -0,0 +1,88 @@
#include "asl/status_or.hpp"
#include "asl/testing/testing.hpp"
#include "asl/tests/test_types.hpp"
static_assert(asl::copyable<asl::status_or<TrivialType>>);
static_assert(asl::moveable<asl::status_or<TrivialType>>);
static_assert(asl::copyable<asl::status_or<Copyable>>);
static_assert(asl::moveable<asl::status_or<Copyable>>);
static_assert(!asl::copyable<asl::status_or<MoveableOnly>>);
static_assert(asl::moveable<asl::status_or<MoveableOnly>>);
static_assert(!asl::copyable<asl::status_or<Pinned>>);
static_assert(!asl::moveable<asl::status_or<Pinned>>);
ASL_TEST(ok)
{
asl::status_or<int> s = 6;
ASL_TEST_EXPECT(s.ok());
ASL_TEST_EXPECT(s);
ASL_TEST_EXPECT(s.code() == asl::status_code::ok);
}
ASL_TEST(from_status)
{
asl::status_or<char> s = asl::internal_error();
ASL_TEST_EXPECT(!s.ok());
ASL_TEST_EXPECT(!s);
ASL_TEST_EXPECT(s.code() == asl::status_code::internal);
ASL_TEST_EXPECT(s.message() == ""_sv);
asl::status_or<int> s2 = asl::internal_error("oh no");
ASL_TEST_EXPECT(!s2.ok());
ASL_TEST_EXPECT(!s2);
ASL_TEST_EXPECT(s2.code() == asl::status_code::internal);
ASL_TEST_EXPECT(s2.message() == "oh no"_sv);
asl::status_or<int> s3 = asl::internal_error("{} {}", 1, 2);
ASL_TEST_EXPECT(!s3.ok());
ASL_TEST_EXPECT(!s3);
ASL_TEST_EXPECT(s3.code() == asl::status_code::internal);
ASL_TEST_EXPECT(s3.message() == "1 2"_sv);
}
ASL_TEST(destructor)
{
bool d = false;
{
asl::status_or<DestructorObserver> s{&d};
ASL_TEST_EXPECT(s.ok());
ASL_TEST_EXPECT(!d);
asl::status_or s2 = ASL_MOVE(s);
ASL_TEST_EXPECT(s.ok());
ASL_TEST_EXPECT(!d);
s = ASL_MOVE(s2);
ASL_TEST_EXPECT(s.ok());
ASL_TEST_EXPECT(!d);
}
ASL_TEST_EXPECT(d);
}
ASL_TEST(copy)
{
asl::status_or<int> s = 7;
asl::status_or s2 = s;
s = s2;
ASL_TEST_EXPECT(s.ok());
ASL_TEST_EXPECT(s2.ok());
ASL_TEST_EXPECT(s.value() == 7);
ASL_TEST_EXPECT(s2.value() == 7);
}
ASL_TEST(value_or)
{
asl::status_or<int> s = 7;
asl::status_or<int> s2 = asl::internal_error();
ASL_TEST_EXPECT(s.value_or(45) == 7);
ASL_TEST_EXPECT(s2.value_or(45) == 45);
}

View File

@ -2,5 +2,4 @@ hashing
hash_set
hash_map
logging
status_or
dynlib