Drop rcp_internal namespace for now to get working
This commit is contained in:
parent
6fec208922
commit
f5d52dfe98
103
include/rcp.h
103
include/rcp.h
@ -24,58 +24,6 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace rcp_internal
|
||||
{
|
||||
class root
|
||||
{
|
||||
private:
|
||||
mutable std::atomic<int> ref_count{0};
|
||||
|
||||
rcp<root> root_rcp;
|
||||
|
||||
protected:
|
||||
root()
|
||||
{
|
||||
root_rcp = rcp<root>(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<root> & get_root_rcp()
|
||||
{
|
||||
return root_rcp;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class managed
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_base_of<root, T>::value,
|
||||
"T must inherit from rcp::root to use rcp::managed<T>");
|
||||
|
||||
rcp<T> get_rcp()
|
||||
{
|
||||
root * r = (root *)this;
|
||||
return rcp<T>(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<int> ref_count{0};
|
||||
|
||||
using managed = rcp_internal::managed;
|
||||
}
|
||||
rcp<root> root_rcp;
|
||||
|
||||
protected:
|
||||
root()
|
||||
{
|
||||
root_rcp = rcp<root>(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<root> & get_root_rcp()
|
||||
{
|
||||
return root_rcp;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class managed
|
||||
{
|
||||
public:
|
||||
static_assert(std::is_base_of<root, T>::value,
|
||||
"T must inherit from rcp::root to use rcp::managed<T>");
|
||||
|
||||
rcp<T> get_rcp()
|
||||
{
|
||||
root * r = (root *)this;
|
||||
return rcp<T>(r->get_root_rcp());
|
||||
}
|
||||
};
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user