diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-10-02 23:47:54 +0200 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 85462f25d6335bcca9423b6fc1e624d9a76a2375 (patch) | |
tree | f2bac09a057573ff745de6dfd7b80fcd90ac8885 /asl | |
parent | 80b18cc979fcb1e772e5b7c7d45aa72602d461d7 (diff) |
More work on option again
Diffstat (limited to 'asl')
-rw-r--r-- | asl/BUILD.bazel | 1 | ||||
-rw-r--r-- | asl/assert.hpp | 5 | ||||
-rw-r--r-- | asl/option.hpp | 37 |
3 files changed, 33 insertions, 10 deletions
diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index d7371c1..06e2a9f 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -2,6 +2,7 @@ cc_library( name = "asl",
hdrs = [
"annotations.hpp",
+ "assert.hpp",
"integers.hpp",
"layout.hpp",
"maybe_uninit.hpp",
diff --git a/asl/assert.hpp b/asl/assert.hpp new file mode 100644 index 0000000..0c80d9c --- /dev/null +++ b/asl/assert.hpp @@ -0,0 +1,5 @@ +#pragma once
+
+#define ASL_ASSERT(...) \
+ if (__VA_ARGS__) {} \
+ else { __debugbreak(); }
diff --git a/asl/option.hpp b/asl/option.hpp index 6aa5ebb..8662250 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -1,5 +1,6 @@ #pragma once
+#include "asl/assert.hpp"
#include "asl/meta.hpp"
#include "asl/maybe_uninit.hpp"
@@ -9,12 +10,32 @@ namespace asl struct nullopt_t {};
static constexpr nullopt_t nullopt{};
+// @Todo(option) Niche
+// @Todo(option) Reference
+// @Todo(option) Function
+// @Todo(option) Arrays
+
template<is_object T>
class option
{
maybe_uninit<T> m_payload;
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:
constexpr option() = default;
constexpr option(nullopt_t) {} // NOLINT(*-explicit-conversions)
@@ -23,8 +44,7 @@ public: {
if (other.m_has_value)
{
- m_payload.init_unsafe(other.m_payload.as_init_unsafe());
- m_has_value = true;
+ construct(other.m_payload.as_init_unsafe());
}
}
@@ -32,8 +52,7 @@ public: {
if (other.m_has_value)
{
- m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe()));
- m_has_value = true;
+ construct(ASL_MOVE(other.m_payload.as_init_unsafe()));
}
}
@@ -45,12 +64,11 @@ public: {
if (m_has_value)
{
- m_payload.as_init_unsafe() = other.m_payload.as_init_unsafe();
+ assign(other.m_payload.as_init_unsafe());
}
else
{
- m_payload.init_unsafe(other.m_payload.as_init_unsafe());
- m_has_value = true;
+ construct(other.m_payload.as_init_unsafe());
}
}
else if (m_has_value)
@@ -69,12 +87,11 @@ public: {
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
{
- m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe()));
- m_has_value = true;
+ construct(ASL_MOVE(other.m_payload.as_init_unsafe()));
}
}
else if (m_has_value)
|