diff --git a/include/rcp.h b/include/rcp.h index f206e8e..b439666 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -24,58 +24,6 @@ #include -namespace rcp_internal -{ - class root - { - private: - mutable std::atomic ref_count{0}; - - rcp root_rcp; - - protected: - root() - { - root_rcp = rcp(this); - } - - virtual ~root() = default; - - public: - void rcp_inc() const - { - ref_count.fetch_add(1, std::memory_order_relaxed); - } - - void rcp_dec() const - { - if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 0) - { - delete this; - } - } - - rcp & get_root_rcp() - { - return root_rcp; - } - } - - template - class managed - { - public: - static_assert(std::is_base_of::value, - "T must inherit from rcp::root to use rcp::managed"); - - rcp get_rcp() - { - root * r = (root *)this; - return rcp(r->get_root_ref()); - } - } -} - /** * Reference-counting pointer. */ @@ -138,8 +86,53 @@ public: { return !ptr; } +}; - using root = rcp_internal::root; +class root +{ +private: + mutable std::atomic ref_count{0}; - using managed = rcp_internal::managed; -} + rcp root_rcp; + +protected: + root() + { + root_rcp = rcp(this); + } + + virtual ~root() = default; + +public: + void rcp_inc() const + { + ref_count.fetch_add(1, std::memory_order_relaxed); + } + + void rcp_dec() const + { + if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 0) + { + delete this; + } + } + + rcp & get_root_rcp() + { + return root_rcp; + } +}; + +template +class managed +{ +public: + static_assert(std::is_base_of::value, + "T must inherit from rcp::root to use rcp::managed"); + + rcp get_rcp() + { + root * r = (root *)this; + return rcp(r->get_root_rcp()); + } +}; diff --git a/test/Makefile b/test/Makefile index 0519cc3..b9d237c 100644 --- a/test/Makefile +++ b/test/Makefile @@ -3,9 +3,10 @@ INCLUDE := ../include .PHONY: all all: build/tests build/tests - valgrind build/tests + valgrind -q build/tests build/tests: tests.cpp $(INCLUDE)/rcp.h + mkdir -p $$(dirname $@) $(CC) -std=c++20 -g -Wall -I$(INCLUDE) -o $@ $< .PHONY: clean