diff --git a/include/rcp.h b/include/rcp.h index 3d5a4df..68334f7 100644 --- a/include/rcp.h +++ b/include/rcp.h @@ -23,6 +23,7 @@ #pragma once #include +#include #include /** @@ -118,6 +119,13 @@ public: other.ptr = nullptr; } + void swap(rcp & other) noexcept + { + T * tmp = ptr; + ptr = other.ptr; + other.ptr = tmp; + } + void reset() { if (ptr) @@ -179,6 +187,18 @@ public: friend rcp rcp_dynamic_cast(rcp && other); }; +namespace std +{ + template + struct hash> + { + size_t operator()(const rcp & p) const noexcept + { + return hash()(static_cast(p.get_raw())); + } + }; +} + template rcp rcp_dynamic_cast(const rcp & other) { diff --git a/tests.cpp b/tests.cpp index ff0a1b9..6395536 100644 --- a/tests.cpp +++ b/tests.cpp @@ -1,5 +1,6 @@ #include #include +#include #include static int mybase_construct; @@ -313,6 +314,26 @@ protected: ~Counter() { external_destruct++; } }; +void test_swap() +{ + MyB a = MyB::create(1, 2); + MyB b = MyB::create(3, 4); + a.swap(b); + assert(a->x == 3); + assert(b->x == 1); +} + +void test_hash() +{ + MyB a = MyB::create(1, 2); + MyB b = a; + std::unordered_map map; + map[a] = 42; + assert(map[b] == 42); + MyB c = MyB::create(3, 4); + assert(map.find(c) == map.end()); +} + void test_external_class() { int before = external_destruct; @@ -347,6 +368,8 @@ int main(int argc, char * argv[]) test_dynamic_cast_failure(); test_dynamic_cast_move_success(); test_dynamic_cast_move_failure(); + test_swap(); + test_hash(); test_external_class(); return 0; }