• c++ 智能指针


    #ifndef _QSHAREPTR_H
    #define _QSHAREPTR_H
    
    #include "Utility.h"
    namespace Q_COMMON{
    
    template<class T>
    class QSharePtr {
      public:
        class PointerCount {
          private:
            long refCount;
    
            T* objPtr;
    
            QCMutex lock_;
    
    
          public:
            void add_ref();
    
            void release_ref();
    
            long use_count();
    
            PointerCount(T*  p_obj);
    
            ~PointerCount();
    
        };
        
        QSharePtr();
    
        explicit QSharePtr(T*  ptr);
    
        QSharePtr(const QSharePtr<T> & source);
    
        virtual ~QSharePtr();
    
        QSharePtr<T> & operator=(const QSharePtr<T> & source);
    
        int getRefCount();
    
        T* operator->();
        const T* operator->() const;
    
        T& operator*();
    
        inline T* get_pointer() {return p_obj_;}
        inline const T* get_pointer() const { return p_obj_;}
    
      public:
        operator bool() const;
    
        bool operator!() const;
        bool operator<(const QSharePtr<T>& a) const;
    
    
      private:
        PointerCount* p_count_;
    
        T* p_obj_;
    
    };
    template<class T>
    void QSharePtr<T>::PointerCount::add_ref() {
        lock_.lock();
        refCount++;
        lock_.unlock();
    }
    
    template<class T>
    void QSharePtr<T>::PointerCount::release_ref() {
        lock_.lock();
        if(refCount <= 1)
        {   
            //This time is in destructor, so release lock here and do delete this.
            lock_.unlock();
            delete this;   
            return;
        }else
        {
            refCount--;
        }
        lock_.unlock();
    }
    
    template<class T>
    long QSharePtr<T>::PointerCount::use_count() {
        return refCount;
    }
    
    template<class T>
    QSharePtr<T>::PointerCount::PointerCount(T*  p_obj) {
        objPtr = p_obj;
        refCount = 1;
    }
    
    template<class T>
    QSharePtr<T>::PointerCount::~PointerCount() {
        if(objPtr)
        {
            delete objPtr;
        }
    }
    
    template<class T>
    QSharePtr<T>::QSharePtr() {
        p_count_ = new PointerCount(0);
        p_obj_ = 0;
        //ptr_->objPtr = NULL;
        //ptr_->refCount = 0;
    }
    
    template<class T>
    QSharePtr<T>::QSharePtr(T*  ptr) {
        p_count_ = new PointerCount(ptr);
        p_obj_ = ptr;
        //p_count_->refCount = 1;  
        //p_count_->add_ref();
    }
    
    template<class T>
    QSharePtr<T>::QSharePtr(const QSharePtr<T> & source) {
        p_count_ = source.p_count_;    
        p_count_->add_ref();
        p_obj_ = source.p_obj_;
    }
    
    template<class T>
    QSharePtr<T>::~QSharePtr() {
        p_count_->release_ref();
    }
    
    template<class T>
    QSharePtr<T> & QSharePtr<T>::operator=(const QSharePtr<T> & source) {
        if(this == &source)
        {
            return *this;
        }
        p_count_->release_ref();
    
        p_count_ = source.p_count_;
        p_obj_ = source.p_obj_;
        if(p_count_)
        {
            p_count_->add_ref();
        }
        return *this;
    }
    
    template<class T>
    int QSharePtr<T>::getRefCount() {
        return p_count_->use_count();
    }
    
    template<class T>
    T* QSharePtr<T>::operator->() {
        return p_obj_;
    }
    
    template<class T>
    const T* QSharePtr<T>::operator->() const {
        return p_obj_;
    }
    
    template<class T>
    T& QSharePtr<T>::operator*() {
        return *(p_obj_);
    }
    
    template<class T>
    QSharePtr<T>::operator bool() const {
        return (p_obj_ != 0);
    }
    
    template<class T>
    bool QSharePtr<T>::operator!() const {
        return (p_obj_ == 0);
    }
    
    template<class T>
    bool QSharePtr<T>::operator<(const QSharePtr<T>& a) const
    {
        return this->p_obj_ < a.p_obj_;
    }
    
    }
    
    #endif
  • 相关阅读:
    JAVA_NIO 与Netty框架
    Socket编程
    P3368树状数组2(树状数组//改段求点)
    P3373 树状数组1(树状数组//改点求段)
    树状数组
    P1197 星球大战(并查集+链式向前星)
    P2024 食物链(种类并查集||带权并查集)
    P1111 修复公路(kruscal+并查集)
    P1387 最大正方形+P1736 创意吃鱼法(矩形上的dp+预处理)
    P2330 繁忙的城市(krusal最小生成树)
  • 原文地址:https://www.cnblogs.com/kaishan1990/p/5801528.html
Copyright © 2020-2023  润新知