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

View File

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

View File

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

View File

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