| 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 22 matching lines...) Expand all Loading... |
| 50 }; | 50 }; |
| 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>> { | |
| 62 public: | 61 public: |
| 63 SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>
>(obj) {} | 62 SkAutoTCallVProc(T* obj) : fPtr(obj) {} |
| 64 | 63 |
| 65 operator T*() const { return this->get(); } | 64 T* get() const { return fPtr.get(); } |
| 66 T* detach() { return this->release(); } | 65 operator T* () const { return fPtr.get(); } |
| 66 T* operator->() const { return fPtr.get(); } |
| 67 |
| 68 T* detach() { return fPtr.release(); } |
| 69 void reset(T* ptr = nullptr) { fPtr.reset(ptr); } |
| 70 private: |
| 71 std::unique_ptr<T, SkFunctionWrapper<void, T, P>> fPtr; |
| 67 }; | 72 }; |
| 68 | 73 |
| 69 /** \class SkAutoTCallIProc | 74 /** \class SkAutoTCallIProc |
| 70 | 75 |
| 71 Call a function when this goes out of scope. The template uses two | 76 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. | 77 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 | 78 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 | 79 reference is null when the destructor is called, we do not call the |
| 75 function. | 80 function. |
| 76 */ | 81 */ |
| 77 template <typename T, int (*P)(T*)> class SkAutoTCallIProc | 82 template <typename T, int (*P)(T*)> class SkAutoTCallIProc { |
| 78 : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> { | |
| 79 public: | 83 public: |
| 80 SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>>
(obj) {} | 84 SkAutoTCallIProc(T* obj) : fPtr(obj) {} |
| 81 | 85 |
| 82 operator T*() const { return this->get(); } | 86 T* get() const { return fPtr.get(); } |
| 83 T* detach() { return this->release(); } | 87 operator T* () const { return fPtr.get(); } |
| 88 T* operator->() const { return fPtr.get(); } |
| 89 |
| 90 T* detach() { return fPtr.release(); } |
| 91 void reset(T* ptr = nullptr) { fPtr.reset(ptr); } |
| 92 private: |
| 93 std::unique_ptr<T, SkFunctionWrapper<int, T, P>> fPtr; |
| 84 }; | 94 }; |
| 85 | 95 |
| 86 /** \class SkAutoTDelete | 96 /** \class SkAutoTDelete |
| 87 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> | 97 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
> | 98 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 | 99 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 | 100 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 | 101 thread-compatible, and once you dereference it, you get the threadsafety |
| 92 guarantees of T. | 102 guarantees of T. |
| 93 | 103 |
| 94 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 104 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
| 95 */ | 105 */ |
| 96 template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> { | 106 template <typename T> class SkAutoTDelete { |
| 97 public: | 107 public: |
| 98 SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {} | 108 SkAutoTDelete(T* obj = NULL) : fPtr(obj) {} |
| 99 | 109 |
| 100 operator T*() const { return this->get(); } | 110 void swap(SkAutoTDelete& other) { fPtr.swap(other.fPtr); } |
| 101 void free() { this->reset(nullptr); } | 111 |
| 102 T* detach() { return this->release(); } | 112 T* get() const { return fPtr.get(); } |
| 113 operator T* () const { return fPtr.get(); } |
| 114 T* operator->() const { return fPtr.get(); } |
| 115 |
| 116 void reset(T* ptr = nullptr) { fPtr.reset(ptr); } |
| 117 void free() { fPtr.reset(nullptr); } |
| 118 T* detach() { return fPtr.release(); } |
| 119 T* release() { return fPtr.release(); } |
| 120 private: |
| 121 std::unique_ptr<T> fPtr; |
| 103 }; | 122 }; |
| 104 | 123 |
| 105 template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> { | 124 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { |
| 106 public: | 125 public: |
| 107 SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {} | 126 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} |
| 108 | 127 |
| 109 void free() { this->reset(nullptr); } | 128 void free() { this->reset(nullptr); } |
| 110 T* detach() { return this->release(); } | 129 T* detach() { return this->release(); } |
| 111 }; | 130 }; |
| 112 | 131 |
| 113 /** Allocate an array of T elements, and free the array in the destructor | 132 /** Allocate an array of T elements, and free the array in the destructor |
| 114 */ | 133 */ |
| 115 template <typename T> class SkAutoTArray : SkNoncopyable { | 134 template <typename T> class SkAutoTArray : SkNoncopyable { |
| 116 public: | 135 public: |
| 117 SkAutoTArray() { | 136 SkAutoTArray() { |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 * Returns void* because this object does not initialize the | 468 * Returns void* because this object does not initialize the |
| 450 * memory. Use placement new for types that require a cons. | 469 * memory. Use placement new for types that require a cons. |
| 451 */ | 470 */ |
| 452 void* get() { return fStorage.get(); } | 471 void* get() { return fStorage.get(); } |
| 453 const void* get() const { return fStorage.get(); } | 472 const void* get() const { return fStorage.get(); } |
| 454 private: | 473 private: |
| 455 SkAlignedSStorage<sizeof(T)*N> fStorage; | 474 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 456 }; | 475 }; |
| 457 | 476 |
| 458 #endif | 477 #endif |
| OLD | NEW |