Merge pull request #15744 from mogemimi/fix-refptr-warnings

Fix warnings caused by comparison between RefPtr and C++11's nullptr
This commit is contained in:
minggo 2016-05-30 10:45:28 +08:00
commit 67c95549b3
2 changed files with 64 additions and 8 deletions

View File

@ -29,6 +29,7 @@
#include "base/CCRef.h"
#include "base/ccMacros.h"
#include <functional>
#include <type_traits>
NS_CC_BEGIN
@ -212,8 +213,6 @@ public:
inline bool operator > (typename std::remove_const<T>::type * other) const { return _ptr > other; }
inline bool operator > (const std::nullptr_t other) const { return _ptr > other; }
inline bool operator < (const RefPtr<T> & other) const { return _ptr < other._ptr; }
@ -221,8 +220,6 @@ public:
inline bool operator < (typename std::remove_const<T>::type * other) const { return _ptr < other; }
inline bool operator < (const std::nullptr_t other) const { return _ptr < other; }
inline bool operator >= (const RefPtr<T> & other) const { return _ptr >= other._ptr; }
@ -230,8 +227,6 @@ public:
inline bool operator >= (typename std::remove_const<T>::type * other) const { return _ptr >= other; }
inline bool operator >= (const std::nullptr_t other) const { return _ptr >= other; }
inline bool operator <= (const RefPtr<T> & other) const { return _ptr <= other._ptr; }
@ -239,8 +234,6 @@ public:
inline bool operator <= (typename std::remove_const<T>::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<class T> inline
bool operator<(const RefPtr<T>& r, std::nullptr_t)
{
return std::less<T*>()(r.get(), nullptr);
}
template<class T> inline
bool operator<(std::nullptr_t, const RefPtr<T>& r)
{
return std::less<T*>()(nullptr, r.get());
}
template<class T> inline
bool operator>(const RefPtr<T>& r, std::nullptr_t)
{
return nullptr < r;
}
template<class T> inline
bool operator>(std::nullptr_t, const RefPtr<T>& r)
{
return r < nullptr;
}
template<class T> inline
bool operator<=(const RefPtr<T>& r, std::nullptr_t)
{
return !(nullptr < r);
}
template<class T> inline
bool operator<=(std::nullptr_t, const RefPtr<T>& r)
{
return !(r < nullptr);
}
template<class T> inline
bool operator>=(const RefPtr<T>& r, std::nullptr_t)
{
return !(r < nullptr);
}
template<class T> inline
bool operator>=(std::nullptr_t, const RefPtr<T>& r)
{
return !(nullptr < r);
}
/**
* Cast between RefPtr types statically.
*/

View File

@ -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)