diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-07-23 14:53:27 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2021-07-23 14:53:27 +0100 |
commit | e75e11f87bf6c7136d171fa14cb4b5f9509c52f4 (patch) | |
tree | bc7ce55de230abdd070b9295c555dabc215a6fc3 | |
parent | 22b852043d054e18660c2478cf677a14d114309c (diff) | |
download | rspamd-e75e11f87bf6c7136d171fa14cb4b5f9509c52f4.tar.gz rspamd-e75e11f87bf6c7136d171fa14cb4b5f9509c52f4.zip |
[Minor] Add std::swap specialisation
-rw-r--r-- | src/libutil/cxx/local_shared_ptr.hxx | 17 | ||||
-rw-r--r-- | test/rspamd_cxx_local_ptr.hxx | 26 |
2 files changed, 41 insertions, 2 deletions
diff --git a/src/libutil/cxx/local_shared_ptr.hxx b/src/libutil/cxx/local_shared_ptr.hxx index b9a429d14..81cbe6d08 100644 --- a/src/libutil/cxx/local_shared_ptr.hxx +++ b/src/libutil/cxx/local_shared_ptr.hxx @@ -340,16 +340,29 @@ private: namespace std { template <class T> struct hash<rspamd::local_shared_ptr<T>> { - inline size_t operator()(const rspamd::local_shared_ptr<T> &p) const noexcept { + inline auto operator()(const rspamd::local_shared_ptr<T> &p) const noexcept -> auto { return hash<T *>()(p.get()); } }; template <class T> struct hash<rspamd::local_weak_ptr<T>> { - inline size_t operator()(const rspamd::local_weak_ptr<T> &p) const noexcept { + inline auto operator()(const rspamd::local_weak_ptr<T> &p) const noexcept -> auto { return hash<T *>()(p.get()); } }; + +template<class T> +inline void swap(rspamd::local_shared_ptr<T> &x, rspamd::local_shared_ptr<T> &y) noexcept +{ + x.swap(y); +} + +template<class T> +inline void swap(rspamd::local_weak_ptr<T> &x, rspamd::local_weak_ptr<T> &y) noexcept +{ + x.swap(y); +} + } #endif //RSPAMD_LOCAL_SHARED_PTR_HXX diff --git a/test/rspamd_cxx_local_ptr.hxx b/test/rspamd_cxx_local_ptr.hxx index c04c3f93d..5b0554292 100644 --- a/test/rspamd_cxx_local_ptr.hxx +++ b/test/rspamd_cxx_local_ptr.hxx @@ -279,6 +279,32 @@ TEST_CASE("weak_ptr") { CHECK(t == true); CHECK(wp.expired()); } + +TEST_CASE("std::swap") { + bool t; + + { + rspamd::local_shared_ptr<deleter_test> pi(new deleter_test{t}); + CHECK(pi.use_count() == 1); + CHECK(pi.unique()); + CHECK(t == false); + + rspamd::local_shared_ptr<deleter_test> pi1; + CHECK(pi1.get() == nullptr); + CHECK(pi1.use_count() == 0); + std::swap(pi1, pi); + CHECK(pi.use_count() == 0); + CHECK(pi.get() == nullptr); + CHECK(pi1.get() != nullptr); + std::swap(pi, pi1); + CHECK(pi.use_count() != 0); + CHECK(pi.get() != nullptr); + CHECK(pi1.get() == nullptr); + } + + CHECK(t == true); +} + } #endif //RSPAMD_RSPAMD_CXX_LOCAL_PTR_HXX |