OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "Test.h" | 7 #include "Test.h" |
8 #include "SkTemplates.h" | 8 #include "SkTemplates.h" |
9 | 9 |
10 namespace { | 10 namespace { |
11 class Moveable { | 11 class Moveable { |
12 public: | 12 public: |
13 Moveable() {} | 13 Moveable() {} |
14 Moveable(Moveable&&) {} | 14 Moveable(Moveable&&) {} |
15 Moveable& operator=(Moveable&&) { return *this; } | 15 Moveable& operator=(Moveable&&) { return *this; } |
16 private: | 16 private: |
17 Moveable(const Moveable&); | 17 Moveable(const Moveable&); |
18 Moveable& operator=(const Moveable&); | 18 Moveable& operator=(const Moveable&); |
19 }; | 19 }; |
20 template <typename T> void deleter(T*) { } | 20 template <typename T> void deleter(T*) { } |
21 template <typename T> struct Deleter { | 21 template <typename T> struct Deleter { |
22 void operator()(T* t) { delete static_cast<const Moveable*>(t); } | 22 void operator()(T* t) { delete static_cast<const Moveable*>(t); } |
23 }; | 23 }; |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) { | 26 DEF_TEST(CPlusPlusEleven_RvalueAndMove, r) { |
27 Moveable src1; Moveable dst1(skstd::move(src1)); | 27 Moveable src1; Moveable dst1(skstd::move(src1)); |
28 Moveable src2, dst2; dst2 = skstd::move(src2); | 28 Moveable src2, dst2; dst2 = skstd::move(src2); |
29 } | 29 } |
| 30 |
| 31 #define TOO_BIG "The unique_ptr was bigger than expected." |
| 32 #define WEIRD_SIZE "The unique_ptr was a different size than expected." |
| 33 |
| 34 DEF_TEST(CPlusPlusEleven_UniquePtr, r) { |
| 35 struct SmallUniquePtr { |
| 36 Moveable* p; |
| 37 }; |
| 38 struct BigUniquePtr { |
| 39 void(*d)(Moveable*); |
| 40 Moveable* p; |
| 41 }; |
| 42 |
| 43 static_assert(sizeof(skstd::unique_ptr<Moveable>) == sizeof(SmallUniquePtr),
TOO_BIG); |
| 44 static_assert(sizeof(skstd::unique_ptr<Moveable[]>) == sizeof(SmallUniquePtr
), TOO_BIG); |
| 45 |
| 46 using proc = void(*)(Moveable*); |
| 47 static_assert(sizeof(skstd::unique_ptr<Moveable, proc>) == sizeof(BigUniqueP
tr), WEIRD_SIZE); |
| 48 static_assert(sizeof(skstd::unique_ptr<Moveable[], proc>) == sizeof(BigUniqu
ePtr), WEIRD_SIZE); |
| 49 |
| 50 { |
| 51 skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, deleter<Movea
ble>); |
| 52 static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 53 |
| 54 auto u2 = skstd::move(u); |
| 55 static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 56 } |
| 57 |
| 58 { |
| 59 skstd::unique_ptr<Moveable, void(*)(Moveable*)> u(nullptr, [](Moveable*
m){ deleter(m); }); |
| 60 static_assert(sizeof(u) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 61 |
| 62 auto u2 = skstd::move(u); |
| 63 static_assert(sizeof(u2) == sizeof(BigUniquePtr), WEIRD_SIZE); |
| 64 } |
| 65 |
| 66 { |
| 67 auto d = [](Moveable* m){ deleter(m); }; |
| 68 skstd::unique_ptr<Moveable, decltype(d)> u(nullptr, d); |
| 69 static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 70 |
| 71 auto u2 = skstd::move(u); |
| 72 static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 73 } |
| 74 |
| 75 { |
| 76 skstd::unique_ptr<Moveable, Deleter<Moveable>> u(nullptr, Deleter<Moveab
le>()); |
| 77 static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 78 |
| 79 auto u2 = skstd::move(u); |
| 80 static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 81 } |
| 82 |
| 83 { |
| 84 skstd::unique_ptr<Moveable, Deleter<Moveable>> u(new Moveable(), Deleter
<Moveable>()); |
| 85 static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 86 |
| 87 auto u2 = skstd::move(u); |
| 88 static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 89 } |
| 90 |
| 91 { |
| 92 skstd::unique_ptr<const void, Deleter<const void>> u(new Moveable(), Del
eter<const void>()); |
| 93 static_assert(sizeof(u) == sizeof(SmallUniquePtr), TOO_BIG); |
| 94 |
| 95 auto u2 = skstd::move(u); |
| 96 static_assert(sizeof(u2) == sizeof(SmallUniquePtr), TOO_BIG); |
| 97 } |
| 98 } |
OLD | NEW |