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
|
|
|
|
{
|
|
|
|
|
2016-12-03 13:32:16 +01:00
|
|
|
struct RefCountable
|
|
|
|
{
|
2017-08-04 06:38:04 +02:00
|
|
|
RefCountable() = default;
|
|
|
|
RefCountable(const RefCountable&) {}
|
|
|
|
RefCountable(RefCountable&&) {}
|
2016-12-03 13:32:16 +01:00
|
|
|
virtual ~RefCountable() = default;
|
2017-08-04 06:38:04 +02:00
|
|
|
|
|
|
|
RefCountable& operator=(const RefCountable&) { return *this; }
|
|
|
|
RefCountable& operator=(RefCountable&&) { return *this; }
|
|
|
|
|
|
|
|
int refcount = 0;
|
2016-12-03 13:32:16 +01:00
|
|
|
};
|
2015-02-23 21:39:56 +01:00
|
|
|
|
2016-12-03 13:32:16 +01:00
|
|
|
struct RefCountablePolicy
|
|
|
|
{
|
2017-01-31 22:45:55 +01:00
|
|
|
static void inc_ref(RefCountable* r, void*) noexcept { ++r->refcount; }
|
2018-02-28 05:04:00 +01:00
|
|
|
static void dec_ref(RefCountable* r, void*) noexcept { if (--r->refcount == 0) delete r; }
|
2016-12-03 13:32:16 +01:00
|
|
|
static void ptr_moved(RefCountable*, void*, void*) noexcept {}
|
|
|
|
};
|
2015-02-23 21:39:56 +01:00
|
|
|
|
2016-12-03 13:32:16 +01:00
|
|
|
template<typename T, typename Policy = RefCountablePolicy>
|
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(); }
|
2018-02-28 05:04:00 +01:00
|
|
|
~RefPtr() noexcept { release(); }
|
2015-02-19 14:58:25 +01:00
|
|
|
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
|
|
|
{
|
2015-11-10 14:50:15 +01:00
|
|
|
if (other.m_ptr != m_ptr)
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
m_ptr = other.m_ptr;
|
|
|
|
acquire();
|
|
|
|
}
|
2015-01-18 19:23:58 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2015-11-10 14:50:15 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-11-07 19:24:08 +01:00
|
|
|
RefPtr& operator=(T* ptr)
|
|
|
|
{
|
2015-11-10 14:50:15 +01:00
|
|
|
if (ptr != m_ptr)
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
m_ptr = ptr;
|
|
|
|
acquire();
|
|
|
|
}
|
2015-11-07 19:24:08 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-11-04 20:47:26 +01:00
|
|
|
[[gnu::always_inline]]
|
2015-01-18 19:23:58 +01:00
|
|
|
T* operator->() const { return m_ptr; }
|
2015-11-04 20:47:26 +01:00
|
|
|
[[gnu::always_inline]]
|
2015-01-18 19:23:58 +01:00
|
|
|
T& operator*() const { return *m_ptr; }
|
|
|
|
|
2015-11-04 20:47:26 +01:00
|
|
|
[[gnu::always_inline]]
|
2015-01-18 19:23:58 +01:00
|
|
|
T* get() const { return m_ptr; }
|
|
|
|
|
2015-11-04 20:47:26 +01:00
|
|
|
[[gnu::always_inline]]
|
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
|
|
|
|
2022-04-12 04:49:19 +02:00
|
|
|
friend bool operator==(const RefPtr& lhs, const RefPtr& rhs) = default;
|
|
|
|
friend bool operator==(const RefPtr& lhs, const T* rhs) { return lhs.m_ptr == rhs; }
|
2015-02-23 21:39:56 +01:00
|
|
|
|
2015-01-18 19:23:58 +01:00
|
|
|
private:
|
|
|
|
T* m_ptr = nullptr;
|
|
|
|
|
2015-02-24 14:43:37 +01:00
|
|
|
[[gnu::always_inline]]
|
2015-01-18 19:23:58 +01:00
|
|
|
void acquire()
|
|
|
|
{
|
|
|
|
if (m_ptr)
|
2016-12-03 13:32:16 +01:00
|
|
|
Policy::inc_ref(m_ptr, this);
|
2015-01-18 19:23:58 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 14:43:37 +01:00
|
|
|
[[gnu::always_inline]]
|
2018-02-28 05:04:00 +01:00
|
|
|
void release() noexcept
|
2015-01-18 19:23:58 +01:00
|
|
|
{
|
|
|
|
if (m_ptr)
|
2016-12-03 13:32:16 +01:00
|
|
|
Policy::dec_ref(m_ptr, this);
|
2015-02-23 21:39:56 +01:00
|
|
|
}
|
|
|
|
|
2015-02-24 14:43:37 +01:00
|
|
|
[[gnu::always_inline]]
|
2015-02-23 21:39:56 +01:00
|
|
|
void moved(void* from)
|
2016-12-03 13:32:16 +01:00
|
|
|
noexcept(noexcept(Policy::ptr_moved(nullptr, nullptr, nullptr)))
|
2015-02-23 21:39:56 +01:00
|
|
|
{
|
|
|
|
if (m_ptr)
|
2016-12-03 13:32:16 +01:00
|
|
|
Policy::ptr_moved(m_ptr, from, this);
|
2015-12-13 00:51:51 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-18 19:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ref_ptr_hh_INCLUDED
|