More work on option again

This commit is contained in:
2024-10-02 23:47:54 +02:00
parent 80b18cc979
commit 85462f25d6
4 changed files with 34 additions and 10 deletions

View File

@ -13,3 +13,4 @@ build --cxxopt=-Wno-pre-c++17-compat
build --cxxopt=-Wno-c++20-compat build --cxxopt=-Wno-c++20-compat
build --cxxopt=-Wno-unused-macros build --cxxopt=-Wno-unused-macros
build --cxxopt=-Wno-documentation-unknown-command build --cxxopt=-Wno-documentation-unknown-command
build --cxxopt=-Wno-extra-semi-stmt

View File

@ -2,6 +2,7 @@ cc_library(
name = "asl", name = "asl",
hdrs = [ hdrs = [
"annotations.hpp", "annotations.hpp",
"assert.hpp",
"integers.hpp", "integers.hpp",
"layout.hpp", "layout.hpp",
"maybe_uninit.hpp", "maybe_uninit.hpp",

5
asl/assert.hpp Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#define ASL_ASSERT(...) \
if (__VA_ARGS__) {} \
else { __debugbreak(); }

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "asl/assert.hpp"
#include "asl/meta.hpp" #include "asl/meta.hpp"
#include "asl/maybe_uninit.hpp" #include "asl/maybe_uninit.hpp"
@ -9,12 +10,32 @@ namespace asl
struct nullopt_t {}; struct nullopt_t {};
static constexpr nullopt_t nullopt{}; static constexpr nullopt_t nullopt{};
// @Todo(option) Niche
// @Todo(option) Reference
// @Todo(option) Function
// @Todo(option) Arrays
template<is_object T> template<is_object T>
class option class option
{ {
maybe_uninit<T> m_payload; maybe_uninit<T> m_payload;
bool m_has_value = false; bool m_has_value = false;
template<typename... Args>
constexpr void construct(Args&&... args) &
{
ASL_ASSERT(!m_has_value);
m_payload.init_unsafe(ASL_FWD(args)...);
m_has_value = true;
}
template<typename Arg>
constexpr void assign(Arg&& arg) &
{
ASL_ASSERT(m_has_value);
m_payload.as_init_unsafe() = ASL_FWD(arg);
}
public: public:
constexpr option() = default; constexpr option() = default;
constexpr option(nullopt_t) {} // NOLINT(*-explicit-conversions) constexpr option(nullopt_t) {} // NOLINT(*-explicit-conversions)
@ -23,8 +44,7 @@ public:
{ {
if (other.m_has_value) if (other.m_has_value)
{ {
m_payload.init_unsafe(other.m_payload.as_init_unsafe()); construct(other.m_payload.as_init_unsafe());
m_has_value = true;
} }
} }
@ -32,8 +52,7 @@ public:
{ {
if (other.m_has_value) if (other.m_has_value)
{ {
m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe())); construct(ASL_MOVE(other.m_payload.as_init_unsafe()));
m_has_value = true;
} }
} }
@ -45,12 +64,11 @@ public:
{ {
if (m_has_value) if (m_has_value)
{ {
m_payload.as_init_unsafe() = other.m_payload.as_init_unsafe(); assign(other.m_payload.as_init_unsafe());
} }
else else
{ {
m_payload.init_unsafe(other.m_payload.as_init_unsafe()); construct(other.m_payload.as_init_unsafe());
m_has_value = true;
} }
} }
else if (m_has_value) else if (m_has_value)
@ -69,12 +87,11 @@ public:
{ {
if (m_has_value) if (m_has_value)
{ {
m_payload.as_init_unsafe() = ASL_MOVE(other.m_payload.as_init_unsafe()); assign(ASL_MOVE(other.m_payload.as_init_unsafe()));
} }
else else
{ {
m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe())); construct(ASL_MOVE(other.m_payload.as_init_unsafe()));
m_has_value = true;
} }
} }
else if (m_has_value) else if (m_has_value)