• 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
  • 相关阅读:
    C#中的Excel操作【1】——设置Excel单元格的内容,打开Excel文件的一种方式
    Java 在指定目录建立指定文件名的文件 并输入内容
    JSP基础——属性保存范围和request对象
    JSP基础总结(运行机制、脚本元素、指令元素、动作元素)
    weblogic 清除缓存
    oracle 分区表详解
    ORA-00257: archiver error的解决方法
    oracle中job定时器任务
    timestamp类型在jsp页面输出格式化方法
    cmd中测试常用到的命令汇总
  • 原文地址:https://www.cnblogs.com/kaishan1990/p/5801528.html
Copyright © 2020-2023  润新知