Marine Library  1.0
C++ library for Linux Networking Development
shared_ptr.hh
1 #ifndef DOZERG_SHARED_PTR_H_20070828
2 #define DOZERG_SHARED_PTR_H_20070828
3 
4 /*
5  CSharedPtr 采用引用计数的智能指针,多线程安全
6  需要与allocator一起使用
7  既可以防止内存泄漏,又减少内存碎片
8  History
9  20070924 在__RefImp中加入pthread_mutex_t,修正多线程下操作的错误 20070925 把cnt_和pthread_mutex_t改成CLockInt 20080123 __RefImp的cnt_成员类型改为模板参数IntType.CSharedPtr加入Lock模板参数,表示是否需要加锁 20080604 增加release,set成员函数,safe_bool_type,和4个比较操作 20080912 增加swap函数,并重载std::swap 20080920 使用模板参数决定锁类型 20120118 增加release() //*/ #include <algorithm> //std::swap #include "impl/shared_ptr_impl.hh" NS_SERVER_BEGIN template<class T, class Alloc = std::allocator<T> > class CSharedPtr { //typedefs typedef CSharedPtr<T, Alloc> __Myt; typedef NS_IMPL::__RefImp<T, Alloc> __RefType; public: typedef T element_type; typedef T & reference; typedef T * pointer; typedef const T & const_reference; typedef const T * const_pointer; typedef typename __RefType::__ElemAlloc allocator_type; private: typedef typename __RefType::__RefAlloc __RefAlloc; typedef void (__Myt::*safe_bool_type)(pointer); public: //functions explicit CSharedPtr(pointer p = NULL):ref_(NULL){init(p);} CSharedPtr(const __Myt & a) : ref_(NULL) { __RefType::changeRef(ref_, a.ref_); } ~CSharedPtr() throw(){__RefType::subRef(ref_);} // cppcheck-suppress operatorEqVarError __Myt & operator =(const __Myt & a) throw(){ __RefType::changeRef(ref_, a.ref_); return *this; } __Myt & operator =(pointer a){ reset(a); return *this; } pointer get() const throw(){return (operator !() ? NULL : ref_->ptr_);} pointer operator ->() const throw(){return get();} reference operator *() const throw(){return *get();} bool operator !() const throw(){return !ref_;} operator safe_bool_type() const throw(){return (operator !() ? 0 : &__Myt::init);} bool operator ==(const __Myt & a) const throw(){return (ref_ == a.ref_);} bool operator !=(const __Myt & a) const throw(){return !operator ==(a);} bool operator ==(const_pointer a) const throw(){ return (operator !() ? !a : a == operator ->()); } bool operator !=(const_pointer a) const throw(){return !operator ==(a);} void reset(pointer a = NULL){ if(operator !=(a)){ __RefType::subRef(ref_); init(a); } } void swap(__Myt & a) throw(){std::swap(ref_, a.ref_);} private: void init(pointer p){ if(p) ref_ = tools::New1<__RefType>(p, __RefAlloc()); } //field __RefType * ref_; }; template<class T, class A> inline void swap(CSharedPtr<T, A> & a, CSharedPtr<T, A> & b) throw() { a.swap(b); } template<class T, class A> inline bool operator ==(const T * p, const CSharedPtr<T, A> & a) throw() { return (a == p); } template<class T, class A> inline bool operator !=(const T * p, const CSharedPtr<T, A> & a) throw() { return (a != p); } NS_SERVER_END #endif
10  20070925 把cnt_和pthread_mutex_t改成CLockInt
11  20080123 __RefImp的cnt_成员类型改为模板参数IntType.CSharedPtr加入Lock模板参数,表示是否需要加锁 20080604 增加release,set成员函数,safe_bool_type,和4个比较操作 20080912 增加swap函数,并重载std::swap 20080920 使用模板参数决定锁类型 20120118 增加release() //*/ #include <algorithm> //std::swap #include "impl/shared_ptr_impl.hh" NS_SERVER_BEGIN template<class T, class Alloc = std::allocator<T> > class CSharedPtr { //typedefs typedef CSharedPtr<T, Alloc> __Myt; typedef NS_IMPL::__RefImp<T, Alloc> __RefType; public: typedef T element_type; typedef T & reference; typedef T * pointer; typedef const T & const_reference; typedef const T * const_pointer; typedef typename __RefType::__ElemAlloc allocator_type; private: typedef typename __RefType::__RefAlloc __RefAlloc; typedef void (__Myt::*safe_bool_type)(pointer); public: //functions explicit CSharedPtr(pointer p = NULL):ref_(NULL){init(p);} CSharedPtr(const __Myt & a) : ref_(NULL) { __RefType::changeRef(ref_, a.ref_); } ~CSharedPtr() throw(){__RefType::subRef(ref_);} // cppcheck-suppress operatorEqVarError __Myt & operator =(const __Myt & a) throw(){ __RefType::changeRef(ref_, a.ref_); return *this; } __Myt & operator =(pointer a){ reset(a); return *this; } pointer get() const throw(){return (operator !() ? NULL : ref_->ptr_);} pointer operator ->() const throw(){return get();} reference operator *() const throw(){return *get();} bool operator !() const throw(){return !ref_;} operator safe_bool_type() const throw(){return (operator !() ? 0 : &__Myt::init);} bool operator ==(const __Myt & a) const throw(){return (ref_ == a.ref_);} bool operator !=(const __Myt & a) const throw(){return !operator ==(a);} bool operator ==(const_pointer a) const throw(){ return (operator !() ? !a : a == operator ->()); } bool operator !=(const_pointer a) const throw(){return !operator ==(a);} void reset(pointer a = NULL){ if(operator !=(a)){ __RefType::subRef(ref_); init(a); } } void swap(__Myt & a) throw(){std::swap(ref_, a.ref_);} private: void init(pointer p){ if(p) ref_ = tools::New1<__RefType>(p, __RefAlloc()); } //field __RefType * ref_; }; template<class T, class A> inline void swap(CSharedPtr<T, A> & a, CSharedPtr<T, A> & b) throw() { a.swap(b); } template<class T, class A> inline bool operator ==(const T * p, const CSharedPtr<T, A> & a) throw() { return (a == p); } template<class T, class A> inline bool operator !=(const T * p, const CSharedPtr<T, A> & a) throw() { return (a != p); } NS_SERVER_END #endif
12  20080604 增加release,set成员函数,safe_bool_type,和4个比较操作 20080912 增加swap函数,并重载std::swap 20080920 使用模板参数决定锁类型 20120118 增加release() //*/ #include <algorithm> //std::swap #include "impl/shared_ptr_impl.hh" NS_SERVER_BEGIN template<class T, class Alloc = std::allocator<T> > class CSharedPtr { //typedefs typedef CSharedPtr<T, Alloc> __Myt; typedef NS_IMPL::__RefImp<T, Alloc> __RefType; public: typedef T element_type; typedef T & reference; typedef T * pointer; typedef const T & const_reference; typedef const T * const_pointer; typedef typename __RefType::__ElemAlloc allocator_type; private: typedef typename __RefType::__RefAlloc __RefAlloc; typedef void (__Myt::*safe_bool_type)(pointer); public: //functions explicit CSharedPtr(pointer p = NULL):ref_(NULL){init(p);} CSharedPtr(const __Myt & a) : ref_(NULL) { __RefType::changeRef(ref_, a.ref_); } ~CSharedPtr() throw(){__RefType::subRef(ref_);} // cppcheck-suppress operatorEqVarError __Myt & operator =(const __Myt & a) throw(){ __RefType::changeRef(ref_, a.ref_); return *this; } __Myt & operator =(pointer a){ reset(a); return *this; } pointer get() const throw(){return (operator !() ? NULL : ref_->ptr_);} pointer operator ->() const throw(){return get();} reference operator *() const throw(){return *get();} bool operator !() const throw(){return !ref_;} operator safe_bool_type() const throw(){return (operator !() ? 0 : &__Myt::init);} bool operator ==(const __Myt & a) const throw(){return (ref_ == a.ref_);} bool operator !=(const __Myt & a) const throw(){return !operator ==(a);} bool operator ==(const_pointer a) const throw(){ return (operator !() ? !a : a == operator ->()); } bool operator !=(const_pointer a) const throw(){return !operator ==(a);} void reset(pointer a = NULL){ if(operator !=(a)){ __RefType::subRef(ref_); init(a); } } void swap(__Myt & a) throw(){std::swap(ref_, a.ref_);} private: void init(pointer p){ if(p) ref_ = tools::New1<__RefType>(p, __RefAlloc()); } //field __RefType * ref_; }; template<class T, class A> inline void swap(CSharedPtr<T, A> & a, CSharedPtr<T, A> & b) throw() { a.swap(b); } template<class T, class A> inline bool operator ==(const T * p, const CSharedPtr<T, A> & a) throw() { return (a == p); } template<class T, class A> inline bool operator !=(const T * p, const CSharedPtr<T, A> & a) throw() { return (a != p); } NS_SERVER_END #endif
13  20080912 增加swap函数,并重载std::swap
14  20080920 使用模板参数决定锁类型
15  20120118 增加release()
16 //*/
17 
18 #include <algorithm> //std::swap
19 #include "impl/shared_ptr_impl.hh"
20 
21 NS_SERVER_BEGIN
22 
23 template<class T, class Alloc = std::allocator<T> >
25 { //typedefs
27  typedef NS_IMPL::__RefImp<T, Alloc> __RefType;
28 public:
29  typedef T element_type;
30  typedef T & reference;
31  typedef T * pointer;
32  typedef const T & const_reference;
33  typedef const T * const_pointer;
34  typedef typename __RefType::__ElemAlloc allocator_type;
35 private:
36  typedef typename __RefType::__RefAlloc __RefAlloc;
37  typedef void (__Myt::*safe_bool_type)(pointer);
38 public:
39  //functions
40  explicit CSharedPtr(pointer p = NULL):ref_(NULL){init(p);}
41  CSharedPtr(const __Myt & a)
42  : ref_(NULL)
43  {
44  __RefType::changeRef(ref_, a.ref_);
45  }
46  ~CSharedPtr() throw(){__RefType::subRef(ref_);}
47  // cppcheck-suppress operatorEqVarError
48  __Myt & operator =(const __Myt & a) throw(){
49  __RefType::changeRef(ref_, a.ref_);
50  return *this;
51  }
52  __Myt & operator =(pointer a){
53  reset(a);
54  return *this;
55  }
56  pointer get() const throw(){return (operator !() ? NULL : ref_->ptr_);}
57  pointer operator ->() const throw(){return get();}
58  reference operator *() const throw(){return *get();}
59  bool operator !() const throw(){return !ref_;}
60  operator safe_bool_type() const throw(){return (operator !() ? 0 : &__Myt::init);}
61  bool operator ==(const __Myt & a) const throw(){return (ref_ == a.ref_);}
62  bool operator !=(const __Myt & a) const throw(){return !operator ==(a);}
63  bool operator ==(const_pointer a) const throw(){
64  return (operator !() ? !a : a == operator ->());
65  }
66  bool operator !=(const_pointer a) const throw(){return !operator ==(a);}
67  void reset(pointer a = NULL){
68  if(operator !=(a)){
69  __RefType::subRef(ref_);
70  init(a);
71  }
72  }
73  void swap(__Myt & a) throw(){std::swap(ref_, a.ref_);}
74 private:
75  void init(pointer p){
76  if(p)
77  ref_ = tools::New1<__RefType>(p, __RefAlloc());
78  }
79  //field
80  __RefType * ref_;
81 };
82 
83 template<class T, class A>
84 inline void swap(CSharedPtr<T, A> & a, CSharedPtr<T, A> & b) throw()
85 {
86  a.swap(b);
87 }
88 
89 template<class T, class A>
90 inline bool operator ==(const T * p, const CSharedPtr<T, A> & a) throw()
91 {
92  return (a == p);
93 }
94 
95 template<class T, class A>
96 inline bool operator !=(const T * p, const CSharedPtr<T, A> & a) throw()
97 {
98  return (a != p);
99 }
100 
101 NS_SERVER_END
102 
103 #endif
Definition: shared_ptr.hh:24