Add ordering comparison operators

This commit is contained in:
Josh Holtrop 2026-02-25 00:19:04 -05:00
parent d9a9a88a24
commit 17a647b897
2 changed files with 53 additions and 0 deletions

View File

@ -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;

View File

@ -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();