2015-01-18 19:23:58 +01:00
|
|
|
#ifndef ref_ptr_hh_INCLUDED
|
|
|
|
#define ref_ptr_hh_INCLUDED
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2015-01-18 19:23:58 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
struct WorstMatch { [[gnu::always_inline]] WorstMatch(...) {} };
|
|
|
|
|
|
|
|
[[gnu::always_inline]]
|
|
|
|
inline void ref_ptr_moved(WorstMatch, void*, void*) noexcept {}
|
|
|
|
|
|
|
|
template<typename T, typename TForOverload = T>
|
2015-02-19 14:58:25 +01:00
|
|
|
struct RefPtr
|
2015-01-18 19:23:58 +01:00
|
|
|
{
|
2015-02-19 14:58:25 +01:00
|
|
|
RefPtr() = default;
|
2015-02-23 21:39:56 +01:00
|
|
|
explicit RefPtr(T* ptr) : m_ptr(ptr) { acquire(); }
|
2015-02-19 14:58:25 +01:00
|
|
|
~RefPtr() { release(); }
|
|
|
|
RefPtr(const RefPtr& other) : m_ptr(other.m_ptr) { acquire(); }
|
2015-02-23 21:39:56 +01:00
|
|
|
RefPtr(RefPtr&& other)
|
|
|
|
noexcept(noexcept(std::declval<RefPtr>().moved(nullptr)))
|
|
|
|
: m_ptr(other.m_ptr) { other.m_ptr = nullptr; moved(&other); }
|
2015-01-18 19:23:58 +01:00
|
|
|
|
2015-02-19 14:58:25 +01:00
|
|
|
RefPtr& operator=(const RefPtr& other)
|
2015-01-18 19:23:58 +01:00
|
|
|
{
|
|
|
|
release();
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
acquire();
|
|
|
|
return *this;
|
|
|
|
}
|
2015-02-19 14:58:25 +01:00
|
|
|
RefPtr& operator=(RefPtr&& other)
|
2015-01-18 19:23:58 +01:00
|
|
|
{
|
|
|
|
release();
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
other.m_ptr = nullptr;
|
2015-02-23 21:39:56 +01:00
|
|
|
moved(&other);
|
2015-01-18 19:23:58 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() const { return m_ptr; }
|
|
|
|
T& operator*() const { return *m_ptr; }
|
|
|
|
|
|
|
|
T* get() const { return m_ptr; }
|
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
explicit operator bool() const { return m_ptr; }
|
2015-01-18 19:23:58 +01:00
|
|
|
|
2015-02-23 21:39:56 +01:00
|
|
|
void reset(T* ptr = nullptr)
|
2015-01-18 19:23:58 +01:00
|
|
|
{
|
2015-02-23 21:39:56 +01:00
|
|
|
if (ptr == m_ptr)
|
|
|
|
return;
|
|
|
|
release();
|
|
|
|
m_ptr = ptr;
|
|
|
|
acquire();
|
2015-01-18 19:23:58 +01:00
|
|
|
}
|
2015-02-23 21:39:56 +01:00
|
|
|
|
|
|
|
friend bool operator==(const RefPtr& lhs, const RefPtr& rhs) { return lhs.m_ptr == rhs.m_ptr; }
|
|
|
|
friend bool operator!=(const RefPtr& lhs, const RefPtr& rhs) { return lhs.m_ptr != rhs.m_ptr; }
|
|
|
|
|
2015-01-18 19:23:58 +01:00
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
|
|
|
|
void acquire()
|
|
|
|
{
|
|
|
|
if (m_ptr)
|
2015-02-23 21:39:56 +01:00
|
|
|
inc_ref_count(static_cast<TForOverload*>(m_ptr), this);
|
2015-01-18 19:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void release()
|
|
|
|
{
|
|
|
|
if (m_ptr)
|
2015-02-23 21:39:56 +01:00
|
|
|
dec_ref_count(static_cast<TForOverload*>(m_ptr), this);
|
2015-01-18 19:23:58 +01:00
|
|
|
m_ptr = nullptr;
|
2015-02-23 21:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void moved(void* from)
|
|
|
|
noexcept(noexcept(ref_ptr_moved(static_cast<TForOverload*>(nullptr), nullptr, nullptr)))
|
|
|
|
{
|
|
|
|
if (m_ptr)
|
|
|
|
ref_ptr_moved(static_cast<TForOverload*>(m_ptr), from, this);
|
|
|
|
}
|
2015-01-18 19:23:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ref_ptr_hh_INCLUDED
|