home/src/ref_ptr.hh

116 lines
2.5 KiB
C++
Raw Normal View History

#ifndef ref_ptr_hh_INCLUDED
#define ref_ptr_hh_INCLUDED
2015-02-23 21:39:56 +01:00
#include <utility>
namespace Kakoune
{
struct RefCountable
{
int refcount = 0;
virtual ~RefCountable() = default;
};
2015-02-23 21:39:56 +01:00
struct RefCountablePolicy
{
2017-01-31 22:45:55 +01:00
static void inc_ref(RefCountable* r, void*) noexcept { ++r->refcount; }
static void dec_ref(RefCountable* r, void*) { if (--r->refcount == 0) delete r; }
static void ptr_moved(RefCountable*, void*, void*) noexcept {}
};
2015-02-23 21:39:56 +01:00
template<typename T, typename Policy = RefCountablePolicy>
struct RefPtr
{
RefPtr() = default;
2015-02-23 21:39:56 +01:00
explicit RefPtr(T* ptr) : m_ptr(ptr) { acquire(); }
~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); }
RefPtr& operator=(const RefPtr& other)
{
if (other.m_ptr != m_ptr)
{
release();
m_ptr = other.m_ptr;
acquire();
}
return *this;
}
RefPtr& operator=(RefPtr&& other)
{
release();
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
2015-02-23 21:39:56 +01:00
moved(&other);
return *this;
}
RefPtr& operator=(T* ptr)
{
if (ptr != m_ptr)
{
release();
m_ptr = ptr;
acquire();
}
return *this;
}
2015-11-04 20:47:26 +01:00
[[gnu::always_inline]]
T* operator->() const { return m_ptr; }
2015-11-04 20:47:26 +01:00
[[gnu::always_inline]]
T& operator*() const { return *m_ptr; }
2015-11-04 20:47:26 +01:00
[[gnu::always_inline]]
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-02-23 21:39:56 +01:00
void reset(T* ptr = nullptr)
{
2015-02-23 21:39:56 +01:00
if (ptr == m_ptr)
return;
release();
m_ptr = ptr;
acquire();
}
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; }
private:
T* m_ptr = nullptr;
[[gnu::always_inline]]
void acquire()
{
if (m_ptr)
Policy::inc_ref(m_ptr, this);
}
[[gnu::always_inline]]
void release()
{
if (m_ptr)
Policy::dec_ref(m_ptr, this);
2015-02-23 21:39:56 +01:00
}
[[gnu::always_inline]]
2015-02-23 21:39:56 +01:00
void moved(void* from)
noexcept(noexcept(Policy::ptr_moved(nullptr, nullptr, nullptr)))
2015-02-23 21:39:56 +01:00
{
if (m_ptr)
Policy::ptr_moved(m_ptr, from, this);
}
};
}
#endif // ref_ptr_hh_INCLUDED