add RefPtr policy

This commit is contained in:
Nomango 2020-07-25 15:57:14 +08:00
parent 537f38bde1
commit 3ab717be25
4 changed files with 38 additions and 38 deletions

View File

@ -26,8 +26,8 @@ namespace kiwano
{
/// \~chinese
/// @brief 默认的智能指针引用计数代理
struct DefaultRefPtrRefProxy
/// @brief 默认的智能指针引用计数策略
struct DefaultRefPtrPolicy
{
static inline void Retain(RefObject* ptr)
{
@ -45,7 +45,7 @@ struct DefaultRefPtrRefProxy
/// \~chinese
/// @brief 引用计数对象智能指针
template <typename _Ty>
using RefPtr = RefBasePtr<_Ty, DefaultRefPtrRefProxy>;
using RefPtr = RefBasePtr<_Ty, DefaultRefPtrPolicy>;
/// \~chinese
/// @brief 构造引用计数对象智能指针

View File

@ -30,8 +30,8 @@ namespace kiwano
* \~chinese
* @brief ÒýÓüÆÊýÖÇÄÜÖ¸Õë
*/
template <typename _Ty, typename _ProxyTy>
class RefBasePtr
template <typename _Ty, typename _RefPolicy>
class RefBasePtr : protected _RefPolicy
{
public:
using value_type = _Ty;
@ -53,13 +53,13 @@ public:
RefBasePtr(pointer_type p)
: ptr_(p)
{
_ProxyTy::Retain(ptr_);
_RefPolicy::Retain(ptr_);
}
RefBasePtr(const RefBasePtr& other)
: ptr_(other.ptr_)
{
_ProxyTy::Retain(ptr_);
_RefPolicy::Retain(ptr_);
}
RefBasePtr(RefBasePtr&& other) noexcept
@ -74,10 +74,10 @@ public:
}
template <typename _UTy, typename std::enable_if<std::is_convertible<_UTy*, _Ty*>::value, int>::type = 0>
RefBasePtr(const RefBasePtr<_UTy, _ProxyTy>& other)
RefBasePtr(const RefBasePtr<_UTy, _RefPolicy>& other)
{
ptr_ = dynamic_cast<pointer_type>(other.Get());
_ProxyTy::Retain(ptr_);
_RefPolicy::Retain(ptr_);
}
inline pointer_type Get() const noexcept
@ -165,7 +165,7 @@ public:
}
template <typename _UTy, typename std::enable_if<std::is_convertible<_UTy*, _Ty*>::value, int>::type = 0>
inline RefBasePtr& operator=(const RefBasePtr<_UTy, _ProxyTy>& other)
inline RefBasePtr& operator=(const RefBasePtr<_UTy, _RefPolicy>& other)
{
if (other.Get() != ptr_)
RefBasePtr(dynamic_cast<pointer_type>(other.Get())).Swap(*this);
@ -181,7 +181,7 @@ public:
private:
void Tidy()
{
_ProxyTy::Release(ptr_);
_RefPolicy::Release(ptr_);
ptr_ = nullptr;
}
@ -189,76 +189,76 @@ private:
pointer_type ptr_;
};
template <class _Ty, class _UTy, class _ProxyTy>
inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept
template <class _Ty, class _UTy, class _RefPolicy>
inline bool operator==(const RefBasePtr<_Ty, _RefPolicy>& lhs, const RefBasePtr<_UTy, _RefPolicy>& rhs) noexcept
{
return lhs.Get() == rhs.Get();
}
template <class _Ty, class _ProxyTy>
inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator==(const RefBasePtr<_Ty, _RefPolicy>& lhs, _Ty* rhs) noexcept
{
return lhs.Get() == rhs;
}
template <class _Ty, class _ProxyTy>
inline bool operator==(_Ty* lhs, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator==(_Ty* lhs, const RefBasePtr<_Ty, _RefPolicy>& rhs) noexcept
{
return lhs == rhs.Get();
}
template <class _Ty, class _ProxyTy>
inline bool operator==(const RefBasePtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator==(const RefBasePtr<_Ty, _RefPolicy>& lhs, std::nullptr_t) noexcept
{
return !static_cast<bool>(lhs);
}
template <class _Ty, class _ProxyTy>
inline bool operator==(std::nullptr_t, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator==(std::nullptr_t, const RefBasePtr<_Ty, _RefPolicy>& rhs) noexcept
{
return !static_cast<bool>(rhs);
}
template <class _Ty, class _UTy, class _ProxyTy>
inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept
template <class _Ty, class _UTy, class _RefPolicy>
inline bool operator!=(const RefBasePtr<_Ty, _RefPolicy>& lhs, const RefBasePtr<_UTy, _RefPolicy>& rhs) noexcept
{
return !(lhs == rhs);
}
template <class _Ty, class _ProxyTy>
inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, _Ty* rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator!=(const RefBasePtr<_Ty, _RefPolicy>& lhs, _Ty* rhs) noexcept
{
return lhs.Get() != rhs;
}
template <class _Ty, class _ProxyTy>
inline bool operator!=(_Ty* lhs, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator!=(_Ty* lhs, const RefBasePtr<_Ty, _RefPolicy>& rhs) noexcept
{
return lhs != rhs.Get();
}
template <class _Ty, class _ProxyTy>
inline bool operator!=(const RefBasePtr<_Ty, _ProxyTy>& lhs, std::nullptr_t) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator!=(const RefBasePtr<_Ty, _RefPolicy>& lhs, std::nullptr_t) noexcept
{
return static_cast<bool>(lhs);
}
template <class _Ty, class _ProxyTy>
inline bool operator!=(std::nullptr_t, const RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept
template <class _Ty, class _RefPolicy>
inline bool operator!=(std::nullptr_t, const RefBasePtr<_Ty, _RefPolicy>& rhs) noexcept
{
return static_cast<bool>(rhs);
}
template <class _Ty, class _UTy, class _ProxyTy>
inline bool operator<(const RefBasePtr<_Ty, _ProxyTy>& lhs, const RefBasePtr<_UTy, _ProxyTy>& rhs) noexcept
template <class _Ty, class _UTy, class _RefPolicy>
inline bool operator<(const RefBasePtr<_Ty, _RefPolicy>& lhs, const RefBasePtr<_UTy, _RefPolicy>& rhs) noexcept
{
return lhs.Get() < rhs.Get();
}
// template class cannot specialize std::swap,
// so implement a swap function in kiwano namespace
template <class _Ty, class _ProxyTy>
inline void swap(RefBasePtr<_Ty, _ProxyTy>& lhs, RefBasePtr<_Ty, _ProxyTy>& rhs) noexcept
template <class _Ty, class _RefPolicy>
inline void swap(RefBasePtr<_Ty, _RefPolicy>& lhs, RefBasePtr<_Ty, _RefPolicy>& rhs) noexcept
{
lhs.Swap(rhs);
}

View File

@ -26,7 +26,7 @@
namespace kiwano
{
struct ComPtrProxy
struct ComPtrPolicy
{
static inline void Retain(IUnknown* ptr)
{
@ -43,6 +43,6 @@ struct ComPtrProxy
// ComPtr<> is a smart pointer for COM
template <typename _Ty, typename = typename std::enable_if<std::is_base_of<IUnknown, _Ty>::value, int>::type>
using ComPtr = RefBasePtr<_Ty, ComPtrProxy>;
using ComPtr = RefBasePtr<_Ty, ComPtrPolicy>;
} // namespace kiwano

View File

@ -112,7 +112,7 @@ public:
* \~chinese
* @brief
*/
class LogBuffer : public std::streambuf
class KGE_API LogBuffer : public std::streambuf
{
public:
LogBuffer(size_t buffer_size);