Add a niche to box

This commit is contained in:
2024-11-22 17:13:03 +01:00
parent f5ef1937ea
commit 58bb86fd2f
5 changed files with 32 additions and 26 deletions

14
MODULE.bazel.lock generated
View File

@ -64,20 +64,20 @@
"@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": { "@@apple_support~//crosstool:setup.bzl%apple_cc_configure_extension": {
"general": { "general": {
"bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=", "bzlTransitiveDigest": "PjIds3feoYE8SGbbIq2SFTZy3zmxeO2tQevJZNDo7iY=",
"usagesDigest": "+hz7IHWN6A1oVJJWNDB6yZRG+RYhF76wAYItpAeIUIg=", "usagesDigest": "aLmqbvowmHkkBPve05yyDNGN7oh7QE9kBADr3QIZTZs=",
"recordedFileInputs": {}, "recordedFileInputs": {},
"recordedDirentsInputs": {}, "recordedDirentsInputs": {},
"envVariables": {}, "envVariables": {},
"generatedRepoSpecs": { "generatedRepoSpecs": {
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
},
"local_config_apple_cc": { "local_config_apple_cc": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl", "bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf", "ruleClassName": "_apple_cc_autoconf",
"attributes": {} "attributes": {}
},
"local_config_apple_cc_toolchains": {
"bzlFile": "@@apple_support~//crosstool:setup.bzl",
"ruleClassName": "_apple_cc_autoconf_toolchains",
"attributes": {}
} }
}, },
"recordedRepoMappingEntries": [ "recordedRepoMappingEntries": [
@ -92,7 +92,7 @@
"@@platforms//host:extension.bzl%host_platform": { "@@platforms//host:extension.bzl%host_platform": {
"general": { "general": {
"bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=", "bzlTransitiveDigest": "xelQcPZH8+tmuOHVjL9vDxMnnQNMlwj0SlvgoqBkm4U=",
"usagesDigest": "pCYpDQmqMbmiiPI1p2Kd3VLm5T48rRAht5WdW0X2GlA=", "usagesDigest": "meSzxn3DUCcYEhq4HQwExWkWtU4EjriRBQLsZN+Q0SU=",
"recordedFileInputs": {}, "recordedFileInputs": {},
"recordedDirentsInputs": {}, "recordedDirentsInputs": {},
"envVariables": {}, "envVariables": {},

View File

@ -2,7 +2,6 @@
#include "asl/config.hpp" #include "asl/config.hpp"
#if ASL_COMPILER_CLANG_CL #if ASL_COMPILER_CLANG_CL
#define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]] #define ASL_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
#elif ASL_COMPILER_CLANG #elif ASL_COMPILER_CLANG

View File

@ -16,15 +16,18 @@ class box
ASL_NO_UNIQUE_ADDRESS Allocator m_alloc; ASL_NO_UNIQUE_ADDRESS Allocator m_alloc;
public: public:
explicit constexpr box(T* ptr = nullptr) explicit constexpr box(niche)
requires default_constructible<Allocator> requires default_constructible<Allocator>
: m_ptr{ptr} : m_ptr{nullptr}
, m_alloc{}
{} {}
constexpr box(T* ptr, Allocator alloc) constexpr box(T* ptr, Allocator alloc)
: m_ptr{ptr} : m_ptr{ptr}
, m_alloc{ASL_MOVE(alloc)} , m_alloc{ASL_MOVE(alloc)}
{} {
ASL_ASSERT(m_ptr != nullptr);
}
constexpr box(box&& other) constexpr box(box&& other)
: m_ptr{exchange(other.m_ptr, nullptr)} : m_ptr{exchange(other.m_ptr, nullptr)}
@ -77,6 +80,11 @@ public:
ASL_ASSERT(m_ptr != nullptr); ASL_ASSERT(m_ptr != nullptr);
return m_ptr; return m_ptr;
} }
constexpr bool operator==(niche) const
{
return m_ptr == nullptr;
}
}; };
template<is_object T, allocator Allocator = DefaultAllocator, typename... Args> template<is_object T, allocator Allocator = DefaultAllocator, typename... Args>

View File

@ -160,4 +160,13 @@ template<typename T, int N> struct _is_array_helper<T[N]> : true_type {};
template<typename T> concept is_array = _is_array_helper<T>::value; template<typename T> concept is_array = _is_array_helper<T>::value;
struct niche {};
template<typename T>
concept has_niche = constructible_from<T, niche> &&
requires (const T& value, niche n)
{
{ value == n } -> same_as<bool>;
};
} // namespace asl } // namespace asl

View File

@ -4,30 +4,20 @@
#include "asl/tests/test_types.hpp" #include "asl/tests/test_types.hpp"
static_assert(sizeof(asl::box<int>) == sizeof(int*)); static_assert(sizeof(asl::box<int>) == sizeof(int*));
static_assert(asl::default_constructible<asl::box<int>>);
static_assert(!asl::copyable<asl::box<int>>); static_assert(!asl::copyable<asl::box<int>>);
static_assert(asl::moveable<asl::box<int>>); static_assert(asl::moveable<asl::box<int>>);
static_assert(asl::default_constructible<asl::box<NonMoveConstructible>>); static_assert(asl::has_niche<asl::box<int>>);
ASL_TEST(destructor) ASL_TEST(destructor)
{ {
bool d = false; bool d = false;
{ {
asl::box<DestructorObserver> box2; auto box = asl::make_box<DestructorObserver>(&d);
ASL_TEST_ASSERT(!d);
{
auto box = asl::make_box<DestructorObserver>(&d);
ASL_TEST_ASSERT(!d);
auto box3 = ASL_MOVE(box);
ASL_TEST_ASSERT(!d);
box2 = ASL_MOVE(box3); auto box3 = ASL_MOVE(box);
ASL_TEST_ASSERT(!d);
}
ASL_TEST_ASSERT(!d); ASL_TEST_ASSERT(!d);
} }