2019-04-11 14:40:54 +08:00
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2019-03-31 01:37:06 +08:00
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining lhs 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:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
#include "../macros.h"
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
template <typename _Ty, typename _Manager, bool _Enable>
|
|
|
|
|
class IntrusivePtr;
|
|
|
|
|
|
|
|
|
|
template <typename _Ty, typename _Manager>
|
|
|
|
|
class IntrusivePtr<_Ty, _Manager, false>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename _Ty, typename _Manager>
|
|
|
|
|
using RealIntrusivePtr = IntrusivePtr<_Ty, _Manager, true>;
|
|
|
|
|
|
|
|
|
|
template <typename _Ty, typename _Manager>
|
|
|
|
|
class IntrusivePtr<_Ty, _Manager, true>
|
|
|
|
|
{
|
|
|
|
|
_Ty* ptr_{ nullptr };
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
using Type = _Ty;
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr() KGE_NOEXCEPT {}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr(nullptr_t) KGE_NOEXCEPT {}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr(Type* p) KGE_NOEXCEPT : ptr_(p)
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
typename _Manager::AddRef(ptr_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr(const IntrusivePtr& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
: ptr_(other.ptr_)
|
|
|
|
|
{
|
|
|
|
|
typename _Manager::AddRef(ptr_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename _UTy>
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr(const RealIntrusivePtr<_UTy, _Manager>& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
: ptr_(other.Get())
|
|
|
|
|
{
|
|
|
|
|
typename _Manager::AddRef(ptr_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
IntrusivePtr(IntrusivePtr&& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
|
other.ptr_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
~IntrusivePtr() KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
typename _Manager::Release(ptr_);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline Type* Get() const KGE_NOEXCEPT { return ptr_; }
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline void Reset() KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
IntrusivePtr{}.Swap(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline void Swap(IntrusivePtr& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
std::swap(ptr_, other.ptr_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Type* operator ->() const
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ASSERT(ptr_ != nullptr && "Invalid pointer");
|
2019-03-31 01:37:06 +08:00
|
|
|
return ptr_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Type& operator *() const
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ASSERT(ptr_ != nullptr && "Invalid pointer");
|
2019-03-31 01:37:06 +08:00
|
|
|
return *ptr_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Type** operator &()
|
|
|
|
|
{
|
2019-04-11 14:40:54 +08:00
|
|
|
KGE_ASSERT(ptr_ == nullptr && "Memory leak");
|
2019-03-31 01:37:06 +08:00
|
|
|
return &ptr_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline operator bool() const KGE_NOEXCEPT { return ptr_ != nullptr; }
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator !() const KGE_NOEXCEPT { return ptr_ == 0; }
|
2019-03-31 01:37:06 +08:00
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline IntrusivePtr& operator =(const IntrusivePtr& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (other.ptr_ != ptr_)
|
|
|
|
|
IntrusivePtr(other).Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline IntrusivePtr& operator =(IntrusivePtr&& other) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
typename _Manager::Release(ptr_);
|
|
|
|
|
ptr_ = other.ptr_;
|
|
|
|
|
other.ptr_ = nullptr;
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline IntrusivePtr& operator =(Type* p) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (p != ptr_)
|
|
|
|
|
IntrusivePtr(p).Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
inline IntrusivePtr& operator =(nullptr_t) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
if (nullptr != ptr_)
|
|
|
|
|
IntrusivePtr{}.Swap(*this);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _UTy, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator==(RealIntrusivePtr<_Ty, _Manager> const& lhs, RealIntrusivePtr<_UTy, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs.Get() == rhs.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _UTy, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator!=(RealIntrusivePtr<_Ty, _Manager> const& lhs, RealIntrusivePtr<_UTy, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs.Get() != rhs.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _UTy, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator<(RealIntrusivePtr<_Ty, _Manager> const& lhs, RealIntrusivePtr<_UTy, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs.Get() < rhs.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator==(RealIntrusivePtr<_Ty, _Manager> const& lhs, _Ty* rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs.Get() == rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator!=(RealIntrusivePtr<_Ty, _Manager> const& lhs, _Ty* rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs.Get() != rhs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator==(_Ty* lhs, RealIntrusivePtr<_Ty, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs == rhs.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator!=(_Ty* lhs, RealIntrusivePtr<_Ty, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return lhs != rhs.Get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator==(RealIntrusivePtr<_Ty, _Manager> const& lhs, nullptr_t) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return !static_cast<bool>(lhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator!=(RealIntrusivePtr<_Ty, _Manager> const& lhs, nullptr_t) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return static_cast<bool>(lhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator==(nullptr_t, RealIntrusivePtr<_Ty, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return !static_cast<bool>(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline bool operator!=(nullptr_t, RealIntrusivePtr<_Ty, _Manager> const& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
return static_cast<bool>(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// template class cannot specialize std::swap,
|
2019-04-11 14:40:54 +08:00
|
|
|
// so implement a swap function in kiwano namespace
|
2019-03-31 01:37:06 +08:00
|
|
|
template <class _Ty, class _Manager>
|
2019-04-11 14:40:54 +08:00
|
|
|
inline void swap(RealIntrusivePtr<_Ty, _Manager>& lhs, RealIntrusivePtr<_Ty, _Manager>& rhs) KGE_NOEXCEPT
|
2019-03-31 01:37:06 +08:00
|
|
|
{
|
|
|
|
|
lhs.Swap(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|