llvm.org GIT mirror llvm / d2128a7
cleanup this code, making it more "llvm-like". Add comments to reset indicating that it deletes its pointer. Add a new take() method, which can be used to get the pointer without it being deleted. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45112 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
1 changed file(s) with 71 addition(s) and 76 deletion(s). Raw diff Collapse all Expand all
2222 //
2323 // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
2424 //
25
26 #ifndef LLVM_SCOPED_PTR_HPP_INCLUDED
27 #define LLVM_SCOPED_PTR_HPP_INCLUDED
25 #ifndef LLVM_SCOPED_PTR_H_INCLUDED
26 #define LLVM_SCOPED_PTR_H_INCLUDED
2827
2928 #include
3029
3130 namespace llvm {
3231
3332 // verify that types are complete for increased safety
34 template inline void checked_delete(T * x) {
35 // intentionally complex - simplification causes regressions
36 typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
37 (void) sizeof(type_must_be_complete);
38 delete x;
33 template
34 inline void checked_delete(T * x) {
35 // intentionally complex - simplification causes warnings in some compilers.
36 typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
37 (void)sizeof(type_must_be_complete);
38 delete x;
3939 }
4040
41 // scoped_ptr mimics a built-in pointer except that it guarantees deletion
42 // of the object pointed to, either on destruction of the scoped_ptr or via
43 // an explicit reset(). scoped_ptr is a simple solution for simple needs;
44 // use shared_ptr or std::auto_ptr if your needs are more complex.
41 /// scoped_ptr mimics a built-in pointer except that it guarantees deletion
42 /// of the object pointed to, either on destruction of the scoped_ptr or via
43 /// an explicit reset(). scoped_ptr is a simple solution for simple needs;
44 /// use shared_ptr or std::auto_ptr if your needs are more complex.
45 template
46 class scoped_ptr {// noncopyable
47 T *ptr;
48 scoped_ptr(scoped_ptr const &); // DO NOT IMPLEMENT
49 scoped_ptr & operator=(scoped_ptr const &); // DO NOT IMPLEMENT
50 typedef scoped_ptr this_type;
51 public:
52 typedef T element_type;
4553
46 template class scoped_ptr // noncopyable
47 {
48 private:
54 explicit scoped_ptr(T * p = 0): ptr(p) {} // never throws
4955
50 T * ptr;
56 ~scoped_ptr() { // never throws
57 llvm::checked_delete(ptr);
58 }
5159
52 scoped_ptr(scoped_ptr const &);
53 scoped_ptr & operator=(scoped_ptr const &);
60 /// reset - Change the current pointee to the specified pointer. Note that
61 /// calling this with any pointer (including a null pointer) deletes the
62 /// current pointer.
63 void reset(T *p = 0) {
64 // catch self-reset errors
65 assert((p == 0 || p != ptr) && "scoped_ptr: self-reset error");
66 T *tmp = ptr;
67 ptr = p;
68 delete tmp;
69 }
5470
55 typedef scoped_ptr this_type;
71 /// take - Reset the scoped pointer to null and return its pointer. This does
72 /// not delete the pointer before returning it.
73 T *take() {
74 T *P = ptr;
75 ptr = 0;
76 return P;
77 }
78
79 T& operator*() const {
80 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
81 return *ptr;
82 }
5683
57 public:
84 T* operator->() const {
85 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
86 return ptr;
87 }
5888
59 typedef T element_type;
89 T* get() const {
90 return ptr;
91 }
6092
61 explicit scoped_ptr(T * p = 0): ptr(p) // never throws
62 {
63 }
93 // implicit conversion to "bool"
94 typedef T * this_type::*unspecified_bool_type;
6495
65 ~scoped_ptr() // never throws
66 {
67 llvm::checked_delete(ptr);
68 }
96 operator unspecified_bool_type() const {// never throws
97 return ptr == 0? 0: &this_type::ptr;
98 }
6999
70 void reset(T * p = 0) // never throws
71 {
72 assert( (p == 0 || p != ptr) && "scoped_ptr: self-reset error"); // catch self-reset errors
73 this_type(p).swap(*this);
74 }
100 bool operator!() const { // never throws
101 return ptr == 0;
102 }
75103
76 T & operator*() const // never throws
77 {
78 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur");
79 return *ptr;
80 }
81
82 T * operator->() const // never throws
83 {
84 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur");
85 return ptr;
86 }
87
88 T * get() const // never throws
89 {
90 return ptr;
91 }
92
93 // implicit conversion to "bool"
94 typedef T * this_type::*unspecified_bool_type;
95
96 operator unspecified_bool_type() const // never throws
97 {
98 return ptr == 0? 0: &this_type::ptr;
99 }
100
101 bool operator! () const // never throws
102 {
103 return ptr == 0;
104 }
105
106 void swap(scoped_ptr & b) // never throws
107 {
108 T * tmp = b.ptr;
109 b.ptr = ptr;
110 ptr = tmp;
111 }
104 void swap(scoped_ptr &b) {// never throws
105 T * tmp = b.ptr;
106 b.ptr = ptr;
107 ptr = tmp;
108 }
112109 };
113110
114 template inline void swap(scoped_ptr & a, scoped_ptr & b) // never throws
115 {
116 a.swap(b);
111 template inline void swap(scoped_ptr &a, scoped_ptr &b) {
112 // never throws
113 a.swap(b);
117114 }
118115
119116 // get_pointer(p) is a generic way to say p.get()
120
121 template inline T * get_pointer(scoped_ptr const & p)
122 {
123 return p.get();
117 template inline T * get_pointer(scoped_ptr const &p) {
118 return p.get();
124119 }
125120
126121 } // namespace llvm