summaryrefslogtreecommitdiff
path: root/asl
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-05 18:53:16 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-05 18:53:16 +0100
commite65fe1b93684f9517599be695eb40aa4537fc6c7 (patch)
tree7b3c429e20574c9c0a66d1302a978d1b39379397 /asl
parent11894bef04095c37196af5ae1bfed885775d49eb (diff)
Add status_or
Diffstat (limited to 'asl')
-rw-r--r--asl/BUILD.bazel2
-rw-r--r--asl/option.hpp1
-rw-r--r--asl/status.hpp5
-rw-r--r--asl/status_or.hpp170
-rw-r--r--asl/tests/status_or_tests.cpp88
5 files changed, 263 insertions, 3 deletions
diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel
index da1018e..571b8f9 100644
--- a/asl/BUILD.bazel
+++ b/asl/BUILD.bazel
@@ -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",
diff --git a/asl/option.hpp b/asl/option.hpp
index 8c0a573..eb6a91c 100644
--- a/asl/option.hpp
+++ b/asl/option.hpp
@@ -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> &&
diff --git a/asl/status.hpp b/asl/status.hpp
index 0d1175e..aa64e4e 100644
--- a/asl/status.hpp
+++ b/asl/status.hpp
@@ -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;
}
diff --git a/asl/status_or.hpp b/asl/status_or.hpp
new file mode 100644
index 0000000..04fcc4a
--- /dev/null
+++ b/asl/status_or.hpp
@@ -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
+
diff --git a/asl/tests/status_or_tests.cpp b/asl/tests/status_or_tests.cpp
new file mode 100644
index 0000000..5038e7e
--- /dev/null
+++ b/asl/tests/status_or_tests.cpp
@@ -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);
+}
+