Marine Library  1.0
C++ library for Linux Networking Development
scoped_ptr.hh
1 #ifndef DOZERG_SCOPED_POINTER_H_20130603
2 #define DOZERG_SCOPED_POINTER_H_20130603
3 
4 #include <algorithm> //std::swap
5 #include "tools/memory.hh"
6 
7 NS_SERVER_BEGIN
8 
9 template<class T, class Alloc = std::allocator<T> >
11 {
13  typedef void (__Myt::*__SafeBool)(__Myt &);
14 public:
15  typedef T element_type;
16  typedef Alloc allocator_type;
17  typedef T * pointer;
18  typedef T & reference;
19  //functions
20  explicit CScopedPtr(pointer p = NULL):p_(p){}
21  ~CScopedPtr(){tools::Delete(p_, allocator_type());}
22  bool operator !() const throw(){return (NULL == p_);}
23  operator __SafeBool() const throw(){return (operator !() ? NULL : &__Myt::swap);}
24  pointer get() const throw(){return p_;}
25  pointer operator ->() const throw(){return get();}
26  reference operator *() const throw(){return *get();}
27  void reset(pointer p = NULL) throw(){
28  if(p != p_)
29  __Myt(p).swap(*this);
30  }
31  pointer release() throw(){
32  pointer p = p_;
33  p_ = NULL;
34  return p;
35  }
36  void swap(__Myt & a) throw(){std::swap(p_, a.p_);}
37 private:
38  CScopedPtr(const __Myt &); //disable copy and assignment
39  __Myt & operator =(const __Myt &);
40  //fields
41  pointer p_;
42 };
43 
44 template<class T, class Alloc>
45 inline void swap(CScopedPtr<T, Alloc> & a, CScopedPtr<T, Alloc> & b) throw()
46 {
47  a.swap(b);
48 }
49 
50 NS_SERVER_END
51 
52 #endif
53 
Definition: scoped_ptr.hh:10