diff options
-rw-r--r-- | common/comptr.h | 20 | ||||
-rw-r--r-- | common/intrusive_ptr.h | 20 |
2 files changed, 20 insertions, 20 deletions
diff --git a/common/comptr.h b/common/comptr.h index ab9d4c53..5984ebd9 100644 --- a/common/comptr.h +++ b/common/comptr.h @@ -4,6 +4,8 @@ #include <cstddef> #include <utility> +#include "opthelpers.h" + template<typename T> class ComPtr { @@ -42,10 +44,13 @@ public: } ComPtr& operator=(ComPtr&& rhs) { - if(mPtr) - mPtr->Release(); - mPtr = rhs.mPtr; - rhs.mPtr = nullptr; + if(likely(&rhs != this)) + { + if(mPtr) + mPtr->Release(); + mPtr = rhs.mPtr; + rhs.mPtr = nullptr; + } return *this; } @@ -56,12 +61,7 @@ public: T* get() const noexcept { return mPtr; } T** getPtr() noexcept { return &mPtr; } - T* release() noexcept - { - T *ret{mPtr}; - mPtr = nullptr; - return ret; - } + T* release() noexcept { return std::exchange(mPtr, nullptr); } void swap(ComPtr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); } void swap(ComPtr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); } diff --git a/common/intrusive_ptr.h b/common/intrusive_ptr.h index e1fc1f7b..31afa70f 100644 --- a/common/intrusive_ptr.h +++ b/common/intrusive_ptr.h @@ -1,6 +1,8 @@ #ifndef INTRUSIVE_PTR_H #define INTRUSIVE_PTR_H +#include <utility> + #include "atomic.h" #include "opthelpers.h" @@ -67,10 +69,13 @@ public: } intrusive_ptr& operator=(intrusive_ptr&& rhs) noexcept { - if(mPtr) - mPtr->release(); - mPtr = rhs.mPtr; - rhs.mPtr = nullptr; + if(likely(&rhs != this)) + { + if(mPtr) + mPtr->release(); + mPtr = rhs.mPtr; + rhs.mPtr = nullptr; + } return *this; } @@ -87,12 +92,7 @@ public: mPtr = ptr; } - T* release() noexcept - { - T *ret{mPtr}; - mPtr = nullptr; - return ret; - } + T* release() noexcept { return std::exchange(mPtr, nullptr); } void swap(intrusive_ptr &rhs) noexcept { std::swap(mPtr, rhs.mPtr); } void swap(intrusive_ptr&& rhs) noexcept { std::swap(mPtr, rhs.mPtr); } |