Compare commits

..

4 Commits

2 changed files with 102 additions and 5 deletions

View File

@ -81,7 +81,7 @@ public:
rcp & operator=(rcp && other) noexcept rcp & operator=(rcp && other) noexcept
{ {
if (ptr != other.ptr) if (this != &other)
{ {
if (ptr) if (ptr)
{ {
@ -119,6 +119,11 @@ public:
other.ptr = nullptr; other.ptr = nullptr;
} }
int use_count() const
{
return ptr ? ptr->rcp_count() : 0;
}
void swap(rcp & other) noexcept void swap(rcp & other) noexcept
{ {
T * tmp = ptr; T * tmp = ptr;
@ -167,6 +172,38 @@ public:
return static_cast<const void *>(ptr) != static_cast<const void *>(other.ptr); return static_cast<const void *>(ptr) != static_cast<const void *>(other.ptr);
} }
template <typename U>
bool operator<(const rcp<U> & other) const
{
return std::less<const void *>()(
static_cast<const void *>(ptr),
static_cast<const void *>(other.ptr));
}
template <typename U>
bool operator<=(const rcp<U> & other) const
{
return !std::less<const void *>()(
static_cast<const void *>(other.ptr),
static_cast<const void *>(ptr));
}
template <typename U>
bool operator>(const rcp<U> & other) const
{
return std::less<const void *>()(
static_cast<const void *>(other.ptr),
static_cast<const void *>(ptr));
}
template <typename U>
bool operator>=(const rcp<U> & other) const
{
return !std::less<const void *>()(
static_cast<const void *>(ptr),
static_cast<const void *>(other.ptr));
}
bool operator==(std::nullptr_t) const bool operator==(std::nullptr_t) const
{ {
return ptr == nullptr; return ptr == nullptr;
@ -232,6 +269,10 @@ rcp<T> rcp_dynamic_cast(rcp<U> && other)
delete this; \ delete this; \
} \ } \
} \ } \
int rcp_count() const \
{ \
return ref_count.load(std::memory_order_relaxed); \
} \
private: \ private: \
mutable std::atomic<int> ref_count{0}; \ mutable std::atomic<int> ref_count{0}; \
rcp_managed(classname) rcp_managed(classname)

View File

@ -1,5 +1,6 @@
#include <rcp.h> #include <rcp.h>
#include <cassert> #include <cassert>
#include <map>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
@ -151,10 +152,17 @@ void test_listener_self_registration()
void test_copy_assignment_decrements_previous_reference() void test_copy_assignment_decrements_previous_reference()
{ {
MyB myb = MyB::create(12, 13); int constructed_before = mybase_construct;
MyB myb2 = MyB::create(14, 15); int destructed_before = mybase_destruct;
myb = myb2; {
assert(myb->x == 14); MyB myb = MyB::create(12, 13);
MyB myb2 = MyB::create(14, 15);
myb = myb2;
assert(myb->x == 14);
assert(mybase_destruct == destructed_before + 1);
}
assert(mybase_construct == constructed_before + 2);
assert(mybase_destruct == destructed_before + 2);
} }
void test_move_constructor() void test_move_constructor()
@ -190,6 +198,18 @@ void test_move_assignment()
assert(mybase_destruct == destructed_before + 1); assert(mybase_destruct == destructed_before + 1);
} }
void test_move_assignment_same_object()
{
MyB a = MyB::create(1, 2);
MyB b = a;
assert(a.use_count() == 2);
b = std::move(a);
assert(!a);
assert(b);
assert(b->x == 1);
assert(b.use_count() == 1);
}
void test_move_assignment_releases_existing() void test_move_assignment_releases_existing()
{ {
int constructed_before = mybase_construct; int constructed_before = mybase_construct;
@ -314,6 +334,39 @@ protected:
~Counter() { external_destruct++; } ~Counter() { external_destruct++; }
}; };
void test_ordering()
{
MyB a = MyB::create(1, 2);
MyB b = MyB::create(3, 4);
MyB a2 = a;
assert(!(a < a2));
assert(a <= a2);
assert(!(a > a2));
assert(a >= a2);
assert((a < b) != (b < a));
assert((a < b) == (b > a));
assert((a <= b) == (b >= a));
std::map<MyB, int> m;
m[a] = 1;
m[b] = 2;
assert(m[a2] == 1);
assert(m[b] == 2);
}
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() void test_swap()
{ {
MyB a = MyB::create(1, 2); MyB a = MyB::create(1, 2);
@ -361,6 +414,7 @@ int main(int argc, char * argv[])
test_reset(); test_reset();
test_move_constructor(); test_move_constructor();
test_move_assignment(); test_move_assignment();
test_move_assignment_same_object();
test_move_assignment_releases_existing(); test_move_assignment_releases_existing();
test_upcast(); test_upcast();
test_move_upcast(); test_move_upcast();
@ -368,6 +422,8 @@ int main(int argc, char * argv[])
test_dynamic_cast_failure(); test_dynamic_cast_failure();
test_dynamic_cast_move_success(); test_dynamic_cast_move_success();
test_dynamic_cast_move_failure(); test_dynamic_cast_move_failure();
test_ordering();
test_use_count();
test_swap(); test_swap();
test_hash(); test_hash();
test_external_class(); test_external_class();