Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/spec/ProxyFacade.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,32 @@ A type `F` meets the *ProxyFacade* requirements of a type `P` and a type `MP`, w

In other words, `P` either supports dereferencing or provides an `element_type`, and `std::pointer_traits<P>` yields a valid `element_type`.

The copyability requirements below and the constructors of [`proxy`](proxy/constructor.md) make use of the following exposition-only concepts:

```cpp
template <class T, class... Args>
concept constructible = requires {
::new (std::declval<void*>()) T(std::declval<Args>()...);
}; // exposition only

template <class T, class... Args>
concept nothrow-constructible = constructible<T, Args...> &&
noexcept(::new (std::declval<void*>()) T(std::declval<Args>()...)); // exposition only
```

*constructible* is the condition under which [`std::construct_at`](https://en.cppreference.com/w/cpp/memory/construct_at) participates in overload resolution. Unlike `std::is_constructible_v`, `std::is_nothrow_constructible_v`, and `std::constructible_from`, these concepts do not involve the destructor of `T`. Therefore, a `P` that is not destructible can be stored when `F::destructibility` is `lifetime_requirement::none` and `P` meets the relocatability requirement below. By default, that means `F::relocatability` is also `lifetime_requirement::none`, because every other value requires `P` to be destructible unless [`is_bitwise_trivially_relocatable`](is_bitwise_trivially_relocatable.md) is specialized for `P`. Such a `P` can be copied when `F::copyability` is `lifetime_requirement::supported` or `lifetime_requirement::nothrow`, but not `lifetime_requirement::trivial`, because `std::is_trivially_copy_constructible_v<P>` is `false` for a `P` that is not destructible. A `P` whose destructor may throw can meet `lifetime_requirement::nothrow` copyability.

## Notes

Copyability is defined as *copy-construct an object at a given address*. Specifically, the value of `F::copyability` maps to the following requirements on `P`:

| Value | Requirement on `P` |
| --------------------------------- | ------------------------------------------------------------ |
| `lifetime_requirement::none` | None |
| `lifetime_requirement::supported` | `constructible<P, const P&>` |
| `lifetime_requirement::nothrow` | `nothrow-constructible<P, const P&>` |
| `lifetime_requirement::trivial` | `std::is_trivially_copy_constructible_v<P>` |

Relocatability is defined as *move-construct an object and then destroy the original instance*. Specifically, the value of `F::relocatability` maps to the following requirements on `P`:

| Value | Requirement on `P` |
Expand Down
15 changes: 8 additions & 7 deletions docs/spec/proxy/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ proxy(proxy<F2, MP>&& rhs)

// (6)
template <class P>
proxy(P&& ptr) noexcept(std::is_nothrow_constructible_v<std::decay_t<P>, P>)
requires(std::is_constructible_v<std::decay_t<P>, P>);
proxy(P&& ptr) noexcept(nothrow-constructible<std::decay_t<P>, P>)
requires(constructible<std::decay_t<P>, P>);

// (7)
template <class P, class... Args>
explicit proxy(std::in_place_type_t<P>, Args&&... args)
noexcept(std::is_nothrow_constructible_v<P, Args...>)
requires(std::is_constructible_v<P, Args...>);
noexcept(nothrow-constructible<P, Args...>)
requires(constructible<P, Args...>);

// (8)
template <class P, class U, class... Args>
explicit proxy(std::in_place_type_t<P>, std::initializer_list<U> il,
Args&&... args)
noexcept(std::is_nothrow_constructible_v<
P, std::initializer_list<U>&, Args...>)
requires(std::is_constructible_v<P, std::initializer_list<U>&, Args...>);
noexcept(nothrow-constructible<P, std::initializer_list<U>&, Args...>)
requires(constructible<P, std::initializer_list<U>&, Args...>);
```

See [*ProxyFacade* requirements](../ProxyFacade.md) for the definitions of the exposition-only concepts *constructible* and *nothrow-constructible*.

Creates a new `proxy`.

- `(1)` Default constructor and the constructor taking `nullptr` construct a `proxy` that does not contain a value.
Expand Down
4 changes: 3 additions & 1 deletion docs/spec/proxy/destructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
~proxy() noexcept(F::destructibility == lifetime_requirement::nothrow)
requires(F::destructibility == lifetime_requirement::supported ||
F::destructibility == lifetime_requirement::nothrow);
~proxy() requires(F::destructibility == lifetime_requirement::none)
= delete;
```

Destroys the `proxy` object. If the `proxy` contains a value, the contained value is also destroyed. When `F::destructibility == lifetime_requirement::trivial`, the destructor is defaulted. It is trivial only if the metadata storage and accessor base classes also have trivial destructors.
Destroys the `proxy` object. If the `proxy` contains a value, the contained value is also destroyed. When `F::destructibility == lifetime_requirement::trivial`, the destructor is defaulted. It is trivial only if the metadata storage and accessor base classes also have trivial destructors. When `F::destructibility == lifetime_requirement::none`, the destructor is deleted, so a variable or a function return value of this `proxy` type is ill-formed. Such a `proxy` can be created with a *new-expression*.

## Example

Expand Down
6 changes: 4 additions & 2 deletions docs/spec/proxy/swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
void swap(proxy& rhs)
noexcept(F::relocatability >= lifetime_requirement::nothrow ||
F::copyability == lifetime_requirement::trivial)
requires(F::relocatability >= lifetime_requirement::supported ||
F::copyability == lifetime_requirement::trivial);
requires(F::relocatability == lifetime_requirement::trivial ||
F::copyability == lifetime_requirement::trivial ||
(F::relocatability >= lifetime_requirement::supported &&
F::destructibility >= lifetime_requirement::supported));

// (2) (since 5.0.0)
void swap(proxy& rhs)
Expand Down
42 changes: 28 additions & 14 deletions include/proxy/v5/detail/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,25 @@ struct add_qualifier_traits<T, qualifier_type::const_rv>
template <class T, qualifier_type Q>
using add_qualifier_t = add_qualifier_traits<T, Q>::type;

// Like std::is_constructible_v, but does not require T to be destructible.
template <class T, class... Args>
concept constructible =
requires { ::new (std::declval<void*>()) T(std::declval<Args>()...); };
template <class T, class... Args>
concept nothrow_constructible =
constructible<T, Args...> &&
noexcept(::new (std::declval<void*>()) T(std::declval<Args>()...));

template <class T, lifetime_requirement LR>
struct copyability_traits : inapplicable_traits {};
template <class T>
struct copyability_traits<T, lifetime_requirement::none> : applicable_traits {};
template <class T>
requires(std::is_copy_constructible_v<T>)
requires(constructible<T, const T&>)
struct copyability_traits<T, lifetime_requirement::supported>
: applicable_traits {};
template <class T>
requires(std::is_nothrow_copy_constructible_v<T>)
requires(nothrow_constructible<T, const T&>)
struct copyability_traits<T, lifetime_requirement::nothrow>
: applicable_traits {};
template <class T>
Expand Down Expand Up @@ -547,8 +556,8 @@ consteval bool is_metadata_well_formed() {
}
struct copy_dispatch {
template <class T>
PRO5D_STATIC_CALL(void, const T& self, void* rhs) noexcept(
std::is_nothrow_copy_constructible_v<T>) {
PRO5D_STATIC_CALL(void, const T& self,
void* rhs) noexcept(nothrow_constructible<T, const T&>) {
std::construct_at(static_cast<T*>(rhs), self);
}
};
Expand Down Expand Up @@ -1164,7 +1173,7 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
using metadata_policy_type = MP;

proxy() noexcept { initialize(); }
proxy(std::nullptr_t) noexcept : proxy() {}
proxy(std::nullptr_t) noexcept { initialize(); }
proxy(const proxy&) noexcept
requires(F::copyability == lifetime_requirement::trivial)
= default;
Expand Down Expand Up @@ -1206,28 +1215,28 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
}
template <class P>
constexpr proxy(P&& ptr) noexcept(
std::is_nothrow_constructible_v<std::decay_t<P>, P>)
detail::nothrow_constructible<std::decay_t<P>, P>)
requires(!detail::specialization_of<std::decay_t<P>, proxy> &&
detail::pointer_like<std::decay_t<P>> &&
std::is_constructible_v<std::decay_t<P>, P>)
detail::constructible<std::decay_t<P>, P>)
{
initialize<std::decay_t<P>>(std::forward<P>(ptr));
}
template <class P, class... Args>
constexpr explicit proxy(std::in_place_type_t<P>, Args&&... args) noexcept(
std::is_nothrow_constructible_v<P, Args...>)
requires(detail::pointer_like<P> && std::is_constructible_v<P, Args...>)
detail::nothrow_constructible<P, Args...>)
requires(detail::pointer_like<P> && detail::constructible<P, Args...>)
{
initialize<P>(std::forward<Args>(args)...);
}
template <class P, class U, class... Args>
constexpr explicit proxy(
std::in_place_type_t<P>, std::initializer_list<U> il,
Args&&... args) noexcept(std::
is_nothrow_constructible_v<
Args&&... args) noexcept(detail::
nothrow_constructible<
P, std::initializer_list<U>&, Args...>)
requires(detail::pointer_like<P> &&
std::is_constructible_v<P, std::initializer_list<U>&, Args...>)
detail::constructible<P, std::initializer_list<U>&, Args...>)
{
initialize<P>(il, std::forward<Args>(args)...);
}
Expand Down Expand Up @@ -1333,6 +1342,9 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
}
return *this;
}
~proxy()
requires(F::destructibility == lifetime_requirement::none)
= delete;
~proxy()
requires(F::destructibility == lifetime_requirement::trivial)
= default;
Expand All @@ -1355,8 +1367,10 @@ class proxy : public detail::proxy_traits<F, MP>::direct_accessor,
lifetime_requirement::nothrow ||
F::copyability ==
lifetime_requirement::trivial)
requires(F::relocatability >= lifetime_requirement::supported ||
F::copyability == lifetime_requirement::trivial)
requires(F::relocatability == lifetime_requirement::trivial ||
F::copyability == lifetime_requirement::trivial ||
(F::relocatability >= lifetime_requirement::supported &&
F::destructibility >= lifetime_requirement::supported))
{
if constexpr (F::relocatability == lifetime_requirement::trivial ||
F::copyability == lifetime_requirement::trivial) {
Expand Down
121 changes: 121 additions & 0 deletions tests/proxy_lifetime_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
// Licensed under the MIT License.

#include "utils.h"
#include <cstddef>
#include <gtest/gtest.h>
#include <initializer_list>
#include <new>
#include <proxy/proxy.h>
#include <type_traits>

namespace proxy_lifetime_tests_detail {

Expand Down Expand Up @@ -49,6 +53,101 @@ static_assert(!std::is_convertible_v<pro::proxy<utils::spec::Stringable>,
static_assert(!std::is_convertible_v<pro::proxy<TestTrivialFacade>,
pro::proxy<TestFacade>>);

struct TestNonDestructibleFacade
: pro::facade_builder //
::add_convention<utils::spec::FreeToString, std::string() const> //
::support_relocation<pro::lifetime_requirement::none> //
::support_destruction<pro::lifetime_requirement::none> //
::build {};

struct TestNonDestructibleTrivialRelocationFacade
: pro::facade_builder //
::add_convention<utils::spec::FreeToString, std::string() const> //
::support_relocation<pro::lifetime_requirement::trivial> //
::support_destruction<pro::lifetime_requirement::none> //
::build {};

struct TestNonDestructibleRelocationFacade
: pro::facade_builder //
::add_convention<utils::spec::FreeToString, std::string() const> //
::support_relocation<pro::lifetime_requirement::nothrow> //
::support_destruction<pro::lifetime_requirement::none> //
::build {};

template <class P>
concept MemberSwappable = requires(P& lhs, P& rhs) { lhs.swap(rhs); };

static_assert(!std::is_destructible_v<pro::proxy<TestNonDestructibleFacade>>);
static_assert(
MemberSwappable<pro::proxy<TestNonDestructibleTrivialRelocationFacade>>);
static_assert(
!MemberSwappable<pro::proxy<TestNonDestructibleRelocationFacade>>);

class NonDestructiblePtr {
public:
using element_type = int;

explicit NonDestructiblePtr(int* ptr) noexcept : ptr_(ptr) {}
NonDestructiblePtr(std::initializer_list<int>, int* ptr) noexcept
: ptr_(ptr) {}
NonDestructiblePtr(const NonDestructiblePtr&) noexcept = default;
~NonDestructiblePtr() = delete;
int& operator*() const noexcept { return *ptr_; }

private:
int* ptr_;
};

class ThrowingDestructionPtr {
public:
using element_type = int;

explicit ThrowingDestructionPtr(int* ptr) noexcept : ptr_(ptr) {}
ThrowingDestructionPtr(std::initializer_list<int>, int* ptr) noexcept
: ptr_(ptr) {}
ThrowingDestructionPtr(const ThrowingDestructionPtr&) noexcept = default;
~ThrowingDestructionPtr() noexcept(false) {}
int& operator*() const noexcept { return *ptr_; }

private:
int* ptr_;
};

template <class T, class... Args>
concept NewConstructible =
requires { ::new (std::declval<void*>()) T(std::declval<Args>()...); };

template <class T, class... Args>
concept NothrowNewConstructible = requires {
{ ::new (std::declval<void*>()) T(std::declval<Args>()...) } noexcept;
};

static_assert(NewConstructible<pro::proxy<TestNonDestructibleFacade>,
const NonDestructiblePtr&>);
static_assert(NewConstructible<pro::proxy<TestNonDestructibleFacade>,
std::in_place_type_t<NonDestructiblePtr>, int*>);
static_assert(NewConstructible<pro::proxy<TestNonDestructibleFacade>,
std::in_place_type_t<NonDestructiblePtr>,
std::initializer_list<int>, int*>);

static_assert(NothrowNewConstructible<pro::proxy<TestThrowingDestructionFacade>,
const ThrowingDestructionPtr&>);
static_assert(NothrowNewConstructible<
pro::proxy<TestThrowingDestructionFacade>,
std::in_place_type_t<ThrowingDestructionPtr>, int*>);
static_assert(
NothrowNewConstructible<pro::proxy<TestThrowingDestructionFacade>,
std::in_place_type_t<ThrowingDestructionPtr>,
std::initializer_list<int>, int*>);

struct TestNonDestructibleCopyFacade
: pro::facade_builder //
::add_convention<utils::spec::FreeToString, std::string() const> //
::support_copy<pro::lifetime_requirement::supported> //
::support_relocation<pro::lifetime_requirement::none> //
::support_destruction<pro::lifetime_requirement::none> //
::build {};

} // namespace proxy_lifetime_tests_detail

namespace detail = proxy_lifetime_tests_detail;
Expand Down Expand Up @@ -1589,6 +1688,28 @@ TEST(ProxyLifetimeTests, Test_ConvertingCopyAssignment_NoRelocation) {
ASSERT_EQ(ToString(*p2), "Session 2");
}

TEST(ProxyLifetimeTests, Test_NonDestructible_Construction) {
using Proxy = pro::proxy<detail::TestNonDestructibleFacade>;
alignas(Proxy) std::byte null_storage[sizeof(Proxy)];
alignas(Proxy) std::byte value_storage[sizeof(Proxy)];
Proxy* p1 = ::new (static_cast<void*>(null_storage)) Proxy(nullptr);
ASSERT_FALSE(p1->has_value());
int v = 123;
Proxy* p2 = ::new (static_cast<void*>(value_storage)) Proxy(&v);
ASSERT_EQ(ToString(**p2), "123");
}

TEST(ProxyLifetimeTests, Test_NonDestructible_NonDestructiblePtr) {
using Proxy = pro::proxy<detail::TestNonDestructibleCopyFacade>;
alignas(Proxy) std::byte storage1[sizeof(Proxy)];
alignas(Proxy) std::byte storage2[sizeof(Proxy)];
int v = 123;
Proxy* p1 = ::new (static_cast<void*>(storage1))
Proxy(std::in_place_type<detail::NonDestructiblePtr>, &v);
Proxy* p2 = ::new (static_cast<void*>(storage2)) Proxy(*p1);
ASSERT_EQ(ToString(**p2), "123");
}

TEST(ProxyLifetimeTests, Test_Substitution_TrivialDerived) {
struct Super : pro::facade_builder //
::add_convention<utils::spec::FreeToString,
Expand Down
35 changes: 35 additions & 0 deletions tests/proxy_traits_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ using MockCopyableSmallPtr =
using MockTrivialPtr = MockPtr<true, true, true, sizeof(void*), alignof(void*)>;
using MockFunctionPtr = void (*)();

struct MockNonDestructiblePtr {
using element_type = MockNonDestructiblePtr;

MockNonDestructiblePtr(const MockNonDestructiblePtr&) noexcept = default;
~MockNonDestructiblePtr() = delete;
const MockNonDestructiblePtr* operator->() const noexcept { return this; }
};

struct MockThrowingDestructionPtr {
using element_type = MockThrowingDestructionPtr;

MockThrowingDestructionPtr(const MockThrowingDestructionPtr&) noexcept =
default;
~MockThrowingDestructionPtr() noexcept(false) {}
const MockThrowingDestructionPtr* operator->() const noexcept { return this; }
};

} // namespace proxy_traits_tests_detail

namespace pro {
Expand Down Expand Up @@ -213,6 +230,24 @@ static_assert(
std::is_assignable_v<pro::proxy<CopyableSmallFacade>, MockFunctionPtr>);
static_assert(sizeof(pro::proxy<CopyableSmallFacade>) == 2 * sizeof(void*));

struct NonDestructibleCopyableFacade
: pro::facade_builder //
::support_copy<pro::lifetime_requirement::supported> //
::support_relocation<pro::lifetime_requirement::none> //
::support_destruction<pro::lifetime_requirement::none> //
::build {};
static_assert(
pro::proxiable<MockNonDestructiblePtr, NonDestructibleCopyableFacade>);

struct NothrowCopyableFacade
: pro::facade_builder //
::support_copy<pro::lifetime_requirement::nothrow> //
::support_relocation<pro::lifetime_requirement::none> //
::support_destruction<pro::lifetime_requirement::supported> //
::build {};
static_assert(
pro::proxiable<MockThrowingDestructionPtr, NothrowCopyableFacade>);

struct TrivialFacade
: pro::facade_builder //
::restrict_layout<sizeof(void*), alignof(void*)> //
Expand Down
Loading