From 579bd4d76b457018626d96576f19cfd30ab95016 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Sun, 12 Jun 2016 13:01:04 +0300 Subject: [PATCH] Implement move semantics for Sqrat smart pointers. --- include/sqrat/sqratUtil.h | 196 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) diff --git a/include/sqrat/sqratUtil.h b/include/sqrat/sqratUtil.h index 1a754ed5..c5465db4 100644 --- a/include/sqrat/sqratUtil.h +++ b/include/sqrat/sqratUtil.h @@ -601,6 +601,45 @@ public: } } +#ifdef SCRAT_USE_CXX11_OPTIMIZATIONS + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Move constructor + /// + /// \param other SharedPtr to move + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + SharedPtr(SharedPtr&& other) + : m_Ptr(other.m_Ptr) + , m_RefCount(other.m_RefCount) + , m_RefCountRefCount(other.m_RefCountRefCount) + { + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Move constructor + /// + /// \param other SharedPtr to move + /// + /// \tparam U Type of pointer (usually doesnt need to be defined explicitly) + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + SharedPtr(SharedPtr&& other) + : m_Ptr(static_cast(other.m_Ptr)) + , m_RefCount(other.m_RefCount) + , m_RefCountRefCount(other.m_RefCountRefCount) + { + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + +#endif // SCRAT_USE_CXX11_OPTIMIZATIONS + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Copy constructor /// @@ -694,6 +733,65 @@ public: return *this; } +#ifdef SCRAT_USE_CXX11_OPTIMIZATIONS + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Assigns the SharedPtr + /// + /// \param other SharedPtr to move + /// + /// \return The SharedPtr itself + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + SharedPtr& operator=(SharedPtr&& other) + { + if (m_Ptr != other.m_Ptr) + { + Reset(); + + m_Ptr = other.m_Ptr; + m_RefCount = other.m_RefCount; + m_RefCountRefCount = other.m_RefCountRefCount; + + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + return *this; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Assigns the SharedPtr + /// + /// \param other SharedPtr to move + /// + /// \tparam U Type of pointer (usually doesnt need to be defined explicitly) + /// + /// \return The SharedPtr itself + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + SharedPtr& operator=(SharedPtr&& other) + { + if (m_Ptr != static_cast(other.m_Ptr)) + { + Reset(); + + m_Ptr = static_cast(other.m_Ptr); + m_RefCount = other.m_RefCount; + m_RefCountRefCount = other.m_RefCountRefCount; + + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + return *this; + } + +#endif // SCRAT_USE_CXX11_OPTIMIZATIONS + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Sets up a new object to be managed by the SharedPtr /// @@ -1027,6 +1125,45 @@ public: } } +#ifdef SCRAT_USE_CXX11_OPTIMIZATIONS + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Move constructor + /// + /// \param other WeakPtr to move + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + WeakPtr(WeakPtr&& other) + : m_Ptr(other.m_Ptr) + , m_RefCount(other.m_RefCount) + , m_RefCountRefCount(other.m_RefCountRefCount) + { + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Move constructor + /// + /// \param other WeakPtr to move + /// + /// \tparam U Type of pointer (usually doesnt need to be defined explicitly) + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + WeakPtr(WeakPtr&& other) + : m_Ptr(static_cast(other.m_Ptr)) + , m_RefCount(other.m_RefCount) + , m_RefCountRefCount(other.m_RefCountRefCount) + { + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + +#endif // SCRAT_USE_CXX11_OPTIMIZATIONS + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Copy constructor /// @@ -1117,6 +1254,65 @@ public: return *this; } +#ifdef SCRAT_USE_CXX11_OPTIMIZATIONS + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Assigns the WeakPtr + /// + /// \param other WeakPtr to move + /// + /// \return The WeakPtr itself + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + WeakPtr& operator=(WeakPtr&& other) + { + if (m_Ptr != other.m_Ptr) + { + Reset(); + + m_Ptr = other.m_Ptr; + m_RefCount = other.m_RefCount; + m_RefCountRefCount = other.m_RefCountRefCount; + + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + return *this; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// Assigns the WeakPtr + /// + /// \param other WeakPtr to move + /// + /// \tparam U Type of pointer (usually doesnt need to be defined explicitly) + /// + /// \return The WeakPtr itself + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + template + WeakPtr& operator=(WeakPtr&& other) + { + if (m_Ptr != static_cast(other.m_Ptr)) + { + Reset(); + + m_Ptr = static_cast(other.m_Ptr); + m_RefCount = other.m_RefCount; + m_RefCountRefCount = other.m_RefCountRefCount; + + other.m_Ptr = NULL; + other.m_RefCount = NULL; + other.m_RefCountRefCount = NULL; + } + + return *this; + } + +#endif // SCRAT_USE_CXX11_OPTIMIZATIONS + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// Assigns the WeakPtr ///