Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve API design #128

Merged
merged 3 commits into from
Jul 11, 2024
Merged
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
9 changes: 6 additions & 3 deletions proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ class proxy : public details::facade_traits<F>::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)
Expand Down Expand Up @@ -759,6 +760,11 @@ class proxy : public details::facade_traits<F>::direct_accessor {
auto&& operator*() const&& noexcept requires(_Traits::has_indirection)
{ return std::forward<const typename _Traits::indirect_accessor>(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
mingxwa marked this conversation as resolved.
Show resolved Hide resolved
{ return !lhs.has_value(); }

private:
template <class P, class... Args>
P& initialize(Args&&... args) {
Expand Down Expand Up @@ -821,9 +827,6 @@ template <class R, class F>
const R& proxy_reflect(const proxy<F>& p) noexcept
{ return details::proxy_helper<F>::get_meta(p); }

template <class F>
void swap(proxy<F>& a, proxy<F>& b) noexcept(noexcept(a.swap(b))) { a.swap(b); }

namespace details {

template <class T>
Expand Down
23 changes: 23 additions & 0 deletions tests/proxy_lifetime_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,29 @@ 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<pro::proxy<TestFacade>, bool>);

int foo = 123;
pro::proxy<TestFacade> p1;
ASSERT_FALSE(static_cast<bool>(p1));
p1 = &foo;
ASSERT_TRUE(static_cast<bool>(p1));
ASSERT_EQ(ToString(*p1), "123");
}

TEST(ProxyLifetimeTests, TestEqualsToNullptr) {
int foo = 123;
pro::proxy<TestFacade> p1;
ASSERT_TRUE(p1 == nullptr);
ASSERT_TRUE(nullptr == p1);
p1 = &foo;
ASSERT_TRUE(p1 != nullptr);
ASSERT_TRUE(nullptr != p1);
ASSERT_EQ(ToString(*p1), "123");
}

TEST(ProxyLifetimeTests, TestReset_FromValue) {
utils::LifetimeTracker tracker;
std::vector<utils::LifetimeOperation> expected_ops;
Expand Down
Loading