Add ordering comparison operators
This commit is contained in:
parent
d9a9a88a24
commit
17a647b897
@ -172,6 +172,38 @@ public:
|
||||
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
|
||||
{
|
||||
return ptr == nullptr;
|
||||
|
||||
21
tests.cpp
21
tests.cpp
@ -1,5 +1,6 @@
|
||||
#include <rcp.h>
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
@ -314,6 +315,25 @@ protected:
|
||||
~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);
|
||||
@ -382,6 +402,7 @@ int main(int argc, char * argv[])
|
||||
test_dynamic_cast_failure();
|
||||
test_dynamic_cast_move_success();
|
||||
test_dynamic_cast_move_failure();
|
||||
test_ordering();
|
||||
test_use_count();
|
||||
test_swap();
|
||||
test_hash();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user