Compare commits
No commits in common. "main" and "readme-wip" have entirely different histories.
main
...
readme-wip
@ -81,7 +81,7 @@ public:
|
|||||||
|
|
||||||
rcp & operator=(rcp && other) noexcept
|
rcp & operator=(rcp && other) noexcept
|
||||||
{
|
{
|
||||||
if (this != &other)
|
if (ptr != other.ptr)
|
||||||
{
|
{
|
||||||
if (ptr)
|
if (ptr)
|
||||||
{
|
{
|
||||||
@ -119,11 +119,6 @@ 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;
|
||||||
@ -172,38 +167,6 @@ 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;
|
||||||
@ -269,10 +232,6 @@ 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)
|
||||||
|
|||||||
56
tests.cpp
56
tests.cpp
@ -1,6 +1,5 @@
|
|||||||
#include <rcp.h>
|
#include <rcp.h>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <map>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -152,17 +151,10 @@ void test_listener_self_registration()
|
|||||||
|
|
||||||
void test_copy_assignment_decrements_previous_reference()
|
void test_copy_assignment_decrements_previous_reference()
|
||||||
{
|
{
|
||||||
int constructed_before = mybase_construct;
|
|
||||||
int destructed_before = mybase_destruct;
|
|
||||||
{
|
|
||||||
MyB myb = MyB::create(12, 13);
|
MyB myb = MyB::create(12, 13);
|
||||||
MyB myb2 = MyB::create(14, 15);
|
MyB myb2 = MyB::create(14, 15);
|
||||||
myb = myb2;
|
myb = myb2;
|
||||||
assert(myb->x == 14);
|
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()
|
||||||
@ -198,18 +190,6 @@ 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;
|
||||||
@ -334,39 +314,6 @@ 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);
|
||||||
@ -414,7 +361,6 @@ 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();
|
||||||
@ -422,8 +368,6 @@ 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();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user