1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker * drawElements C++ Base Library
3*35238bceSAndroid Build Coastguard Worker * -----------------------------
4*35238bceSAndroid Build Coastguard Worker *
5*35238bceSAndroid Build Coastguard Worker * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker *
7*35238bceSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker *
11*35238bceSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker *
13*35238bceSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker *
19*35238bceSAndroid Build Coastguard Worker *//*!
20*35238bceSAndroid Build Coastguard Worker * \file
21*35238bceSAndroid Build Coastguard Worker * \brief Shared pointer.
22*35238bceSAndroid Build Coastguard Worker *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker
24*35238bceSAndroid Build Coastguard Worker #include "deSharedPtr.hpp"
25*35238bceSAndroid Build Coastguard Worker #include "deThread.hpp"
26*35238bceSAndroid Build Coastguard Worker #include "deClock.h"
27*35238bceSAndroid Build Coastguard Worker
28*35238bceSAndroid Build Coastguard Worker #include <exception>
29*35238bceSAndroid Build Coastguard Worker
30*35238bceSAndroid Build Coastguard Worker namespace de
31*35238bceSAndroid Build Coastguard Worker {
32*35238bceSAndroid Build Coastguard Worker
33*35238bceSAndroid Build Coastguard Worker namespace
34*35238bceSAndroid Build Coastguard Worker {
35*35238bceSAndroid Build Coastguard Worker
36*35238bceSAndroid Build Coastguard Worker enum
37*35238bceSAndroid Build Coastguard Worker {
38*35238bceSAndroid Build Coastguard Worker THREAD_TEST_TIME = 200 * 1000
39*35238bceSAndroid Build Coastguard Worker };
40*35238bceSAndroid Build Coastguard Worker
41*35238bceSAndroid Build Coastguard Worker class Object
42*35238bceSAndroid Build Coastguard Worker {
43*35238bceSAndroid Build Coastguard Worker public:
Object(bool & exists)44*35238bceSAndroid Build Coastguard Worker Object(bool &exists) : m_exists(exists)
45*35238bceSAndroid Build Coastguard Worker {
46*35238bceSAndroid Build Coastguard Worker m_exists = true;
47*35238bceSAndroid Build Coastguard Worker }
48*35238bceSAndroid Build Coastguard Worker
~Object(void)49*35238bceSAndroid Build Coastguard Worker virtual ~Object(void)
50*35238bceSAndroid Build Coastguard Worker {
51*35238bceSAndroid Build Coastguard Worker m_exists = false;
52*35238bceSAndroid Build Coastguard Worker }
53*35238bceSAndroid Build Coastguard Worker
54*35238bceSAndroid Build Coastguard Worker private:
55*35238bceSAndroid Build Coastguard Worker bool &m_exists;
56*35238bceSAndroid Build Coastguard Worker };
57*35238bceSAndroid Build Coastguard Worker
58*35238bceSAndroid Build Coastguard Worker class DerivedObject : public Object
59*35238bceSAndroid Build Coastguard Worker {
60*35238bceSAndroid Build Coastguard Worker public:
DerivedObject(bool & exists)61*35238bceSAndroid Build Coastguard Worker DerivedObject(bool &exists) : Object(exists)
62*35238bceSAndroid Build Coastguard Worker {
63*35238bceSAndroid Build Coastguard Worker }
64*35238bceSAndroid Build Coastguard Worker };
65*35238bceSAndroid Build Coastguard Worker
66*35238bceSAndroid Build Coastguard Worker class SharedPtrTestThread : public Thread
67*35238bceSAndroid Build Coastguard Worker {
68*35238bceSAndroid Build Coastguard Worker public:
SharedPtrTestThread(const SharedPtr<Object> & ptr,const bool & exists)69*35238bceSAndroid Build Coastguard Worker SharedPtrTestThread(const SharedPtr<Object> &ptr, const bool &exists) : m_ptr(ptr), m_exists(exists)
70*35238bceSAndroid Build Coastguard Worker {
71*35238bceSAndroid Build Coastguard Worker }
72*35238bceSAndroid Build Coastguard Worker
run(void)73*35238bceSAndroid Build Coastguard Worker void run(void)
74*35238bceSAndroid Build Coastguard Worker {
75*35238bceSAndroid Build Coastguard Worker uint64_t startTime = deGetMicroseconds();
76*35238bceSAndroid Build Coastguard Worker uint64_t cnt = 0;
77*35238bceSAndroid Build Coastguard Worker
78*35238bceSAndroid Build Coastguard Worker for (;; cnt++)
79*35238bceSAndroid Build Coastguard Worker {
80*35238bceSAndroid Build Coastguard Worker if (((cnt & (1 << 14)) != 0) && (deGetMicroseconds() - startTime >= THREAD_TEST_TIME))
81*35238bceSAndroid Build Coastguard Worker break;
82*35238bceSAndroid Build Coastguard Worker
83*35238bceSAndroid Build Coastguard Worker {
84*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA(m_ptr);
85*35238bceSAndroid Build Coastguard Worker {
86*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB;
87*35238bceSAndroid Build Coastguard Worker ptrB = ptrA;
88*35238bceSAndroid Build Coastguard Worker ptrA = SharedPtr<Object>();
89*35238bceSAndroid Build Coastguard Worker }
90*35238bceSAndroid Build Coastguard Worker }
91*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(m_exists);
92*35238bceSAndroid Build Coastguard Worker }
93*35238bceSAndroid Build Coastguard Worker }
94*35238bceSAndroid Build Coastguard Worker
95*35238bceSAndroid Build Coastguard Worker private:
96*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> m_ptr;
97*35238bceSAndroid Build Coastguard Worker const bool &m_exists;
98*35238bceSAndroid Build Coastguard Worker };
99*35238bceSAndroid Build Coastguard Worker
100*35238bceSAndroid Build Coastguard Worker class WeakPtrTestThread : public Thread
101*35238bceSAndroid Build Coastguard Worker {
102*35238bceSAndroid Build Coastguard Worker public:
WeakPtrTestThread(const SharedPtr<Object> & ptr,const bool & exists)103*35238bceSAndroid Build Coastguard Worker WeakPtrTestThread(const SharedPtr<Object> &ptr, const bool &exists) : m_ptr(ptr), m_exists(exists)
104*35238bceSAndroid Build Coastguard Worker {
105*35238bceSAndroid Build Coastguard Worker }
106*35238bceSAndroid Build Coastguard Worker
run(void)107*35238bceSAndroid Build Coastguard Worker void run(void)
108*35238bceSAndroid Build Coastguard Worker {
109*35238bceSAndroid Build Coastguard Worker uint64_t startTime = deGetMicroseconds();
110*35238bceSAndroid Build Coastguard Worker uint64_t cnt = 0;
111*35238bceSAndroid Build Coastguard Worker
112*35238bceSAndroid Build Coastguard Worker for (;; cnt++)
113*35238bceSAndroid Build Coastguard Worker {
114*35238bceSAndroid Build Coastguard Worker if (((cnt & (1 << 14)) != 0) && (deGetMicroseconds() - startTime >= THREAD_TEST_TIME))
115*35238bceSAndroid Build Coastguard Worker break;
116*35238bceSAndroid Build Coastguard Worker
117*35238bceSAndroid Build Coastguard Worker {
118*35238bceSAndroid Build Coastguard Worker WeakPtr<Object> ptrA(m_ptr);
119*35238bceSAndroid Build Coastguard Worker {
120*35238bceSAndroid Build Coastguard Worker WeakPtr<Object> ptrB;
121*35238bceSAndroid Build Coastguard Worker ptrB = ptrA;
122*35238bceSAndroid Build Coastguard Worker ptrA = SharedPtr<Object>();
123*35238bceSAndroid Build Coastguard Worker }
124*35238bceSAndroid Build Coastguard Worker }
125*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(m_exists);
126*35238bceSAndroid Build Coastguard Worker }
127*35238bceSAndroid Build Coastguard Worker }
128*35238bceSAndroid Build Coastguard Worker
129*35238bceSAndroid Build Coastguard Worker private:
130*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> m_ptr;
131*35238bceSAndroid Build Coastguard Worker const bool &m_exists;
132*35238bceSAndroid Build Coastguard Worker };
133*35238bceSAndroid Build Coastguard Worker
makeObject(bool & exists)134*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> makeObject(bool &exists)
135*35238bceSAndroid Build Coastguard Worker {
136*35238bceSAndroid Build Coastguard Worker return SharedPtr<Object>(new Object(exists));
137*35238bceSAndroid Build Coastguard Worker }
138*35238bceSAndroid Build Coastguard Worker
139*35238bceSAndroid Build Coastguard Worker struct CustomDeleter
140*35238bceSAndroid Build Coastguard Worker {
CustomDeleterde::__anon7181c7410111::CustomDeleter141*35238bceSAndroid Build Coastguard Worker CustomDeleter(bool *called) : m_called(called)
142*35238bceSAndroid Build Coastguard Worker {
143*35238bceSAndroid Build Coastguard Worker }
144*35238bceSAndroid Build Coastguard Worker
operator ()de::__anon7181c7410111::CustomDeleter145*35238bceSAndroid Build Coastguard Worker void operator()(Object *ptr)
146*35238bceSAndroid Build Coastguard Worker {
147*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!*m_called);
148*35238bceSAndroid Build Coastguard Worker delete ptr;
149*35238bceSAndroid Build Coastguard Worker *m_called = true;
150*35238bceSAndroid Build Coastguard Worker }
151*35238bceSAndroid Build Coastguard Worker
152*35238bceSAndroid Build Coastguard Worker bool *m_called;
153*35238bceSAndroid Build Coastguard Worker };
154*35238bceSAndroid Build Coastguard Worker
155*35238bceSAndroid Build Coastguard Worker } // namespace
156*35238bceSAndroid Build Coastguard Worker
SharedPtr_selfTest(void)157*35238bceSAndroid Build Coastguard Worker void SharedPtr_selfTest(void)
158*35238bceSAndroid Build Coastguard Worker {
159*35238bceSAndroid Build Coastguard Worker // Empty pointer test.
160*35238bceSAndroid Build Coastguard Worker {
161*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr;
162*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr.get() == DE_NULL);
163*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!ptr);
164*35238bceSAndroid Build Coastguard Worker }
165*35238bceSAndroid Build Coastguard Worker
166*35238bceSAndroid Build Coastguard Worker // Empty pointer copy.
167*35238bceSAndroid Build Coastguard Worker {
168*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA;
169*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB(ptrA);
170*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptrB.get() == DE_NULL);
171*35238bceSAndroid Build Coastguard Worker }
172*35238bceSAndroid Build Coastguard Worker
173*35238bceSAndroid Build Coastguard Worker // Empty pointer assignment.
174*35238bceSAndroid Build Coastguard Worker {
175*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA;
176*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB;
177*35238bceSAndroid Build Coastguard Worker ptrB = ptrA;
178*35238bceSAndroid Build Coastguard Worker ptrB = *&ptrB;
179*35238bceSAndroid Build Coastguard Worker }
180*35238bceSAndroid Build Coastguard Worker
181*35238bceSAndroid Build Coastguard Worker // Basic test.
182*35238bceSAndroid Build Coastguard Worker {
183*35238bceSAndroid Build Coastguard Worker bool exists = false;
184*35238bceSAndroid Build Coastguard Worker {
185*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
186*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
187*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr.get() != DE_NULL);
188*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr);
189*35238bceSAndroid Build Coastguard Worker }
190*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
191*35238bceSAndroid Build Coastguard Worker }
192*35238bceSAndroid Build Coastguard Worker
193*35238bceSAndroid Build Coastguard Worker // Exception test.
194*35238bceSAndroid Build Coastguard Worker {
195*35238bceSAndroid Build Coastguard Worker bool exists = false;
196*35238bceSAndroid Build Coastguard Worker try
197*35238bceSAndroid Build Coastguard Worker {
198*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
199*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
200*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr.get() != DE_NULL);
201*35238bceSAndroid Build Coastguard Worker throw std::exception();
202*35238bceSAndroid Build Coastguard Worker }
203*35238bceSAndroid Build Coastguard Worker catch (const std::exception &)
204*35238bceSAndroid Build Coastguard Worker {
205*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
206*35238bceSAndroid Build Coastguard Worker }
207*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
208*35238bceSAndroid Build Coastguard Worker }
209*35238bceSAndroid Build Coastguard Worker
210*35238bceSAndroid Build Coastguard Worker // Expression test.
211*35238bceSAndroid Build Coastguard Worker {
212*35238bceSAndroid Build Coastguard Worker bool exists = false;
213*35238bceSAndroid Build Coastguard Worker bool test = (SharedPtr<Object>(new Object(exists))).get() != DE_NULL && exists;
214*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
215*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(test);
216*35238bceSAndroid Build Coastguard Worker }
217*35238bceSAndroid Build Coastguard Worker
218*35238bceSAndroid Build Coastguard Worker // Assignment test.
219*35238bceSAndroid Build Coastguard Worker {
220*35238bceSAndroid Build Coastguard Worker bool exists = false;
221*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
222*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
223*35238bceSAndroid Build Coastguard Worker ptr = SharedPtr<Object>();
224*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
225*35238bceSAndroid Build Coastguard Worker }
226*35238bceSAndroid Build Coastguard Worker
227*35238bceSAndroid Build Coastguard Worker // Self-assignment test.
228*35238bceSAndroid Build Coastguard Worker {
229*35238bceSAndroid Build Coastguard Worker bool exists = false;
230*35238bceSAndroid Build Coastguard Worker {
231*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
232*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
233*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr.get() != DE_NULL);
234*35238bceSAndroid Build Coastguard Worker ptr = *&ptr;
235*35238bceSAndroid Build Coastguard Worker }
236*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
237*35238bceSAndroid Build Coastguard Worker }
238*35238bceSAndroid Build Coastguard Worker
239*35238bceSAndroid Build Coastguard Worker // Basic multi-reference via copy ctor.
240*35238bceSAndroid Build Coastguard Worker {
241*35238bceSAndroid Build Coastguard Worker bool exists = false;
242*35238bceSAndroid Build Coastguard Worker {
243*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA(new Object(exists));
244*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
245*35238bceSAndroid Build Coastguard Worker {
246*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB(ptrA);
247*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
248*35238bceSAndroid Build Coastguard Worker }
249*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
250*35238bceSAndroid Build Coastguard Worker }
251*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
252*35238bceSAndroid Build Coastguard Worker }
253*35238bceSAndroid Build Coastguard Worker
254*35238bceSAndroid Build Coastguard Worker // Basic multi-reference via assignment to empty.
255*35238bceSAndroid Build Coastguard Worker {
256*35238bceSAndroid Build Coastguard Worker bool exists = false;
257*35238bceSAndroid Build Coastguard Worker {
258*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA(new Object(exists));
259*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
260*35238bceSAndroid Build Coastguard Worker {
261*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB;
262*35238bceSAndroid Build Coastguard Worker ptrB = ptrA;
263*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
264*35238bceSAndroid Build Coastguard Worker }
265*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
266*35238bceSAndroid Build Coastguard Worker }
267*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
268*35238bceSAndroid Build Coastguard Worker }
269*35238bceSAndroid Build Coastguard Worker
270*35238bceSAndroid Build Coastguard Worker // Multi-reference via assignment to non-empty.
271*35238bceSAndroid Build Coastguard Worker {
272*35238bceSAndroid Build Coastguard Worker bool existsA = false;
273*35238bceSAndroid Build Coastguard Worker bool existsB = false;
274*35238bceSAndroid Build Coastguard Worker {
275*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA(new Object(existsA));
276*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(existsA);
277*35238bceSAndroid Build Coastguard Worker {
278*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB(new Object(existsB));
279*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(existsB);
280*35238bceSAndroid Build Coastguard Worker ptrA = ptrB;
281*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!existsA);
282*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(existsB);
283*35238bceSAndroid Build Coastguard Worker }
284*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(existsB);
285*35238bceSAndroid Build Coastguard Worker }
286*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!existsB);
287*35238bceSAndroid Build Coastguard Worker }
288*35238bceSAndroid Build Coastguard Worker
289*35238bceSAndroid Build Coastguard Worker // Return from function.
290*35238bceSAndroid Build Coastguard Worker {
291*35238bceSAndroid Build Coastguard Worker bool exists = false;
292*35238bceSAndroid Build Coastguard Worker {
293*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr;
294*35238bceSAndroid Build Coastguard Worker ptr = makeObject(exists);
295*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
296*35238bceSAndroid Build Coastguard Worker }
297*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
298*35238bceSAndroid Build Coastguard Worker }
299*35238bceSAndroid Build Coastguard Worker
300*35238bceSAndroid Build Coastguard Worker // Equality comparison.
301*35238bceSAndroid Build Coastguard Worker {
302*35238bceSAndroid Build Coastguard Worker bool existsA = false;
303*35238bceSAndroid Build Coastguard Worker bool existsB = false;
304*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrA(new Object(existsA));
305*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrB(new Object(existsB));
306*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptrC(ptrA);
307*35238bceSAndroid Build Coastguard Worker
308*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptrA == ptrA);
309*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptrA != ptrB);
310*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptrA == ptrC);
311*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptrC != ptrB);
312*35238bceSAndroid Build Coastguard Worker }
313*35238bceSAndroid Build Coastguard Worker
314*35238bceSAndroid Build Coastguard Worker // Conversion via assignment.
315*35238bceSAndroid Build Coastguard Worker {
316*35238bceSAndroid Build Coastguard Worker bool exists = false;
317*35238bceSAndroid Build Coastguard Worker {
318*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> basePtr;
319*35238bceSAndroid Build Coastguard Worker {
320*35238bceSAndroid Build Coastguard Worker SharedPtr<DerivedObject> derivedPtr(new DerivedObject(exists));
321*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
322*35238bceSAndroid Build Coastguard Worker basePtr = derivedPtr;
323*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
324*35238bceSAndroid Build Coastguard Worker }
325*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
326*35238bceSAndroid Build Coastguard Worker }
327*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
328*35238bceSAndroid Build Coastguard Worker }
329*35238bceSAndroid Build Coastguard Worker
330*35238bceSAndroid Build Coastguard Worker // Conversion via copy ctor.
331*35238bceSAndroid Build Coastguard Worker {
332*35238bceSAndroid Build Coastguard Worker bool exists = false;
333*35238bceSAndroid Build Coastguard Worker {
334*35238bceSAndroid Build Coastguard Worker SharedPtr<DerivedObject> derivedPtr(new DerivedObject(exists));
335*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> basePtr(derivedPtr);
336*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
337*35238bceSAndroid Build Coastguard Worker derivedPtr = SharedPtr<DerivedObject>();
338*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
339*35238bceSAndroid Build Coastguard Worker }
340*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
341*35238bceSAndroid Build Coastguard Worker }
342*35238bceSAndroid Build Coastguard Worker
343*35238bceSAndroid Build Coastguard Worker // Explicit conversion operator.
344*35238bceSAndroid Build Coastguard Worker {
345*35238bceSAndroid Build Coastguard Worker bool exists = false;
346*35238bceSAndroid Build Coastguard Worker {
347*35238bceSAndroid Build Coastguard Worker SharedPtr<DerivedObject> derivedPtr(new DerivedObject(exists));
348*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
349*35238bceSAndroid Build Coastguard Worker
350*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> basePtr = (SharedPtr<Object>)(derivedPtr);
351*35238bceSAndroid Build Coastguard Worker derivedPtr = SharedPtr<DerivedObject>();
352*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
353*35238bceSAndroid Build Coastguard Worker }
354*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
355*35238bceSAndroid Build Coastguard Worker }
356*35238bceSAndroid Build Coastguard Worker
357*35238bceSAndroid Build Coastguard Worker // Basic weak reference.
358*35238bceSAndroid Build Coastguard Worker {
359*35238bceSAndroid Build Coastguard Worker bool exists = false;
360*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
361*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
362*35238bceSAndroid Build Coastguard Worker
363*35238bceSAndroid Build Coastguard Worker WeakPtr<Object> weakPtr(ptr);
364*35238bceSAndroid Build Coastguard Worker try
365*35238bceSAndroid Build Coastguard Worker {
366*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> newRef(weakPtr);
367*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
368*35238bceSAndroid Build Coastguard Worker }
369*35238bceSAndroid Build Coastguard Worker catch (const DeadReferenceException &)
370*35238bceSAndroid Build Coastguard Worker {
371*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(false);
372*35238bceSAndroid Build Coastguard Worker }
373*35238bceSAndroid Build Coastguard Worker
374*35238bceSAndroid Build Coastguard Worker ptr = SharedPtr<Object>();
375*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
376*35238bceSAndroid Build Coastguard Worker try
377*35238bceSAndroid Build Coastguard Worker {
378*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> newRef(weakPtr);
379*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(false);
380*35238bceSAndroid Build Coastguard Worker }
381*35238bceSAndroid Build Coastguard Worker catch (const DeadReferenceException &)
382*35238bceSAndroid Build Coastguard Worker {
383*35238bceSAndroid Build Coastguard Worker }
384*35238bceSAndroid Build Coastguard Worker }
385*35238bceSAndroid Build Coastguard Worker
386*35238bceSAndroid Build Coastguard Worker // Basic SharedPtr threaded test.
387*35238bceSAndroid Build Coastguard Worker {
388*35238bceSAndroid Build Coastguard Worker bool exists = false;
389*35238bceSAndroid Build Coastguard Worker {
390*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
391*35238bceSAndroid Build Coastguard Worker
392*35238bceSAndroid Build Coastguard Worker SharedPtrTestThread threadA(ptr, exists);
393*35238bceSAndroid Build Coastguard Worker SharedPtrTestThread threadB(ptr, exists);
394*35238bceSAndroid Build Coastguard Worker
395*35238bceSAndroid Build Coastguard Worker threadA.start();
396*35238bceSAndroid Build Coastguard Worker threadB.start();
397*35238bceSAndroid Build Coastguard Worker
398*35238bceSAndroid Build Coastguard Worker threadA.join();
399*35238bceSAndroid Build Coastguard Worker threadB.join();
400*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
401*35238bceSAndroid Build Coastguard Worker }
402*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
403*35238bceSAndroid Build Coastguard Worker }
404*35238bceSAndroid Build Coastguard Worker
405*35238bceSAndroid Build Coastguard Worker // Basic WeakPtr threaded test.
406*35238bceSAndroid Build Coastguard Worker {
407*35238bceSAndroid Build Coastguard Worker bool exists = false;
408*35238bceSAndroid Build Coastguard Worker {
409*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists));
410*35238bceSAndroid Build Coastguard Worker WeakPtrTestThread threadA(ptr, exists);
411*35238bceSAndroid Build Coastguard Worker WeakPtrTestThread threadB(ptr, exists);
412*35238bceSAndroid Build Coastguard Worker
413*35238bceSAndroid Build Coastguard Worker threadA.start();
414*35238bceSAndroid Build Coastguard Worker threadB.start();
415*35238bceSAndroid Build Coastguard Worker
416*35238bceSAndroid Build Coastguard Worker threadA.join();
417*35238bceSAndroid Build Coastguard Worker threadB.join();
418*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
419*35238bceSAndroid Build Coastguard Worker }
420*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
421*35238bceSAndroid Build Coastguard Worker }
422*35238bceSAndroid Build Coastguard Worker
423*35238bceSAndroid Build Coastguard Worker // Basic custom deleter.
424*35238bceSAndroid Build Coastguard Worker {
425*35238bceSAndroid Build Coastguard Worker bool exists = false;
426*35238bceSAndroid Build Coastguard Worker bool deleterCalled = false;
427*35238bceSAndroid Build Coastguard Worker {
428*35238bceSAndroid Build Coastguard Worker SharedPtr<Object> ptr(new Object(exists), CustomDeleter(&deleterCalled));
429*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(exists);
430*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!deleterCalled);
431*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(ptr.get() != DE_NULL);
432*35238bceSAndroid Build Coastguard Worker }
433*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(!exists);
434*35238bceSAndroid Build Coastguard Worker DE_TEST_ASSERT(deleterCalled);
435*35238bceSAndroid Build Coastguard Worker }
436*35238bceSAndroid Build Coastguard Worker }
437*35238bceSAndroid Build Coastguard Worker
438*35238bceSAndroid Build Coastguard Worker } // namespace de
439