llvm.org GIT mirror llvm / ba928e2
Remove OwningPtr.h and associated tests llvm::OwningPtr is superseded by std::unique_ptr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211259 91177308-0d34-0410-b5e6-96231b3b80d8 Alp Toker 5 years ago
4 changed file(s) with 0 addition(s) and 447 deletion(s). Raw diff Collapse all Expand all
+0
-165
include/llvm/ADT/OwningPtr.h less more
None //===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines and implements the OwningPtr class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ADT_OWNINGPTR_H
14 #define LLVM_ADT_OWNINGPTR_H
15
16 #include "llvm/Support/Compiler.h"
17 #include
18 #include
19 #include
20
21 namespace llvm {
22
23 /// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
24 /// guarantees deletion of the object pointed to, either on destruction of the
25 /// OwningPtr or via an explicit reset(). Once created, ownership of the
26 /// pointee object can be taken away from OwningPtr by using the take method.
27 template
28 class OwningPtr {
29 OwningPtr(OwningPtr const &) LLVM_DELETED_FUNCTION;
30 OwningPtr &operator=(OwningPtr const &) LLVM_DELETED_FUNCTION;
31 T *Ptr;
32 public:
33 explicit OwningPtr(T *P = 0) : Ptr(P) {}
34
35 OwningPtr(OwningPtr &&Other) : Ptr(Other.take()) {}
36
37 OwningPtr &operator=(OwningPtr &&Other) {
38 reset(Other.take());
39 return *this;
40 }
41
42 OwningPtr(std::unique_ptr Other) : Ptr(Other.release()) {}
43
44 OwningPtr &operator=(std::unique_ptr Other) {
45 reset(Other.release());
46 return *this;
47 }
48
49 #if LLVM_HAS_RVALUE_REFERENCE_THIS
50 operator std::unique_ptr() && { return std::unique_ptr(take()); }
51 #endif
52
53 ~OwningPtr() {
54 delete Ptr;
55 }
56
57 /// reset - Change the current pointee to the specified pointer. Note that
58 /// calling this with any pointer (including a null pointer) deletes the
59 /// current pointer.
60 void reset(T *P = 0) {
61 if (P == Ptr) return;
62 T *Tmp = Ptr;
63 Ptr = P;
64 delete Tmp;
65 }
66
67 /// take - Reset the owning pointer to null and return its pointer. This does
68 /// not delete the pointer before returning it.
69 T *take() {
70 T *Tmp = Ptr;
71 Ptr = nullptr;
72 return Tmp;
73 }
74
75 T *release() { return take(); }
76
77 std::unique_ptr take_unique() { return std::unique_ptr(take()); }
78
79 T &operator*() const {
80 assert(Ptr && "Cannot dereference null pointer");
81 return *Ptr;
82 }
83
84 T *operator->() const { return Ptr; }
85 T *get() const { return Ptr; }
86 LLVM_EXPLICIT operator bool() const { return Ptr != nullptr; }
87 bool operator!() const { return Ptr == nullptr; }
88 bool isValid() const { return Ptr != nullptr; }
89
90 void swap(OwningPtr &RHS) {
91 T *Tmp = RHS.Ptr;
92 RHS.Ptr = Ptr;
93 Ptr = Tmp;
94 }
95 };
96
97 template
98 inline void swap(OwningPtr &a, OwningPtr &b) {
99 a.swap(b);
100 }
101
102 /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
103 /// functionality as OwningPtr, except that it works for array types.
104 template
105 class OwningArrayPtr {
106 OwningArrayPtr(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
107 OwningArrayPtr &operator=(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
108 T *Ptr;
109 public:
110 explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
111
112 OwningArrayPtr(OwningArrayPtr &&Other) : Ptr(Other.take()) {}
113
114 OwningArrayPtr &operator=(OwningArrayPtr &&Other) {
115 reset(Other.take());
116 return *this;
117 }
118
119 ~OwningArrayPtr() {
120 delete [] Ptr;
121 }
122
123 /// reset - Change the current pointee to the specified pointer. Note that
124 /// calling this with any pointer (including a null pointer) deletes the
125 /// current pointer.
126 void reset(T *P = 0) {
127 if (P == Ptr) return;
128 T *Tmp = Ptr;
129 Ptr = P;
130 delete [] Tmp;
131 }
132
133 /// take - Reset the owning pointer to null and return its pointer. This does
134 /// not delete the pointer before returning it.
135 T *take() {
136 T *Tmp = Ptr;
137 Ptr = 0;
138 return Tmp;
139 }
140
141 T &operator[](std::ptrdiff_t i) const {
142 assert(Ptr && "Cannot dereference null pointer");
143 return Ptr[i];
144 }
145
146 T *get() const { return Ptr; }
147 LLVM_EXPLICIT operator bool() const { return Ptr != 0; }
148 bool operator!() const { return Ptr == nullptr; }
149
150 void swap(OwningArrayPtr &RHS) {
151 T *Tmp = RHS.Ptr;
152 RHS.Ptr = Ptr;
153 Ptr = Tmp;
154 }
155 };
156
157 template
158 inline void swap(OwningArrayPtr &a, OwningArrayPtr &b) {
159 a.swap(b);
160 }
161
162 } // end namespace llvm
163
164 #endif
2222 MakeUniqueTest.cpp
2323 MapVectorTest.cpp
2424 OptionalTest.cpp
25 OwningPtrTest.cpp
2625 PackedVectorTest.cpp
2726 PointerIntPairTest.cpp
2827 PointerUnionTest.cpp
+0
-273
unittests/ADT/OwningPtrTest.cpp less more
None //===- llvm/unittest/ADT/OwningPtrTest.cpp - OwningPtr unit tests ---------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/OwningPtr.h"
10 #include "gtest/gtest.h"
11 using namespace llvm;
12
13 namespace {
14
15 struct TrackDestructor {
16 static unsigned Destructions;
17 int val;
18 explicit TrackDestructor(int val) : val(val) {}
19 ~TrackDestructor() { ++Destructions; }
20 static void ResetCounts() { Destructions = 0; }
21
22 private:
23 TrackDestructor(const TrackDestructor &other) LLVM_DELETED_FUNCTION;
24 TrackDestructor &
25 operator=(const TrackDestructor &other) LLVM_DELETED_FUNCTION;
26 TrackDestructor(TrackDestructor &&other) LLVM_DELETED_FUNCTION;
27 TrackDestructor &operator=(TrackDestructor &&other) LLVM_DELETED_FUNCTION;
28 };
29
30 unsigned TrackDestructor::Destructions = 0;
31
32 // Test fixture
33 class OwningPtrTest : public testing::Test {};
34
35 TEST_F(OwningPtrTest, DefaultConstruction) {
36 TrackDestructor::ResetCounts();
37 {
38 OwningPtr O;
39 EXPECT_FALSE(O);
40 EXPECT_TRUE(!O);
41 EXPECT_FALSE(O.get());
42 EXPECT_FALSE(O.isValid());
43 }
44 EXPECT_EQ(0u, TrackDestructor::Destructions);
45 }
46
47 TEST_F(OwningPtrTest, PtrConstruction) {
48 TrackDestructor::ResetCounts();
49 {
50 OwningPtr O(new TrackDestructor(3));
51 EXPECT_TRUE((bool)O);
52 EXPECT_FALSE(!O);
53 EXPECT_TRUE(O.get());
54 EXPECT_TRUE(O.isValid());
55 EXPECT_EQ(3, (*O).val);
56 EXPECT_EQ(3, O->val);
57 EXPECT_EQ(0u, TrackDestructor::Destructions);
58 }
59 EXPECT_EQ(1u, TrackDestructor::Destructions);
60 }
61
62 TEST_F(OwningPtrTest, Reset) {
63 TrackDestructor::ResetCounts();
64 OwningPtr O(new TrackDestructor(3));
65 EXPECT_EQ(0u, TrackDestructor::Destructions);
66 O.reset();
67 EXPECT_FALSE((bool)O);
68 EXPECT_TRUE(!O);
69 EXPECT_FALSE(O.get());
70 EXPECT_FALSE(O.isValid());
71 EXPECT_EQ(1u, TrackDestructor::Destructions);
72 }
73
74 TEST_F(OwningPtrTest, Take) {
75 TrackDestructor::ResetCounts();
76 TrackDestructor *T = nullptr;
77 {
78 OwningPtr O(new TrackDestructor(3));
79 T = O.take();
80 EXPECT_FALSE((bool)O);
81 EXPECT_TRUE(!O);
82 EXPECT_FALSE(O.get());
83 EXPECT_FALSE(O.isValid());
84 EXPECT_TRUE(T);
85 EXPECT_EQ(3, T->val);
86 EXPECT_EQ(0u, TrackDestructor::Destructions);
87 }
88 delete T;
89 EXPECT_EQ(1u, TrackDestructor::Destructions);
90 }
91
92 TEST_F(OwningPtrTest, Release) {
93 TrackDestructor::ResetCounts();
94 TrackDestructor *T = nullptr;
95 {
96 OwningPtr O(new TrackDestructor(3));
97 T = O.release();
98 EXPECT_FALSE((bool)O);
99 EXPECT_TRUE(!O);
100 EXPECT_FALSE(O.get());
101 EXPECT_FALSE(O.isValid());
102 EXPECT_TRUE(T);
103 EXPECT_EQ(3, T->val);
104 EXPECT_EQ(0u, TrackDestructor::Destructions);
105 }
106 delete T;
107 EXPECT_EQ(1u, TrackDestructor::Destructions);
108 }
109
110 TEST_F(OwningPtrTest, MoveConstruction) {
111 TrackDestructor::ResetCounts();
112 {
113 OwningPtr A(new TrackDestructor(3));
114 OwningPtr B = std::move(A);
115 EXPECT_FALSE((bool)A);
116 EXPECT_TRUE(!A);
117 EXPECT_FALSE(A.get());
118 EXPECT_FALSE(A.isValid());
119 EXPECT_TRUE((bool)B);
120 EXPECT_FALSE(!B);
121 EXPECT_TRUE(B.get());
122 EXPECT_TRUE(B.isValid());
123 EXPECT_EQ(3, (*B).val);
124 EXPECT_EQ(3, B->val);
125 EXPECT_EQ(0u, TrackDestructor::Destructions);
126 }
127 EXPECT_EQ(1u, TrackDestructor::Destructions);
128 }
129
130 TEST_F(OwningPtrTest, MoveAssignment) {
131 TrackDestructor::ResetCounts();
132 {
133 OwningPtr A(new TrackDestructor(3));
134 OwningPtr B(new TrackDestructor(4));
135 B = std::move(A);
136 EXPECT_FALSE(A);
137 EXPECT_TRUE(!A);
138 EXPECT_FALSE(A.get());
139 EXPECT_FALSE(A.isValid());
140 EXPECT_TRUE((bool)B);
141 EXPECT_FALSE(!B);
142 EXPECT_TRUE(B.get());
143 EXPECT_TRUE(B.isValid());
144 EXPECT_EQ(3, (*B).val);
145 EXPECT_EQ(3, B->val);
146 EXPECT_EQ(1u, TrackDestructor::Destructions);
147 }
148 EXPECT_EQ(2u, TrackDestructor::Destructions);
149 }
150
151 TEST_F(OwningPtrTest, Swap) {
152 TrackDestructor::ResetCounts();
153 {
154 OwningPtr A(new TrackDestructor(3));
155 OwningPtr B(new TrackDestructor(4));
156 B.swap(A);
157 EXPECT_TRUE((bool)A);
158 EXPECT_FALSE(!A);
159 EXPECT_TRUE(A.get());
160 EXPECT_TRUE(A.isValid());
161 EXPECT_EQ(4, (*A).val);
162 EXPECT_EQ(4, A->val);
163 EXPECT_TRUE((bool)B);
164 EXPECT_FALSE(!B);
165 EXPECT_TRUE(B.get());
166 EXPECT_TRUE(B.isValid());
167 EXPECT_EQ(3, (*B).val);
168 EXPECT_EQ(3, B->val);
169 EXPECT_EQ(0u, TrackDestructor::Destructions);
170 }
171 EXPECT_EQ(2u, TrackDestructor::Destructions);
172 TrackDestructor::ResetCounts();
173 {
174 OwningPtr A(new TrackDestructor(3));
175 OwningPtr B(new TrackDestructor(4));
176 swap(A, B);
177 EXPECT_TRUE((bool)A);
178 EXPECT_FALSE(!A);
179 EXPECT_TRUE(A.get());
180 EXPECT_TRUE(A.isValid());
181 EXPECT_EQ(4, (*A).val);
182 EXPECT_EQ(4, A->val);
183 EXPECT_TRUE((bool)B);
184 EXPECT_FALSE(!B);
185 EXPECT_TRUE(B.get());
186 EXPECT_TRUE(B.isValid());
187 EXPECT_EQ(3, (*B).val);
188 EXPECT_EQ(3, B->val);
189 EXPECT_EQ(0u, TrackDestructor::Destructions);
190 }
191 EXPECT_EQ(2u, TrackDestructor::Destructions);
192 }
193
194 TEST_F(OwningPtrTest, UniqueToOwningConstruction) {
195 TrackDestructor::ResetCounts();
196 {
197 std::unique_ptr A(new TrackDestructor(3));
198 OwningPtr B = std::move(A);
199 EXPECT_FALSE(A);
200 EXPECT_TRUE(!A);
201 EXPECT_FALSE(A.get());
202 EXPECT_TRUE((bool)B);
203 EXPECT_FALSE(!B);
204 EXPECT_TRUE(B.get());
205 EXPECT_TRUE(B.isValid());
206 EXPECT_EQ(3, (*B).val);
207 EXPECT_EQ(3, B->val);
208 EXPECT_EQ(0u, TrackDestructor::Destructions);
209 }
210 EXPECT_EQ(1u, TrackDestructor::Destructions);
211 }
212
213 TEST_F(OwningPtrTest, UniqueToOwningAssignment) {
214 TrackDestructor::ResetCounts();
215 {
216 std::unique_ptr A(new TrackDestructor(3));
217 OwningPtr B(new TrackDestructor(4));
218 B = std::move(A);
219 EXPECT_FALSE(A);
220 EXPECT_TRUE(!A);
221 EXPECT_FALSE(A.get());
222 EXPECT_TRUE((bool)B);
223 EXPECT_FALSE(!B);
224 EXPECT_TRUE(B.get());
225 EXPECT_TRUE(B.isValid());
226 EXPECT_EQ(3, (*B).val);
227 EXPECT_EQ(3, B->val);
228 EXPECT_EQ(1u, TrackDestructor::Destructions);
229 }
230 EXPECT_EQ(2u, TrackDestructor::Destructions);
231 }
232
233 TEST_F(OwningPtrTest, TakeUniqueConstruction) {
234 TrackDestructor::ResetCounts();
235 {
236 OwningPtr A(new TrackDestructor(3));
237 std::unique_ptr B = A.take_unique();
238 EXPECT_FALSE(A);
239 EXPECT_TRUE(!A);
240 EXPECT_FALSE(A.get());
241 EXPECT_FALSE(A.isValid());
242 EXPECT_TRUE((bool)B);
243 EXPECT_FALSE(!B);
244 EXPECT_TRUE(B.get());
245 EXPECT_EQ(3, (*B).val);
246 EXPECT_EQ(3, B->val);
247 EXPECT_EQ(0u, TrackDestructor::Destructions);
248 }
249 EXPECT_EQ(1u, TrackDestructor::Destructions);
250 }
251
252 #if LLVM_HAS_RVALUE_REFERENCE_THIS
253 TEST_F(OwningPtrTest, OwningToUniqueConstruction) {
254 TrackDestructor::ResetCounts();
255 {
256 OwningPtr A(new TrackDestructor(3));
257 std::unique_ptr B = std::move(A);
258 EXPECT_FALSE(A);
259 EXPECT_TRUE(!A);
260 EXPECT_FALSE(A.get());
261 EXPECT_FALSE(A.isValid());
262 EXPECT_TRUE((bool)B);
263 EXPECT_FALSE(!B);
264 EXPECT_TRUE(B.get());
265 EXPECT_EQ(3, (*B).val);
266 EXPECT_EQ(3, B->val);
267 EXPECT_EQ(0u, TrackDestructor::Destructions);
268 }
269 EXPECT_EQ(1u, TrackDestructor::Destructions);
270 }
271 #endif
272 }
107107
108108
109109
110
111 empty
112 OwningPtr {*Ptr}
113
114 Ptr
115
116
117
118110
119111 {{ [Small Mode] size={NumElements}, capacity={CurArraySize} }}
120112 {{ [Big Mode] size={NumElements}, capacity={CurArraySize} }}