2014-03-27 06:30:15 +08:00
|
|
|
/****************************************************************************
|
2015-03-24 20:23:51 +08:00
|
|
|
Copyright (c) 2014 PlayFirst Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#ifndef __AX_REF_PTR_H__
|
|
|
|
#define __AX_REF_PTR_H__
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @cond DO_NOT_SHOW
|
2014-03-27 06:30:15 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2016-05-28 21:15:56 +08:00
|
|
|
#include <functional>
|
2014-03-27 06:30:15 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-03-27 06:30:15 +08:00
|
|
|
|
|
|
|
/**
|
2014-04-08 07:04:00 +08:00
|
|
|
* Utility/support macros. Defined to enable RefPtr<T> to contain types like 'const T' because we do not
|
|
|
|
* regard retain()/release() as affecting mutability of state.
|
2014-03-27 06:30:15 +08:00
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
#define AX_REF_PTR_SAFE_RETAIN(ptr) \
|
2021-12-25 10:04:45 +08:00
|
|
|
\
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (ptr) \
|
|
|
|
{ \
|
|
|
|
const_cast<Ref*>(static_cast<const Ref*>(ptr))->retain(); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
} while (0);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#define AX_REF_PTR_SAFE_RELEASE(ptr) \
|
2021-12-25 10:04:45 +08:00
|
|
|
\
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (ptr) \
|
|
|
|
{ \
|
|
|
|
const_cast<Ref*>(static_cast<const Ref*>(ptr))->release(); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
} while (0);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#define AX_REF_PTR_SAFE_RELEASE_NULL(ptr) \
|
2021-12-25 10:04:45 +08:00
|
|
|
\
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (ptr) \
|
|
|
|
{ \
|
|
|
|
const_cast<Ref*>(static_cast<const Ref*>(ptr))->release(); \
|
|
|
|
ptr = nullptr; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
} while (0);
|
2014-03-27 06:30:15 +08:00
|
|
|
|
2020-09-11 12:14:40 +08:00
|
|
|
// Tell RefPtr, the object already referenced, no need to retain again
|
2021-12-25 10:04:45 +08:00
|
|
|
template <typename T>
|
|
|
|
struct ReferencedObject
|
|
|
|
{
|
2020-09-11 11:57:55 +08:00
|
|
|
T* _ptr;
|
|
|
|
};
|
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
/**
|
2022-07-11 17:50:21 +08:00
|
|
|
* Wrapper class which maintains a strong reference to a cocos2dx axis::Ref* type object.
|
2014-03-27 06:30:15 +08:00
|
|
|
* Similar in concept to a boost smart pointer.
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
* Enables the use of the RAII idiom with Cocos2dx objects and helps automate some of the more
|
2014-03-27 06:30:15 +08:00
|
|
|
* mundane tasks of pointer initialization and cleanup.
|
|
|
|
*
|
2021-12-25 10:04:45 +08:00
|
|
|
* The class itself is modelled on C++ 11 std::shared_ptr, and trys to keep some of the methods
|
2014-03-27 06:30:15 +08:00
|
|
|
* and functionality consistent with std::shared_ptr.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
template <typename T>
|
|
|
|
class RefPtr
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
RefPtr() : _ptr(nullptr) {}
|
|
|
|
|
|
|
|
RefPtr(RefPtr<T>&& other)
|
2014-04-09 02:34:27 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_ptr = other._ptr;
|
2014-04-09 02:34:27 +08:00
|
|
|
other._ptr = nullptr;
|
|
|
|
}
|
2014-03-27 06:30:15 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
RefPtr(T* ptr) : _ptr(ptr) { AX_REF_PTR_SAFE_RETAIN(_ptr); }
|
2020-09-11 11:57:55 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <typename _Other>
|
|
|
|
RefPtr(ReferencedObject<_Other>&& ptr) : _ptr(ptr._ptr)
|
|
|
|
{}
|
|
|
|
|
|
|
|
RefPtr(std::nullptr_t ptr) : _ptr(nullptr) {}
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
RefPtr(const RefPtr<T>& other) : _ptr(other._ptr) { AX_REF_PTR_SAFE_RETAIN(_ptr); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
~RefPtr() { AX_REF_PTR_SAFE_RELEASE_NULL(_ptr); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
RefPtr<T>& operator=(const RefPtr<T>& other)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
if (other._ptr != _ptr)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_REF_PTR_SAFE_RETAIN(other._ptr);
|
|
|
|
AX_REF_PTR_SAFE_RELEASE(_ptr);
|
2014-03-27 06:30:15 +08:00
|
|
|
_ptr = other._ptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
RefPtr<T>& operator=(RefPtr<T>&& other)
|
2014-04-08 07:04:00 +08:00
|
|
|
{
|
2014-04-09 02:34:27 +08:00
|
|
|
if (&other != this)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_REF_PTR_SAFE_RELEASE(_ptr);
|
2021-12-25 10:04:45 +08:00
|
|
|
_ptr = other._ptr;
|
2014-04-09 02:34:27 +08:00
|
|
|
other._ptr = nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-04-08 07:04:00 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
RefPtr<T>& operator=(T* other)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
if (other != _ptr)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_REF_PTR_SAFE_RETAIN(other);
|
|
|
|
AX_REF_PTR_SAFE_RELEASE(_ptr);
|
2016-06-26 20:39:43 +08:00
|
|
|
_ptr = other;
|
2014-03-27 06:30:15 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
RefPtr<T>& operator=(std::nullptr_t other)
|
2014-04-09 02:34:27 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_REF_PTR_SAFE_RELEASE_NULL(_ptr);
|
2014-04-09 02:34:27 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
operator T*() const { return _ptr; }
|
|
|
|
|
|
|
|
T& operator*() const
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_ptr, "Attempt to dereference a null pointer!");
|
2016-06-26 20:39:43 +08:00
|
|
|
return *_ptr;
|
2014-03-27 06:30:15 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
T* operator->() const
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_ptr, "Attempt to dereference a null pointer!");
|
2016-06-26 20:39:43 +08:00
|
|
|
return _ptr;
|
2014-03-27 06:30:15 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
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; }
|
|
|
|
|
2020-09-11 11:57:55 +08:00
|
|
|
explicit operator bool() const { return _ptr != nullptr; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
void reset() { AX_REF_PTR_SAFE_RELEASE_NULL(_ptr); }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
void swap(RefPtr<T>& other)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
if (&other != this)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
T* tmp = _ptr;
|
|
|
|
_ptr = other._ptr;
|
2014-03-27 06:30:15 +08:00
|
|
|
other._ptr = tmp;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
/**
|
|
|
|
* This function assigns to this RefPtr<T> but does not increase the reference count of the object pointed to.
|
|
|
|
* Useful for assigning an object created through the 'new' operator to a RefPtr<T>. Basically used in scenarios
|
|
|
|
* where the RefPtr<T> has the initial ownership of the object.
|
|
|
|
*
|
|
|
|
* E.G:
|
2022-07-11 17:50:21 +08:00
|
|
|
* RefPtr<axis::Image> image;
|
|
|
|
* image.weakAssign(new axis::Image());
|
2014-03-27 06:30:15 +08:00
|
|
|
*
|
|
|
|
* Instead of:
|
2022-07-11 17:50:21 +08:00
|
|
|
* RefPtr<axis::Image> image;
|
|
|
|
* image = new axis::Image();
|
2014-03-27 06:30:15 +08:00
|
|
|
* image->release(); // Required because new'd object already has a reference count of '1'.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void weakAssign(const RefPtr<T>& other)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_REF_PTR_SAFE_RELEASE(_ptr);
|
2014-03-27 06:30:15 +08:00
|
|
|
_ptr = other._ptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
private:
|
2021-12-25 10:04:45 +08:00
|
|
|
T* _ptr;
|
2016-06-26 20:39:43 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
// NOTE: We can ensure T is derived from axis::Ref at compile time here.
|
2016-06-26 20:39:43 +08:00
|
|
|
static_assert(std::is_base_of<Ref, typename std::remove_const<T>::type>::value, "T must be derived from Ref");
|
2014-03-27 06:30:15 +08:00
|
|
|
};
|
2017-02-08 10:00:08 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline RefPtr<T> makeRef(T* ptr)
|
2017-02-08 10:00:08 +08:00
|
|
|
{
|
|
|
|
return RefPtr<T>(ptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator<(const RefPtr<T>& r, std::nullptr_t)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return std::less<T*>()(r.get(), nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator<(std::nullptr_t, const RefPtr<T>& r)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return std::less<T*>()(nullptr, r.get());
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator>(const RefPtr<T>& r, std::nullptr_t)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return nullptr < r;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator>(std::nullptr_t, const RefPtr<T>& r)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return r < nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator<=(const RefPtr<T>& r, std::nullptr_t)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return !(nullptr < r);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator<=(std::nullptr_t, const RefPtr<T>& r)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return !(r < nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator>=(const RefPtr<T>& r, std::nullptr_t)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return !(r < nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T>
|
|
|
|
inline bool operator>=(std::nullptr_t, const RefPtr<T>& r)
|
2016-05-28 21:15:56 +08:00
|
|
|
{
|
|
|
|
return !(nullptr < r);
|
|
|
|
}
|
|
|
|
|
2014-03-27 06:30:15 +08:00
|
|
|
/**
|
|
|
|
* Cast between RefPtr types statically.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T, class U>
|
|
|
|
RefPtr<T> static_pointer_cast(const RefPtr<U>& r)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
return RefPtr<T>(static_cast<T*>(r.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cast between RefPtr types dynamically.
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
template <class T, class U>
|
|
|
|
RefPtr<T> dynamic_pointer_cast(const RefPtr<U>& r)
|
2014-03-27 06:30:15 +08:00
|
|
|
{
|
|
|
|
return RefPtr<T>(dynamic_cast<T*>(r.get()));
|
|
|
|
}
|
|
|
|
|
2014-04-08 07:04:00 +08:00
|
|
|
/**
|
|
|
|
* Done with these macros.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
#undef AX_REF_PTR_SAFE_RETAIN
|
|
|
|
#undef AX_REF_PTR_SAFE_RELEASE
|
|
|
|
#undef AX_REF_PTR_SAFE_RELEASE_NULL
|
2014-04-08 07:04:00 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2014-03-27 06:30:15 +08:00
|
|
|
|
2015-03-24 20:23:51 +08:00
|
|
|
/// @endcond
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // __AX_REF_PTR_H__
|