]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Add std::swap specialisation
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jul 2021 13:53:27 +0000 (14:53 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 23 Jul 2021 13:53:27 +0000 (14:53 +0100)
src/libutil/cxx/local_shared_ptr.hxx
test/rspamd_cxx_local_ptr.hxx

index b9a429d148ff1988992f0156406e837c6eff8e4e..81cbe6d085a5806a8ef3653948630a47e7548c48 100644 (file)
@@ -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
index c04c3f93dff375a02c3c2629c1bee795a2fc50eb..5b05542927a73a7c5bde1181666ab7172f21ea90 100644 (file)
@@ -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