Compare commits
1 Commits
readme-wip
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| d9a9a88a24 |
@ -119,6 +119,11 @@ public:
|
||||
other.ptr = nullptr;
|
||||
}
|
||||
|
||||
int use_count() const
|
||||
{
|
||||
return ptr ? ptr->rcp_count() : 0;
|
||||
}
|
||||
|
||||
void swap(rcp & other) noexcept
|
||||
{
|
||||
T * tmp = ptr;
|
||||
@ -232,6 +237,10 @@ rcp<T> rcp_dynamic_cast(rcp<U> && other)
|
||||
delete this; \
|
||||
} \
|
||||
} \
|
||||
int rcp_count() const \
|
||||
{ \
|
||||
return ref_count.load(std::memory_order_relaxed); \
|
||||
} \
|
||||
private: \
|
||||
mutable std::atomic<int> ref_count{0}; \
|
||||
rcp_managed(classname)
|
||||
|
||||
15
tests.cpp
15
tests.cpp
@ -314,6 +314,20 @@ protected:
|
||||
~Counter() { external_destruct++; }
|
||||
};
|
||||
|
||||
void test_use_count()
|
||||
{
|
||||
MyB a = MyB::create(1, 2);
|
||||
assert(a.use_count() == 1);
|
||||
{
|
||||
MyB b = a;
|
||||
assert(a.use_count() == 2);
|
||||
assert(b.use_count() == 2);
|
||||
}
|
||||
assert(a.use_count() == 1);
|
||||
MyB empty;
|
||||
assert(empty.use_count() == 0);
|
||||
}
|
||||
|
||||
void test_swap()
|
||||
{
|
||||
MyB a = MyB::create(1, 2);
|
||||
@ -368,6 +382,7 @@ int main(int argc, char * argv[])
|
||||
test_dynamic_cast_failure();
|
||||
test_dynamic_cast_move_success();
|
||||
test_dynamic_cast_move_failure();
|
||||
test_use_count();
|
||||
test_swap();
|
||||
test_hash();
|
||||
test_external_class();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user