From 58bb86fd2f0ecd740fc5bc8078de44221c12c70a Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 22 Nov 2024 17:13:03 +0100 Subject: Add a niche to box --- MODULE.bazel.lock | 12 ++++++------ asl/annotations.hpp | 1 - asl/box.hpp | 16 ++++++++++++---- asl/meta.hpp | 9 +++++++++ asl/tests/box_tests.cpp | 22 ++++++---------------- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index d62a47c..21b358e 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -64,19 +64,19 @@ "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "general": { "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", - "usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", + "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, "generatedRepoSpecs": { - "local_config_apple_cc_toolchains": { + "local_config_apple_cc": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf_toolchains", + "ruleClassName": "_apple_cc_autoconf", "attributes": {} }, - "local_config_apple_cc": { + "local_config_apple_cc_toolchains": { "bzlFile": "@@apple_support~//crosstool:setup.bzl", - "ruleClassName": "_apple_cc_autoconf", + "ruleClassName": "_apple_cc_autoconf_toolchains", "attributes": {} } }, @@ -92,7 +92,7 @@ "@@platforms//host:extension.bzl%host_platform": { "general": { "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", - "usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", + "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=", "recordedFileInputs": {}, "recordedDirentsInputs": {}, "envVariables": {}, diff --git a/asl/annotations.hpp b/asl/annotations.hpp index a398e13..fc65378 100644 --- a/asl/annotations.hpp +++ b/asl/annotations.hpp @@ -2,7 +2,6 @@ #include "asl/config.hpp" - #if ASL_COMPILER_CLANG_CL #define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] #elif ASL_COMPILER_CLANG diff --git a/asl/box.hpp b/asl/box.hpp index d8ccd36..7556d76 100644 --- a/asl/box.hpp +++ b/asl/box.hpp @@ -16,15 +16,18 @@ class box ASL_NO_UNIQUE_ADDRESS Allocator m_alloc; public: - explicit constexpr box(T* ptr = nullptr) + explicit constexpr box(niche) requires default_constructible - : m_ptr{ptr} + : m_ptr{nullptr} + , m_alloc{} {} - + constexpr box(T* ptr, Allocator alloc) : m_ptr{ptr} , m_alloc{ASL_MOVE(alloc)} - {} + { + ASL_ASSERT(m_ptr != nullptr); + } constexpr box(box&& other) : m_ptr{exchange(other.m_ptr, nullptr)} @@ -77,6 +80,11 @@ public: ASL_ASSERT(m_ptr != nullptr); return m_ptr; } + + constexpr bool operator==(niche) const + { + return m_ptr == nullptr; + } }; template diff --git a/asl/meta.hpp b/asl/meta.hpp index f0397a1..72b36fd 100644 --- a/asl/meta.hpp +++ b/asl/meta.hpp @@ -160,4 +160,13 @@ template struct _is_array_helper : true_type {}; template concept is_array = _is_array_helper::value; +struct niche {}; + +template +concept has_niche = constructible_from && + requires (const T& value, niche n) + { + { value == n } -> same_as; + }; + } // namespace asl diff --git a/asl/tests/box_tests.cpp b/asl/tests/box_tests.cpp index f1a35f3..3813639 100644 --- a/asl/tests/box_tests.cpp +++ b/asl/tests/box_tests.cpp @@ -4,30 +4,20 @@ #include "asl/tests/test_types.hpp" static_assert(sizeof(asl::box) == sizeof(int*)); -static_assert(asl::default_constructible>); static_assert(!asl::copyable>); static_assert(asl::moveable>); -static_assert(asl::default_constructible>); +static_assert(asl::has_niche>); ASL_TEST(destructor) { bool d = false; { - asl::box box2; - - { - auto box = asl::make_box(&d); - ASL_TEST_ASSERT(!d); - - - auto box3 = ASL_MOVE(box); - ASL_TEST_ASSERT(!d); - - box2 = ASL_MOVE(box3); - ASL_TEST_ASSERT(!d); - } - + auto box = asl::make_box(&d); + ASL_TEST_ASSERT(!d); + + + auto box3 = ASL_MOVE(box); ASL_TEST_ASSERT(!d); } -- cgit