Replace root class with rcp_root() macro

This commit is contained in:
Josh Holtrop 2026-02-23 22:33:09 -05:00
parent a70a0218dc
commit eaad6ba6e0
2 changed files with 18 additions and 23 deletions

View File

@ -96,28 +96,21 @@ public:
} }
}; };
class root #define rcp_root() \
{ public: \
private: void rcp_inc() const \
mutable std::atomic<int> ref_count{0}; { \
ref_count.fetch_add(1, std::memory_order_relaxed); \
protected: } \
virtual ~root() = default; void rcp_dec() const \
{ \
public: if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) \
void rcp_inc() const { \
{ delete this; \
ref_count.fetch_add(1, std::memory_order_relaxed); } \
} } \
private: \
void rcp_dec() const mutable std::atomic<int> ref_count{0}
{
if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1)
{
delete this;
}
}
};
#define rcp_managed(classname) \ #define rcp_managed(classname) \
rcp<classname> get_rcp() \ rcp<classname> get_rcp() \

View File

@ -6,8 +6,10 @@ static int mybase_destruct;
static int myderived_construct; static int myderived_construct;
static int myderived_destruct; static int myderived_destruct;
class MyBase : public root class MyBase
{ {
rcp_root();
protected: protected:
MyBase(int x, int y) MyBase(int x, int y)
{ {