From 9d8bf6e810597152eef8906c670b96679af2faec Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 21 Jun 2026 10:43:53 +0100 Subject: [PATCH] memory: fix implicit comparison of unrelated types (#114) --- include/hyprutils/memory/Atomic.hpp | 8 ++++---- include/hyprutils/memory/SharedPtr.hpp | 2 +- include/hyprutils/memory/UniquePtr.hpp | 2 +- include/hyprutils/memory/WeakPtr.hpp | 2 +- tests/memory/Memory.cpp | 18 ++++++++++++++++-- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/include/hyprutils/memory/Atomic.hpp b/include/hyprutils/memory/Atomic.hpp index c8fd033..c43475b 100644 --- a/include/hyprutils/memory/Atomic.hpp +++ b/include/hyprutils/memory/Atomic.hpp @@ -203,8 +203,8 @@ namespace Hyprutils::Memory { return m_ptr.get(); } - operator bool() const { - return m_ptr; + explicit operator bool() const { + return static_cast(m_ptr); } bool operator==(const CAtomicSharedPointer& rhs) const { @@ -366,8 +366,8 @@ namespace Hyprutils::Memory { return m_ptr.get(); } - operator bool() const { - return m_ptr; + explicit operator bool() const { + return static_cast(m_ptr); } bool operator==(const CAtomicWeakPointer& rhs) const { diff --git a/include/hyprutils/memory/SharedPtr.hpp b/include/hyprutils/memory/SharedPtr.hpp index ccf1635..86b2abe 100644 --- a/include/hyprutils/memory/SharedPtr.hpp +++ b/include/hyprutils/memory/SharedPtr.hpp @@ -106,7 +106,7 @@ namespace Hyprutils { return *this; } - operator bool() const { + explicit operator bool() const { return impl_ && impl_->dataNonNull(); } diff --git a/include/hyprutils/memory/UniquePtr.hpp b/include/hyprutils/memory/UniquePtr.hpp index 64f14e2..6db13d0 100644 --- a/include/hyprutils/memory/UniquePtr.hpp +++ b/include/hyprutils/memory/UniquePtr.hpp @@ -67,7 +67,7 @@ namespace Hyprutils { return *this; } - operator bool() const { + explicit operator bool() const { return impl_; } diff --git a/include/hyprutils/memory/WeakPtr.hpp b/include/hyprutils/memory/WeakPtr.hpp index 5d2297e..9372669 100644 --- a/include/hyprutils/memory/WeakPtr.hpp +++ b/include/hyprutils/memory/WeakPtr.hpp @@ -171,7 +171,7 @@ namespace Hyprutils { } /* this returns valid() */ - operator bool() const { + explicit operator bool() const { return valid(); } diff --git a/tests/memory/Memory.cpp b/tests/memory/Memory.cpp index bdbbf50..fc37aed 100644 --- a/tests/memory/Memory.cpp +++ b/tests/memory/Memory.cpp @@ -19,6 +19,20 @@ using namespace Hyprutils::Memory; #define NTHREADS 8 #define ITERATIONS 10000 +namespace { + class Peepee {}; + class Poopoo {}; +} + +template +concept EqualityComparable = requires(A a, B b) { a == b; }; + +static_assert(!EqualityComparable, SP>); +static_assert(!EqualityComparable, WP>); +static_assert(!EqualityComparable, UP>); +static_assert(!EqualityComparable, ASP>); +static_assert(!EqualityComparable, AWP>); + static void testAtomicImpl() { { // Using makeShared here could lead to invalid refcounts. @@ -43,7 +57,7 @@ static void testAtomicImpl() { // Actual count is not incremented in a thread-safe manner here, so we can't check it. // We just want to check that the concurent refcounting doesn't cause any memory corruption. shared.reset(); - EXPECT_EQ(shared, false); + EXPECT_FALSE(shared); } { @@ -73,7 +87,7 @@ static void testAtomicImpl() { EXPECT_EQ(weak.valid(), false); auto shared2 = weak.lock(); - EXPECT_EQ(shared, false); + EXPECT_FALSE(shared); EXPECT_EQ(shared2.get(), nullptr); EXPECT_EQ(shared.strongRef(), 0); EXPECT_EQ(weak.valid(), false);