OLD | NEW |
1 #include "Test.h" | 1 #include "Test.h" |
2 #include "SkLazyPtr.h" | 2 #include "SkLazyPtr.h" |
3 #include "SkTaskGroup.h" | 3 #include "SkTaskGroup.h" |
4 | 4 |
| 5 namespace { |
| 6 |
| 7 struct CreateIntFromFloat { |
| 8 CreateIntFromFloat(float val) : fVal(val) {} |
| 9 int* operator()() const { return SkNEW_ARGS(int, ((int)fVal)); } |
| 10 float fVal; |
| 11 }; |
| 12 |
| 13 // As a template argument this must have external linkage. |
| 14 void custom_destroy(int* ptr) { *ptr = 99; } |
| 15 |
| 16 } // namespace |
| 17 |
5 DEF_TEST(LazyPtr, r) { | 18 DEF_TEST(LazyPtr, r) { |
| 19 // Basic usage: calls SkNEW(int). |
6 SkLazyPtr<int> lazy; | 20 SkLazyPtr<int> lazy; |
7 int* ptr = lazy.get(); | 21 int* ptr = lazy.get(); |
8 | |
9 REPORTER_ASSERT(r, ptr); | 22 REPORTER_ASSERT(r, ptr); |
10 REPORTER_ASSERT(r, lazy.get() == ptr); | 23 REPORTER_ASSERT(r, lazy.get() == ptr); |
11 | 24 |
| 25 // Advanced usage: calls a functor. |
| 26 SkLazyPtr<int> lazyFunctor; |
| 27 int* six = lazyFunctor.get(CreateIntFromFloat(6.4f)); |
| 28 REPORTER_ASSERT(r, six); |
| 29 REPORTER_ASSERT(r, 6 == *six); |
| 30 |
| 31 // Just makes sure this is safe. |
12 SkLazyPtr<double> neverRead; | 32 SkLazyPtr<double> neverRead; |
| 33 |
| 34 // SkLazyPtr supports custom destroy methods. |
| 35 { |
| 36 SkLazyPtr<int, custom_destroy> customDestroy; |
| 37 ptr = customDestroy.get(); |
| 38 // custom_destroy called here. |
| 39 } |
| 40 REPORTER_ASSERT(r, ptr); |
| 41 REPORTER_ASSERT(r, 99 == *ptr); |
| 42 // Since custom_destroy didn't actually delete ptr, we do now. |
| 43 SkDELETE(ptr); |
13 } | 44 } |
14 | 45 |
15 namespace { | 46 namespace { |
16 | 47 |
17 struct Racer : public SkRunnable { | 48 struct Racer : public SkRunnable { |
18 Racer() : fLazy(NULL), fSeen(NULL) {} | 49 Racer() : fLazy(NULL), fSeen(NULL) {} |
19 | 50 |
20 virtual void run() SK_OVERRIDE { fSeen = fLazy->get(); } | 51 virtual void run() SK_OVERRIDE { fSeen = fLazy->get(); } |
21 | 52 |
22 SkLazyPtr<int>* fLazy; | 53 SkLazyPtr<int>* fLazy; |
(...skipping 16 matching lines...) Expand all Loading... |
39 for (int i = 0; i < kRacers; i++) { | 70 for (int i = 0; i < kRacers; i++) { |
40 tg.add(racers + i); | 71 tg.add(racers + i); |
41 } | 72 } |
42 tg.wait(); | 73 tg.wait(); |
43 | 74 |
44 for (int i = 1; i < kRacers; i++) { | 75 for (int i = 1; i < kRacers; i++) { |
45 REPORTER_ASSERT(r, racers[i].fSeen); | 76 REPORTER_ASSERT(r, racers[i].fSeen); |
46 REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen); | 77 REPORTER_ASSERT(r, racers[i].fSeen == racers[0].fSeen); |
47 } | 78 } |
48 } | 79 } |
OLD | NEW |