| 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" |
| 16 #include "SkUtility.h" | 17 #include "SkUtility.h" |
| 17 #include <limits.h> | 18 #include <limits.h> |
| 18 #include <memory> | |
| 19 #include <new> | 19 #include <new> |
| 20 | 20 |
| 21 /** \file SkTemplates.h | 21 /** \file SkTemplates.h |
| 22 | 22 |
| 23 This file contains light-weight template classes for type-safe and exception
-safe | 23 This file contains light-weight template classes for type-safe and exception
-safe |
| 24 resource management. | 24 resource management. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 /** | 27 /** |
| 28 * Marks a local variable as known to be unused (to avoid warnings). | 28 * Marks a local variable as known to be unused (to avoid warnings). |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 | 52 |
| 53 /** \class SkAutoTCallVProc | 53 /** \class SkAutoTCallVProc |
| 54 | 54 |
| 55 Call a function when this goes out of scope. The template uses two | 55 Call a function when this goes out of scope. The template uses two |
| 56 parameters, the object, and a function that is to be called in the destructo
r. | 56 parameters, the object, and a function that is to be called in the destructo
r. |
| 57 If detach() is called, the object reference is set to null. If the object | 57 If detach() is called, the object reference is set to null. If the object |
| 58 reference is null when the destructor is called, we do not call the | 58 reference is null when the destructor is called, we do not call the |
| 59 function. | 59 function. |
| 60 */ | 60 */ |
| 61 template <typename T, void (*P)(T*)> class SkAutoTCallVProc | 61 template <typename T, void (*P)(T*)> class SkAutoTCallVProc |
| 62 : public std::unique_ptr<T, SkFunctionWrapper<void, T, P>> { | 62 : public skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>> { |
| 63 public: | 63 public: |
| 64 SkAutoTCallVProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<void, T, P>>(
obj) {} | 64 SkAutoTCallVProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<void, T, P>
>(obj) {} |
| 65 | 65 |
| 66 operator T*() const { return this->get(); } | 66 operator T*() const { return this->get(); } |
| 67 T* detach() { return this->release(); } | 67 T* detach() { return this->release(); } |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 /** \class SkAutoTCallIProc | 70 /** \class SkAutoTCallIProc |
| 71 | 71 |
| 72 Call a function when this goes out of scope. The template uses two | 72 Call a function when this goes out of scope. The template uses two |
| 73 parameters, the object, and a function that is to be called in the destructor. | 73 parameters, the object, and a function that is to be called in the destructor. |
| 74 If detach() is called, the object reference is set to null. If the object | 74 If detach() is called, the object reference is set to null. If the object |
| 75 reference is null when the destructor is called, we do not call the | 75 reference is null when the destructor is called, we do not call the |
| 76 function. | 76 function. |
| 77 */ | 77 */ |
| 78 template <typename T, int (*P)(T*)> class SkAutoTCallIProc | 78 template <typename T, int (*P)(T*)> class SkAutoTCallIProc |
| 79 : public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> { | 79 : public skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>> { |
| 80 public: | 80 public: |
| 81 SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(o
bj) {} | 81 SkAutoTCallIProc(T* obj): skstd::unique_ptr<T, SkFunctionWrapper<int, T, P>>
(obj) {} |
| 82 | 82 |
| 83 operator T*() const { return this->get(); } | 83 operator T*() const { return this->get(); } |
| 84 T* detach() { return this->release(); } | 84 T* detach() { return this->release(); } |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 /** \class SkAutoTDelete | 87 /** \class SkAutoTDelete |
| 88 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> | 88 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> |
| 89 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T
> | 89 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T
> |
| 90 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold | 90 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold |
| 91 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is | 91 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is |
| 92 thread-compatible, and once you dereference it, you get the threadsafety | 92 thread-compatible, and once you dereference it, you get the threadsafety |
| 93 guarantees of T. | 93 guarantees of T. |
| 94 | 94 |
| 95 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 95 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
| 96 */ | 96 */ |
| 97 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { | 97 template <typename T> class SkAutoTDelete : public skstd::unique_ptr<T> { |
| 98 public: | 98 public: |
| 99 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} | 99 SkAutoTDelete(T* obj = NULL) : skstd::unique_ptr<T>(obj) {} |
| 100 | 100 |
| 101 operator T*() const { return this->get(); } | 101 operator T*() const { return this->get(); } |
| 102 void free() { this->reset(nullptr); } | 102 void free() { this->reset(nullptr); } |
| 103 T* detach() { return this->release(); } | 103 T* detach() { return this->release(); } |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { | 106 template <typename T> class SkAutoTDeleteArray : public skstd::unique_ptr<T[]> { |
| 107 public: | 107 public: |
| 108 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} | 108 SkAutoTDeleteArray(T array[]) : skstd::unique_ptr<T[]>(array) {} |
| 109 | 109 |
| 110 void free() { this->reset(nullptr); } | 110 void free() { this->reset(nullptr); } |
| 111 T* detach() { return this->release(); } | 111 T* detach() { return this->release(); } |
| 112 }; | 112 }; |
| 113 | 113 |
| 114 /** Allocate an array of T elements, and free the array in the destructor | 114 /** Allocate an array of T elements, and free the array in the destructor |
| 115 */ | 115 */ |
| 116 template <typename T> class SkAutoTArray : SkNoncopyable { | 116 template <typename T> class SkAutoTArray : SkNoncopyable { |
| 117 public: | 117 public: |
| 118 SkAutoTArray() { | 118 SkAutoTArray() { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 * Returns void* because this object does not initialize the | 442 * Returns void* because this object does not initialize the |
| 443 * memory. Use placement new for types that require a cons. | 443 * memory. Use placement new for types that require a cons. |
| 444 */ | 444 */ |
| 445 void* get() { return fStorage.get(); } | 445 void* get() { return fStorage.get(); } |
| 446 const void* get() const { return fStorage.get(); } | 446 const void* get() const { return fStorage.get(); } |
| 447 private: | 447 private: |
| 448 SkAlignedSStorage<sizeof(T)*N> fStorage; | 448 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 449 }; | 449 }; |
| 450 | 450 |
| 451 #endif | 451 #endif |
| OLD | NEW |