- //原子操作
- class Atom
- {
- public:
- //自增
- inline static int Inc(int * var)
- {
- #ifdef WIN32
- return _InterlockedIncrement((long volatile *)(var));
- #else
- return __sync_add_and_fetch(var, 1);
- #endif
- }
- //自减
- inline static int Dec(int * var)
- {
- #ifdef WIN32
- return _InterlockedDecrement((long volatile *)(var));
- #else
- return __sync_add_and_fetch(var, -1);
- #endif
- }
- //加一个值
- inline static int Add(int * var, const int value)
- {
- #ifdef WIN32
- return _InterlockedExchangeAdd((long volatile *)(var),(long)value);
- #else
- return __sync_fetch_and_add(var, value);
- #endif
- }
- //赋值
- inline static void Set(int * var, const int value)
- {
- #ifdef WIN32
- _InterlockedExchange((long volatile *)(var), (long)value);
- #else
- __sync_lock_test_and_set(var, value);
- #endif
- }
- //取值
- inline static int Get(int * var)
- {
- #ifdef WIN32
- return _InterlockedExchangeAdd((long volatile *)(var), 0);
- #else
- return __sync_fetch_and_add(var, 0);
- #endif
- }
- private:
- Atom(void);
- ~Atom(void);
- };
- ///对象删除操作
- struct IntoPtrDel
- {
- template<class T>
- static void PtrDelete(T * obj)
- {
- delete obj;
- }
- };
- ///内嵌引用计数
- #define PTR_REF_COUNT private:df::IntoPtrBase into_Ptr_Member_;\\
- public:inline void PtrIncRef(){ into_Ptr_Member_.PtrIncRef(); }\\
- inline int PtrDecRef(){ return into_Ptr_Member_.PtrDecRef(); }
- ///继承此类或定义成员PTR_REF_COUNT,实现内嵌引用计数
- class IntoPtrBase
- {
- private:
- //引用计数
- int ptrRefCount_;
- public:
- //引用+1
- inline void PtrIncRef()
- {
- Atom::Inc(&ptrRefCount_);
- }
- //引用-1
- inline int PtrDecRef()
- {
- return Atom::Dec(&ptrRefCount_);
- }
- protected:
- inline IntoPtrBase() : ptrRefCount_(0)
- {}
- };
- //侵入式智能指针
- template<class ObjT, class DelT = IntoPtrDel>
- class IntoPtr
- {
- private:
- //托管对象
- ObjT * objPtr_;
- template <class T, class U>
- friend class IntoPtr;
- public:
- inline IntoPtr() : objPtr_(nullptr)
- {
- }
- inline IntoPtr(const IntoPtr & ip)
- {
- MY_ASSERT(ip.objPtr_ != nullptr);
- Set(ip);
- }
- template<class T>
- inline IntoPtr(const IntoPtr<T> & ip)
- {
- MY_ASSERT(ip.objPtr_ != nullptr);
- Set(ip);
- }
- //右值构造,如编译器不支持可将此注释掉
- template<class T>
- inline IntoPtr(IntoPtr<T> && ip)
- {
- objPtr_ = ip.objPtr_;
- ip.objPtr_ = nullptr;
- }
- inline IntoPtr(ObjT * obj)
- {
- MY_ASSERT(obj != nullptr);
- Set(obj);
- }
- //释放资源
- inline void Release()
- {
- if (objPtr_ != nullptr && objPtr_->PtrDecRef() == 0)
- {
- DelT::PtrDelete(objPtr_);
- }
- }
- inline ~IntoPtr()
- {
- Release();
- }
- //更改引用对象
- template<class T>
- inline void Reset(const IntoPtr<T> & ip)
- {
- MY_ASSERT(objPtr_ != ip.objPtr_ && ip.objPtr_ != nullptr);
- if (objPtr_ == ip.objPtr_ || ip.objPtr_ == nullptr)
- return;
- Release();
- Set(ip);
- }
- inline void Reset(ObjT * obj)
- {
- MY_ASSERT(obj != nullptr && objPtr_ != obj);
- if (obj == nullptr || objPtr_ == obj)
- return;
- Release();
- Set(obj);
- }
- //取对象指针
- inline ObjT * operator->()
- {
- MY_ASSERT(objPtr_ != nullptr);
- return objPtr_;
- }
- template<class T>
- inline IntoPtr & operator = (const IntoPtr<T> & ip)
- {
- Reset(ip);
- return *this;
- }
- inline IntoPtr & operator = (ObjT * obj)
- {
- Reset(obj);
- return *this;
- }
- inline ObjT * Get()
- {
- return objPtr_;
- }
- inline operator bool()
- {
- return objPtr_ != nullptr;
- }
- private:
- template<class T>
- inline void Set(const IntoPtr<T> & ip)
- {
- ip.objPtr_->PtrIncRef();
- objPtr_ = ip.objPtr_;
- }
- inline void Set(ObjT * obj)
- {
- obj->PtrIncRef();
- objPtr_ = obj;
- }
- };
- ////////////////////测试
- class Test
- {
- PTR_REF_COUNT;
- int a;
- };
- Test * t=new Test;
- IntoPtr<Test> p1=t;
- //引用同一对象,没问题
- IntoPtr<Test> p2=t;
- IntoPtr<Test> p3=new Test;
- //还可以自己管理生存周期,当然也要自己负责异常安全等,且增减引用必须配对
- t->PtrIncRef();
- if(t->PtrDecRef()<1)
- delete t;
- //该片段来自于http://www.codesnippet.cn/detail/181220138100.html
来源: http://www.codesnippet.cn/detail/181220138100.html