diff --git a/include/rcp.h b/include/rcp.h index 2231da8..4a8b86b 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -172,6 +172,38 @@ public: return static_cast(ptr) != static_cast(other.ptr); } + template + bool operator<(const rcp & other) const + { + return std::less()( + static_cast(ptr), + static_cast(other.ptr)); + } + + template + bool operator<=(const rcp & other) const + { + return !std::less()( + static_cast(other.ptr), + static_cast(ptr)); + } + + template + bool operator>(const rcp & other) const + { + return std::less()( + static_cast(other.ptr), + static_cast(ptr)); + } + + template + bool operator>=(const rcp & other) const + { + return !std::less()( + static_cast(ptr), + static_cast(other.ptr)); + } + bool operator==(std::nullptr_t) const { return ptr == nullptr; diff --git a/tests.cpp b/tests.cpp index 9024c84..5c999bd 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -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 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();