From 03baf5ab1e783c80357373ec41ffbe289fff6c56 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Thu, 11 Jul 2024 08:51:46 +0800 Subject: [PATCH 1/3] Improve API design --- proxy.h | 8 +++++--- tests/proxy_lifetime_tests.cpp | 9 +++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/proxy.h b/proxy.h index a7321ff..f5824ae 100644 --- a/proxy.h +++ b/proxy.h @@ -759,6 +759,11 @@ class proxy : public details::facade_traits::direct_accessor { auto&& operator*() const&& noexcept requires(_Traits::has_indirection) { return std::forward(ia_); } + friend void swap(proxy& lhs, proxy& rhs) noexcept(noexcept(lhs.swap(rhs))) + { lhs.swap(rhs); } + friend bool operator==(const proxy& lhs, std::nullptr_t) noexcept + { return !lhs.has_value(); } + private: template P& initialize(Args&&... args) { @@ -821,9 +826,6 @@ template const R& proxy_reflect(const proxy& p) noexcept { return details::proxy_helper::get_meta(p); } -template -void swap(proxy& a, proxy& b) noexcept(noexcept(a.swap(b))) { a.swap(b); } - namespace details { template diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index 90466c3..18fd0fc 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -748,6 +748,15 @@ TEST(ProxyLifetimeTests, TestHasValue) { ASSERT_EQ(ToString(*p1), "123"); } +TEST(ProxyLifetimeTests, TestEqualsToNullptr) { + int foo = 123; + pro::proxy p1; + ASSERT_TRUE(p1 == nullptr); + p1 = &foo; + ASSERT_TRUE(p1 != nullptr); + ASSERT_EQ(ToString(*p1), "123"); +} + TEST(ProxyLifetimeTests, TestReset_FromValue) { utils::LifetimeTracker tracker; std::vector expected_ops; From ff1ed565b7ee1b25c7c1a76448d33b54da7ad82a Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Thu, 11 Jul 2024 10:37:15 +0800 Subject: [PATCH 2/3] Add more unit tests --- tests/proxy_lifetime_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index 18fd0fc..b4da45b 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -752,8 +752,10 @@ TEST(ProxyLifetimeTests, TestEqualsToNullptr) { int foo = 123; pro::proxy p1; ASSERT_TRUE(p1 == nullptr); + ASSERT_TRUE(nullptr == p1); p1 = &foo; ASSERT_TRUE(p1 != nullptr); + ASSERT_TRUE(nullptr != p1); ASSERT_EQ(ToString(*p1), "123"); } From 4f3aabcecdc15d9445a33a2ae86b80cf79b85fd8 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Thu, 11 Jul 2024 20:50:07 +0800 Subject: [PATCH 3/3] Resolve comments --- proxy.h | 1 + tests/proxy_lifetime_tests.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/proxy.h b/proxy.h index f5824ae..c36d52e 100644 --- a/proxy.h +++ b/proxy.h @@ -704,6 +704,7 @@ class proxy : public details::facade_traits::direct_accessor { } bool has_value() const noexcept { return meta_.has_value(); } + explicit operator bool() const noexcept { return meta_.has_value(); } void reset() noexcept(F::constraints.destructibility >= constraint_level::nothrow) requires(F::constraints.destructibility >= constraint_level::nontrivial) diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index b4da45b..6627167 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -748,6 +748,18 @@ TEST(ProxyLifetimeTests, TestHasValue) { ASSERT_EQ(ToString(*p1), "123"); } +TEST(ProxyLifetimeTests, TestOperatorBool) { + // Implicit conversion to bool shall be prohibited. + static_assert(!std::is_nothrow_convertible_v, bool>); + + int foo = 123; + pro::proxy p1; + ASSERT_FALSE(static_cast(p1)); + p1 = &foo; + ASSERT_TRUE(static_cast(p1)); + ASSERT_EQ(ToString(*p1), "123"); +} + TEST(ProxyLifetimeTests, TestEqualsToNullptr) { int foo = 123; pro::proxy p1;