shared_ptr.h revision 0d49a2fbc9857ad8dd29542de7fb37202f1a283f
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#ifndef __SHARED_PTR_H__
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#define __SHARED_PTR_H__
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#ifdef __cplusplus
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsynctemplate<typename T>
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsyncclass SharedPtr
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync{
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync struct imp
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync ~imp() { if (ptr) delete ptr;}
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync int refcnt;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync };
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync public:
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync SharedPtr(T *t = NULL):p(NULL)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = new imp(t);
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync ~SharedPtr()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt--;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync if (p->refcnt == 0)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync delete p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync SharedPtr(const SharedPtr& rhs)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = rhs.p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt++;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync const SharedPtr& operator= (const SharedPtr& rhs)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync if (p == rhs.p) return *this;
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt--;
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync if (p->refcnt == 0)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync delete p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = rhs.p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt++;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return *this;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *get() const
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *operator->()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync const T*operator->() const
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync int use_count()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->refcnt;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync private:
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync imp *p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync};
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#endif
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#endif