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