2022-12-13 13:40:54 +02:00
|
|
|
#pragma once
|
|
|
|
|
2023-01-30 18:54:37 +02:00
|
|
|
#include <BAN/Errors.h>
|
|
|
|
#include <BAN/Move.h>
|
|
|
|
#include <BAN/NoCopyMove.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-12-13 13:40:54 +02:00
|
|
|
namespace BAN
|
|
|
|
{
|
2023-01-10 17:51:33 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2023-03-08 03:21:30 +02:00
|
|
|
class RefCounted
|
2023-01-10 17:51:33 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
BAN_NON_COPYABLE(RefCounted);
|
|
|
|
BAN_NON_MOVABLE(RefCounted);
|
2023-01-30 18:54:37 +02:00
|
|
|
|
2023-01-10 17:51:33 +02:00
|
|
|
public:
|
2023-03-08 03:21:30 +02:00
|
|
|
uint32_t ref_count() const
|
2023-01-10 17:51:33 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
return m_ref_count;
|
2023-01-10 17:51:33 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
void ref() const
|
2023-01-10 17:51:33 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
ASSERT(m_ref_count > 0);
|
|
|
|
m_ref_count++;
|
2023-01-10 17:51:33 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
void unref() const
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
ASSERT(m_ref_count > 0);
|
|
|
|
m_ref_count--;
|
|
|
|
if (m_ref_count == 0)
|
|
|
|
delete (const T*)this;
|
2023-01-30 18:54:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
protected:
|
|
|
|
RefCounted() = default;
|
|
|
|
~RefCounted() { ASSERT(m_ref_count == 0); }
|
|
|
|
|
2023-01-10 17:51:33 +02:00
|
|
|
private:
|
2023-03-08 03:21:30 +02:00
|
|
|
mutable uint32_t m_ref_count = 1;
|
2023-01-10 17:51:33 +02:00
|
|
|
};
|
|
|
|
|
2023-01-30 18:54:37 +02:00
|
|
|
template<typename T>
|
2023-03-08 03:21:30 +02:00
|
|
|
class RefPtr
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
|
|
|
public:
|
2023-03-08 03:21:30 +02:00
|
|
|
RefPtr() = default;
|
2023-03-16 12:17:04 +02:00
|
|
|
RefPtr(T* pointer)
|
|
|
|
{
|
|
|
|
m_pointer = pointer;
|
|
|
|
if (m_pointer)
|
|
|
|
m_pointer->ref();
|
|
|
|
}
|
2023-03-08 03:21:30 +02:00
|
|
|
~RefPtr() { clear(); }
|
|
|
|
|
2023-03-02 12:30:11 +02:00
|
|
|
template<typename U>
|
2023-03-08 03:21:30 +02:00
|
|
|
static RefPtr adopt(U* pointer)
|
2023-03-02 12:30:11 +02:00
|
|
|
{
|
2023-03-16 12:17:04 +02:00
|
|
|
RefPtr ptr;
|
|
|
|
ptr.m_pointer = pointer;
|
|
|
|
return ptr;
|
2023-03-02 12:30:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-30 18:54:37 +02:00
|
|
|
template<typename... Args>
|
2023-03-08 03:21:30 +02:00
|
|
|
static ErrorOr<RefPtr> create(Args&&... args)
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
T* pointer = new T(forward<Args>(args)...);
|
|
|
|
if (pointer == nullptr)
|
2023-03-02 21:10:44 +02:00
|
|
|
return Error::from_errno(ENOMEM);
|
2023-03-16 12:17:04 +02:00
|
|
|
return adopt(pointer);
|
2023-01-30 18:54:37 +02:00
|
|
|
}
|
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
RefPtr(const RefPtr& other) { *this = other; }
|
|
|
|
RefPtr(RefPtr&& other) { *this = move(other); }
|
2023-07-10 09:41:39 +03:00
|
|
|
template<typename U>
|
|
|
|
RefPtr(const RefPtr<U>& other) { *this = other; }
|
|
|
|
template<typename U>
|
|
|
|
RefPtr(RefPtr<U>&& other) { *this = move(other); }
|
2023-03-08 03:21:30 +02:00
|
|
|
|
|
|
|
RefPtr& operator=(const RefPtr& other)
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
2023-03-02 12:30:11 +02:00
|
|
|
clear();
|
2023-03-08 03:21:30 +02:00
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
if (m_pointer)
|
|
|
|
m_pointer->ref();
|
2023-01-30 18:54:37 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2023-03-08 03:21:30 +02:00
|
|
|
|
|
|
|
RefPtr& operator=(RefPtr&& other)
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
2023-03-02 12:30:11 +02:00
|
|
|
clear();
|
2023-03-08 03:21:30 +02:00
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
other.m_pointer = nullptr;
|
2023-01-30 18:54:37 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-07-10 09:41:39 +03:00
|
|
|
template<typename U>
|
|
|
|
RefPtr& operator=(const RefPtr<U>& other)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
if (m_pointer)
|
|
|
|
m_pointer->ref();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
RefPtr& operator=(RefPtr<U>&& other)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
m_pointer = other.m_pointer;
|
|
|
|
other.m_pointer = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
T* ptr() { ASSERT(!empty()); return m_pointer; }
|
|
|
|
const T* ptr() const { ASSERT(!empty()); return m_pointer; }
|
2023-03-02 12:30:11 +02:00
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
T& operator*() { return *ptr(); }
|
|
|
|
const T& operator*() const { return *ptr(); }
|
2023-01-30 18:54:37 +02:00
|
|
|
|
2023-03-02 12:30:11 +02:00
|
|
|
T* operator->() { return ptr(); }
|
|
|
|
const T* operator->() const { return ptr(); }
|
2023-01-30 18:54:37 +02:00
|
|
|
|
2023-03-08 03:21:30 +02:00
|
|
|
bool empty() const { return m_pointer == nullptr; }
|
|
|
|
operator bool() const { return m_pointer; }
|
|
|
|
|
2023-03-02 12:30:11 +02:00
|
|
|
void clear()
|
2023-01-30 18:54:37 +02:00
|
|
|
{
|
2023-03-08 03:21:30 +02:00
|
|
|
if (m_pointer)
|
|
|
|
m_pointer->unref();
|
2023-01-30 18:54:37 +02:00
|
|
|
m_pointer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-03-08 03:21:30 +02:00
|
|
|
T* m_pointer = nullptr;
|
2023-07-10 09:41:39 +03:00
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
friend class RefPtr;
|
2023-01-30 18:54:37 +02:00
|
|
|
};
|
|
|
|
|
2023-04-10 21:03:41 +03:00
|
|
|
}
|