Fix initial implementation and add first test
This commit is contained in:
parent
13c7832c4b
commit
b45e664726
@ -33,12 +33,9 @@ class rcp
|
|||||||
private:
|
private:
|
||||||
T * ptr = nullptr;
|
T * ptr = nullptr;
|
||||||
|
|
||||||
explicit rcp(T * p) : ptr(p)
|
void init_ptr(T * p)
|
||||||
{
|
{
|
||||||
if (p)
|
ptr = p;
|
||||||
{
|
|
||||||
ptr->rcp_inc();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
friend T;
|
friend T;
|
||||||
@ -54,6 +51,16 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rcp<T> & operator=(const rcp & other)
|
||||||
|
{
|
||||||
|
ptr = other.ptr;
|
||||||
|
if (ptr)
|
||||||
|
{
|
||||||
|
ptr->rcp_inc();
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
~rcp()
|
~rcp()
|
||||||
{
|
{
|
||||||
if (ptr)
|
if (ptr)
|
||||||
@ -93,12 +100,12 @@ class root
|
|||||||
private:
|
private:
|
||||||
mutable std::atomic<int> ref_count{0};
|
mutable std::atomic<int> ref_count{0};
|
||||||
|
|
||||||
rcp<root> root_rcp;
|
mutable rcp<root> root_rcp;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
root()
|
root()
|
||||||
{
|
{
|
||||||
root_rcp = rcp<root>(this);
|
root_rcp.init_ptr(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~root() = default;
|
virtual ~root() = default;
|
||||||
@ -111,8 +118,9 @@ public:
|
|||||||
|
|
||||||
void rcp_dec() const
|
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;
|
delete this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,22 @@
|
|||||||
#include <rcp.h>
|
#include <rcp.h>
|
||||||
|
|
||||||
|
static int mybase_construct;
|
||||||
|
static int mybase_destroy;
|
||||||
|
static int myderived_construct;
|
||||||
|
static int myderived_destroy;
|
||||||
|
|
||||||
class MyBase : public root
|
class MyBase : public root
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
MyBase(int x, int y) {}
|
MyBase(int x, int y)
|
||||||
|
{
|
||||||
|
mybase_construct++;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~MyBase()
|
||||||
|
{
|
||||||
|
mybase_destroy++;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
rcp_managed(MyBase);
|
rcp_managed(MyBase);
|
||||||
@ -12,13 +25,28 @@ public:
|
|||||||
class MyDerived : public MyBase
|
class MyDerived : public MyBase
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
MyDerived(double v) : MyBase(1, 2) {}
|
MyDerived(double v) : MyBase(1, 2)
|
||||||
|
{
|
||||||
|
myderived_construct++;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~MyDerived()
|
||||||
|
{
|
||||||
|
myderived_destroy++;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
rcp_managed(MyDerived)
|
rcp_managed(MyDerived)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void t1()
|
||||||
|
{
|
||||||
|
rcp<MyBase> mybase = MyBase::create(4, 5);
|
||||||
|
rcp<MyDerived> myderived = MyDerived::create(42.5);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
|
t1();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user