From 62328b2c961ba1671b23001c8a29585afa21c10e Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Fri, 12 Sep 2014 15:25:57 +0200 Subject: [PATCH] CSmartPtr: Remove, superseded by std::shared_ptr Signed-off-by: Uli Schlachter --- include/znc/Utils.h | 151 -------------------------------------------- 1 file changed, 151 deletions(-) diff --git a/include/znc/Utils.h b/include/znc/Utils.h index 01499e34..d3c51348 100644 --- a/include/znc/Utils.h +++ b/include/znc/Utils.h @@ -354,155 +354,4 @@ protected: unsigned int m_uTTL; //!< Default time-to-live duration }; -/** - * @class CSmartPtr - * @author prozac - * @brief This is a standard reference counting pointer. Be careful not to have two of these point to the same raw pointer or one will be deleted while the other still thinks it is valid. - */ -template -class CSmartPtr { -public: - /** - * @brief Standard constructor, points to nothing - */ - CSmartPtr() { - m_pType = NULL; - m_puCount = NULL; - } - - /** - * @brief Attach to an existing raw pointer, be CAREFUL not to have more than one CSmartPtr attach to the same raw pointer or bad things will happen - * @param pRawPtr The raw pointer to attach to - */ - CSmartPtr(T* pRawPtr) { - m_pType = NULL; - m_puCount = NULL; - - Attach(pRawPtr); - } - - /** - * @brief Copy constructor, will copy the raw pointer and counter locations and increment the reference counter - * @param CopyFrom A reference of another CSmartPtr to copy from - */ - CSmartPtr(const CSmartPtr& CopyFrom) { - m_pType = NULL; - m_puCount = NULL; - - *this = CopyFrom; - } - - /** - * @brief Destructor will Release() the raw pointer and delete it if this was the last reference - */ - ~CSmartPtr() { - Release(); - } - - // Overloaded operators - T& operator *() const { assert(m_pType); return *m_pType; } - T* operator ->() const { assert(m_pType); return m_pType; } - - /** - * @brief Attach() to a raw pointer - * @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer - * @return Reference to self - */ - CSmartPtr& operator =(T* pRawPtr) { Attach(pRawPtr); return *this; } - - /** - * @brief Copies an existing CSmartPtr adding another reference to the counter - * @param CopyFrom A reference to another CSmartPtr to be copied - * @return Reference to self - */ - CSmartPtr& operator =(const CSmartPtr& CopyFrom) { - if (&CopyFrom != this) { // Check for assignment to self - Release(); // Release the current pointer - - if (CopyFrom.IsNull()) { // If the source raw pointer is null - return *this; // Then just bail out - } - - m_pType = CopyFrom.m_pType; // Make our pointers reference the same raw pointer and counter - m_puCount = CopyFrom.m_puCount; - - assert(m_puCount); // We now point to something valid, so increment the counter - (*m_puCount)++; - } - - return *this; - } - // !Overloaded operators - - /** - * @brief Implicit type conversion to bool for things like if (!ptr) {} and if (ptr) {} - * @return @see IsNull() - */ - operator bool() const { - return !IsNull(); - } - - /** - * @brief Check to see if the underlying raw pointer is null - * @return Whether or not underlying raw pointer is null - */ - bool IsNull() const { - return (m_pType == NULL); - } - - /** - * @brief Attach to a given raw pointer, it will Release() the current raw pointer and assign the new one - * @param pRawPtr The raw pointer to keep track of, ***WARNING*** Do _NOT_ allow more than one CSmartPtr keep track of the same raw pointer - * @return Reference to self - */ - CSmartPtr& Attach(T* pRawPtr) { - if (pRawPtr != m_pType) { // Check for assignment to self - Release(); // Release the current pointer - m_pType = pRawPtr; // Point to the passed raw pointer - - if (m_pType) { // If the passed pointer was valid - m_puCount = new unsigned int(1); // Create a new counter starting at 1 (us) - } - } - - return *this; - } - - /** - * @brief Releases the underlying raw pointer and cleans up if we were the last reference to said pointer - */ - void Release() { - if (m_pType) { // Only release if there is something to be released - assert(m_puCount); - (*m_puCount)--; // Decrement our counter - - if (!*m_puCount) { // If we were the last reference to this pointer, then clean up - delete m_puCount; - delete m_pType; - } - - m_pType = NULL; // Get rid of our references - m_puCount = NULL; - } - } - - // Getters - T* GetPtr() const { return m_pType; } - unsigned int GetCount() const { return (m_puCount) ? *m_puCount : 0; } - // !Getters -private: - T* m_pType; //!< Raw pointer to the class being referenced - unsigned int* m_puCount; //!< Counter of how many CSmartPtr's are referencing the same raw pointer -}; - -template -bool operator ==(T* lhs, const CSmartPtr& rhs) { return (lhs == rhs.GetPtr()); } - -template -bool operator ==(const CSmartPtr& lhs, T* rhs) { return (lhs.GetPtr() == rhs); } - -template -bool operator ==(const CSmartPtr& lhs, const CSmartPtr& rhs) { return (lhs.GetPtr() == rhs.GetPtr()); } - #endif // !_UTILS_H -