OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
11 #define SkTemplates_DEFINED | 11 #define SkTemplates_DEFINED |
12 | 12 |
13 #include "SkMath.h" | 13 #include "SkMath.h" |
14 #include "SkTLogic.h" | 14 #include "SkTLogic.h" |
15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
16 #include "SkUniquePtr.h" | |
17 #include <limits.h> | 16 #include <limits.h> |
| 17 #include <memory> |
18 #include <new> | 18 #include <new> |
19 | 19 |
20 /** \file SkTemplates.h | 20 /** \file SkTemplates.h |
21 | 21 |
22 This file contains light-weight template classes for type-safe and exception
-safe | 22 This file contains light-weight template classes for type-safe and exception
-safe |
23 resource management. | 23 resource management. |
24 */ | 24 */ |
25 | 25 |
26 /** | 26 /** |
27 * Marks a local variable as known to be unused (to avoid warnings). | 27 * Marks a local variable as known to be unused (to avoid warnings). |
(...skipping 23 matching lines...) Expand all Loading... |
51 | 51 |
52 /** \class SkAutoTCallVProc | 52 /** \class SkAutoTCallVProc |
53 | 53 |
54 Call a function when this goes out of scope. The template uses two | 54 Call a function when this goes out of scope. The template uses two |
55 parameters, the object, and a function that is to be called in the destructo
r. | 55 parameters, the object, and a function that is to be called in the destructo
r. |
56 If detach() is called, the object reference is set to null. If the object | 56 If detach() is called, the object reference is set to null. If the object |
57 reference is null when the destructor is called, we do not call the | 57 reference is null when the destructor is called, we do not call the |
58 function. | 58 function. |
59 */ | 59 */ |
60 template <typename T, void (*P)(T*)> class SkAutoTCallVProc | 60 template <typename T, void (*P)(T*)> class SkAutoTCallVProc |
61 : public skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>> { | 61 : public std::unique_ptr<T, SkFunctionWrapper<void, T, P>> { |
62 public: | 62 public: |
63 SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>
>(obj) {} | 63 SkAutoTCallVProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<void, T, P>>(
obj) {} |
64 | 64 |
65 operator T*() const { return this->get(); } | 65 operator T*() const { return this->get(); } |
66 T* detach() { return this->release(); } | 66 T* detach() { return this->release(); } |
67 }; | 67 }; |
68 | 68 |
69 /** \class SkAutoTCallIProc | 69 /** \class SkAutoTCallIProc |
70 | 70 |
71 Call a function when this goes out of scope. The template uses two | 71 Call a function when this goes out of scope. The template uses two |
72 parameters, the object, and a function that is to be called in the destructor. | 72 parameters, the object, and a function that is to be called in the destructor. |
73 If detach() is called, the object reference is set to null. If the object | 73 If detach() is called, the object reference is set to null. If the object |
74 reference is null when the destructor is called, we do not call the | 74 reference is null when the destructor is called, we do not call the |
75 function. | 75 function. |
76 */ | 76 */ |
77 template <typename T, int (*P)(T*)> class SkAutoTCallIProc | 77 template <typename T, int (*P)(T*)> class SkAutoTCallIProc |
78 : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> { | 78 : public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> { |
79 public: | 79 public: |
80 SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>>
(obj) {} | 80 SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(o
bj) {} |
81 | 81 |
82 operator T*() const { return this->get(); } | 82 operator T*() const { return this->get(); } |
83 T* detach() { return this->release(); } | 83 T* detach() { return this->release(); } |
84 }; | 84 }; |
85 | 85 |
86 /** \class SkAutoTDelete | 86 /** \class SkAutoTDelete |
87 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> | 87 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> |
88 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T
> | 88 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T
> |
89 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold | 89 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold |
90 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is | 90 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is |
91 thread-compatible, and once you dereference it, you get the threadsafety | 91 thread-compatible, and once you dereference it, you get the threadsafety |
92 guarantees of T. | 92 guarantees of T. |
93 | 93 |
94 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 94 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
95 */ | 95 */ |
96 template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> { | 96 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { |
97 public: | 97 public: |
98 SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {} | 98 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} |
99 | 99 |
100 operator T*() const { return this->get(); } | 100 operator T*() const { return this->get(); } |
101 void free() { this->reset(nullptr); } | 101 void free() { this->reset(nullptr); } |
102 T* detach() { return this->release(); } | 102 T* detach() { return this->release(); } |
| 103 |
| 104 // See SkAutoTUnref for why we do this. |
| 105 explicit operator bool() const { return this->get() != nullptr; } |
103 }; | 106 }; |
104 | 107 |
105 template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> { | 108 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { |
106 public: | 109 public: |
107 SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {} | 110 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} |
108 | 111 |
109 void free() { this->reset(nullptr); } | 112 void free() { this->reset(nullptr); } |
110 T* detach() { return this->release(); } | 113 T* detach() { return this->release(); } |
111 }; | 114 }; |
112 | 115 |
113 /** Allocate an array of T elements, and free the array in the destructor | 116 /** Allocate an array of T elements, and free the array in the destructor |
114 */ | 117 */ |
115 template <typename T> class SkAutoTArray : SkNoncopyable { | 118 template <typename T> class SkAutoTArray : SkNoncopyable { |
116 public: | 119 public: |
117 SkAutoTArray() { | 120 SkAutoTArray() { |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 * Returns void* because this object does not initialize the | 476 * Returns void* because this object does not initialize the |
474 * memory. Use placement new for types that require a cons. | 477 * memory. Use placement new for types that require a cons. |
475 */ | 478 */ |
476 void* get() { return fStorage.get(); } | 479 void* get() { return fStorage.get(); } |
477 const void* get() const { return fStorage.get(); } | 480 const void* get() const { return fStorage.get(); } |
478 private: | 481 private: |
479 SkAlignedSStorage<sizeof(T)*N> fStorage; | 482 SkAlignedSStorage<sizeof(T)*N> fStorage; |
480 }; | 483 }; |
481 | 484 |
482 #endif | 485 #endif |
OLD | NEW |