123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #ifndef __CC_REF_PTR_H__
- #define __CC_REF_PTR_H__
- #include "base/CCRef.h"
- #include "base/ccMacros.h"
- #include <functional>
- #include <type_traits>
- NS_CC_BEGIN
- #define CC_REF_PTR_SAFE_RETAIN(ptr)\
- \
- do\
- {\
- if (ptr)\
- {\
- const_cast<Ref*>(static_cast<const Ref*>(ptr))->retain();\
- }\
- \
- } while (0);
- #define CC_REF_PTR_SAFE_RELEASE(ptr)\
- \
- do\
- {\
- if (ptr)\
- {\
- const_cast<Ref*>(static_cast<const Ref*>(ptr))->release();\
- }\
- \
- } while (0);
- #define CC_REF_PTR_SAFE_RELEASE_NULL(ptr)\
- \
- do\
- {\
- if (ptr)\
- {\
- const_cast<Ref*>(static_cast<const Ref*>(ptr))->release();\
- ptr = nullptr;\
- }\
- \
- } while (0);
- template <typename T> class RefPtr
- {
- public:
-
- RefPtr()
- : _ptr(nullptr)
- {
-
- }
-
- RefPtr(RefPtr<T> && other)
- {
- _ptr = other._ptr;
- other._ptr = nullptr;
- }
- RefPtr(T * ptr)
- : _ptr(ptr)
- {
- CC_REF_PTR_SAFE_RETAIN(_ptr);
- }
-
- RefPtr(std::nullptr_t ptr)
- : _ptr(nullptr)
- {
-
- }
-
- RefPtr(const RefPtr<T> & other)
- : _ptr(other._ptr)
- {
- CC_REF_PTR_SAFE_RETAIN(_ptr);
- }
-
- ~RefPtr()
- {
- CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
- }
-
- RefPtr<T> & operator = (const RefPtr<T> & other)
- {
- if (other._ptr != _ptr)
- {
- CC_REF_PTR_SAFE_RETAIN(other._ptr);
- CC_REF_PTR_SAFE_RELEASE(_ptr);
- _ptr = other._ptr;
- }
-
- return *this;
- }
-
- RefPtr<T> & operator = (RefPtr<T> && other)
- {
- if (&other != this)
- {
- CC_REF_PTR_SAFE_RELEASE(_ptr);
- _ptr = other._ptr;
- other._ptr = nullptr;
- }
-
- return *this;
- }
-
- RefPtr<T> & operator = (T * other)
- {
- if (other != _ptr)
- {
- CC_REF_PTR_SAFE_RETAIN(other);
- CC_REF_PTR_SAFE_RELEASE(_ptr);
- _ptr = other;
- }
-
- return *this;
- }
-
- RefPtr<T> & operator = (std::nullptr_t other)
- {
- CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
- return *this;
- }
-
- operator T * () const { return _ptr; }
-
- T & operator * () const
- {
- CCASSERT(_ptr, "Attempt to dereference a null pointer!");
- return *_ptr;
- }
-
- T * operator->() const
- {
- CCASSERT(_ptr, "Attempt to dereference a null pointer!");
- return _ptr;
- }
-
- T * get() const { return _ptr; }
-
-
- bool operator == (const RefPtr<T> & other) const { return _ptr == other._ptr; }
-
- bool operator == (const T * other) const { return _ptr == other; }
-
- bool operator == (typename std::remove_const<T>::type * other) const { return _ptr == other; }
-
- bool operator == (const std::nullptr_t other) const { return _ptr == other; }
-
-
- bool operator != (const RefPtr<T> & other) const { return _ptr != other._ptr; }
-
- bool operator != (const T * other) const { return _ptr != other; }
-
- bool operator != (typename std::remove_const<T>::type * other) const { return _ptr != other; }
-
- bool operator != (const std::nullptr_t other) const { return _ptr != other; }
-
-
- bool operator > (const RefPtr<T> & other) const { return _ptr > other._ptr; }
-
- bool operator > (const T * other) const { return _ptr > other; }
-
- bool operator > (typename std::remove_const<T>::type * other) const { return _ptr > other; }
-
-
- bool operator < (const RefPtr<T> & other) const { return _ptr < other._ptr; }
-
- bool operator < (const T * other) const { return _ptr < other; }
-
- bool operator < (typename std::remove_const<T>::type * other) const { return _ptr < other; }
-
-
- bool operator >= (const RefPtr<T> & other) const { return _ptr >= other._ptr; }
-
- bool operator >= (const T * other) const { return _ptr >= other; }
-
- bool operator >= (typename std::remove_const<T>::type * other) const { return _ptr >= other; }
-
-
- bool operator <= (const RefPtr<T> & other) const { return _ptr <= other._ptr; }
-
- bool operator <= (const T * other) const { return _ptr <= other; }
-
- bool operator <= (typename std::remove_const<T>::type * other) const { return _ptr <= other; }
-
-
- operator bool() const { return _ptr != nullptr; }
-
- void reset()
- {
- CC_REF_PTR_SAFE_RELEASE_NULL(_ptr);
- }
-
- void swap(RefPtr<T> & other)
- {
- if (&other != this)
- {
- T * tmp = _ptr;
- _ptr = other._ptr;
- other._ptr = tmp;
- }
- }
-
-
- void weakAssign(const RefPtr<T> & other)
- {
- CC_REF_PTR_SAFE_RELEASE(_ptr);
- _ptr = other._ptr;
- }
-
- private:
- T * _ptr;
-
- static_assert(std::is_base_of<Ref, typename std::remove_const<T>::type>::value, "T must be derived from Ref");
- };
- template <class T> inline
- RefPtr<T> makeRef(T *ptr)
- {
- return RefPtr<T>(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.
- */
- template<class T, class U> RefPtr<T> static_pointer_cast(const RefPtr<U> & r)
- {
- return RefPtr<T>(static_cast<T*>(r.get()));
- }
- /**
- * Cast between RefPtr types dynamically.
- */
- template<class T, class U> RefPtr<T> dynamic_pointer_cast(const RefPtr<U> & r)
- {
- return RefPtr<T>(dynamic_cast<T*>(r.get()));
- }
- /**
- * Done with these macros.
- */
- #undef CC_REF_PTR_SAFE_RETAIN
- #undef CC_REF_PTR_SAFE_RELEASE
- #undef CC_REF_PTR_SAFE_RELEASE_NULL
- NS_CC_END
- /// @endcond
- #endif // __CC_REF_PTR_H__
|