Fix initial implementation and add first test
This commit is contained in:
1 parent
13c7832c4b
commit
b45e664726
2 files changed
+46
-10
No files matched your search
+16
-8
@@ -33,12 +33,9 @@ class rcp
|
||||
private:
|
||||
T * ptr = nullptr;
|
||||
|
||||
explicit rcp(T * p) : ptr(p)
|
||||
void init_ptr(T * p)
|
||||
{
|
||||
if (p)
|
||||
{
|
||||
ptr->rcp_inc();
|
||||
}
|
||||
ptr = p;
|
||||
}
|
||||
|
||||
friend T;
|
||||
@@ -54,6 +51,16 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
rcp<T> & operator=(const rcp & other)
|
||||
{
|
||||
ptr = other.ptr;
|
||||
if (ptr)
|
||||
{
|
||||
ptr->rcp_inc();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~rcp()
|
||||
{
|
||||
if (ptr)
|
||||
@@ -93,12 +100,12 @@ class root
|
||||
private:
|
||||
mutable std::atomic<int> ref_count{0};
|
||||
|
||||
rcp<root> root_rcp;
|
||||
mutable rcp<root> root_rcp;
|
||||
|
||||
protected:
|
||||
root()
|
||||
{
|
||||
root_rcp = rcp<root>(this);
|
||||
root_rcp.init_ptr(this);
|
||||
}
|
||||
|
||||
virtual ~root() = default;
|
||||
@@ -111,8 +118,9 @@ public:
|
||||
|
||||
void rcp_dec() const
|
||||
{
|
||||
if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 0)
|
||||
if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1)
|
||||
{
|
||||
root_rcp.init_ptr(nullptr);
|
||||
delete this;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user