diff --git a/include/rcp.h b/include/rcp.h index e3bab3b..c7482cc 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -33,16 +33,17 @@ class rcp private: T * ptr = nullptr; - void init_ptr(T * p) - { - ptr = p; - } - - friend T; - public: rcp() = default; + rcp(T * p) : ptr(p) + { + if (p) + { + p->rcp_inc(); + } + } + rcp(const rcp & other) : ptr(other.ptr) { if (ptr) @@ -101,13 +102,6 @@ private: mutable std::atomic ref_count{0}; protected: - mutable rcp root_rcp; - - root() - { - root_rcp.init_ptr(this); - } - virtual ~root() = default; public: @@ -120,7 +114,6 @@ public: { if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) { - root_rcp.init_ptr(nullptr); delete this; } } @@ -129,7 +122,7 @@ public: #define rcp_managed(classname) \ rcp get_rcp() \ { \ - return rcp(root_rcp); \ + return rcp(this); \ } \ template \ static rcp create(Args&&... args) \