diff --git a/cocos/base/CCRefPtr.h b/cocos/base/CCRefPtr.h index 60f833f1be..a2c19c8630 100644 --- a/cocos/base/CCRefPtr.h +++ b/cocos/base/CCRefPtr.h @@ -29,6 +29,7 @@ #include "base/CCRef.h" #include "base/ccMacros.h" +#include #include NS_CC_BEGIN @@ -212,8 +213,6 @@ public: inline bool operator > (typename std::remove_const::type * other) const { return _ptr > other; } - inline bool operator > (const std::nullptr_t other) const { return _ptr > other; } - inline bool operator < (const RefPtr & other) const { return _ptr < other._ptr; } @@ -221,8 +220,6 @@ public: inline bool operator < (typename std::remove_const::type * other) const { return _ptr < other; } - inline bool operator < (const std::nullptr_t other) const { return _ptr < other; } - inline bool operator >= (const RefPtr & other) const { return _ptr >= other._ptr; } @@ -230,8 +227,6 @@ public: inline bool operator >= (typename std::remove_const::type * other) const { return _ptr >= other; } - inline bool operator >= (const std::nullptr_t other) const { return _ptr >= other; } - inline bool operator <= (const RefPtr & other) const { return _ptr <= other._ptr; } @@ -239,8 +234,6 @@ public: inline bool operator <= (typename std::remove_const::type * other) const { return _ptr <= other; } - inline bool operator <= (const std::nullptr_t other) const { return _ptr <= other; } - inline operator bool() const { return _ptr != nullptr; } @@ -283,6 +276,54 @@ private: Ref * _ptr; }; +template inline +bool operator<(const RefPtr& r, std::nullptr_t) +{ + return std::less()(r.get(), nullptr); +} + +template inline +bool operator<(std::nullptr_t, const RefPtr& r) +{ + return std::less()(nullptr, r.get()); +} + +template inline +bool operator>(const RefPtr& r, std::nullptr_t) +{ + return nullptr < r; +} + +template inline +bool operator>(std::nullptr_t, const RefPtr& r) +{ + return r < nullptr; +} + +template inline +bool operator<=(const RefPtr& r, std::nullptr_t) +{ + return !(nullptr < r); +} + +template inline +bool operator<=(std::nullptr_t, const RefPtr& r) +{ + return !(r < nullptr); +} + +template inline +bool operator>=(const RefPtr& r, std::nullptr_t) +{ + return !(r < nullptr); +} + +template inline +bool operator>=(std::nullptr_t, const RefPtr& r) +{ + return !(nullptr < r); +} + /** * Cast between RefPtr types statically. */ diff --git a/tests/cpp-tests/Classes/UnitTest/RefPtrTest.cpp b/tests/cpp-tests/Classes/UnitTest/RefPtrTest.cpp index f465fe625e..9498937d87 100644 --- a/tests/cpp-tests/Classes/UnitTest/RefPtrTest.cpp +++ b/tests/cpp-tests/Classes/UnitTest/RefPtrTest.cpp @@ -264,6 +264,10 @@ void RefPtrTest::onEnter() CC_ASSERT(false == (ref1 > nullptr)); CC_ASSERT(true == (ref1 <= nullptr)); CC_ASSERT(true == (ref1 >= nullptr)); + CC_ASSERT(false == (nullptr < ref1)); + CC_ASSERT(false == (nullptr > ref1)); + CC_ASSERT(true == (nullptr <= ref1)); + CC_ASSERT(true == (nullptr >= ref1)); CC_ASSERT(false == (ref1 == __String::create("Hello"))); CC_ASSERT(true == (ref1 != __String::create("Hello"))); @@ -280,6 +284,17 @@ void RefPtrTest::onEnter() CC_ASSERT(true == (ref1 > ref2)); CC_ASSERT(false == (ref1 <= ref2)); CC_ASSERT(true == (ref1 >= ref2)); + + CC_ASSERT(false == (ref1 == nullptr)); + CC_ASSERT(true == (ref1 != nullptr)); + CC_ASSERT(false == (ref1 < nullptr)); + CC_ASSERT(true == (ref1 > nullptr)); + CC_ASSERT(false == (ref1 <= nullptr)); + CC_ASSERT(true == (ref1 >= nullptr)); + CC_ASSERT(true == (nullptr < ref1)); + CC_ASSERT(false == (nullptr > ref1)); + CC_ASSERT(true == (nullptr <= ref1)); + CC_ASSERT(false == (nullptr >= ref1)); } // TEST(moveConstructor)