| 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 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 } | 46 } |
| 47 | 47 |
| 48 template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper { | 48 template <typename R, typename T, R (*P)(T*)> struct SkFunctionWrapper { |
| 49 R operator()(T* t) { return P(t); } | 49 R operator()(T* t) { return P(t); } |
| 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 release() 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 std::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): std::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(); } | |
| 67 }; | 66 }; |
| 68 | 67 |
| 69 /** \class SkAutoTCallIProc | 68 /** \class SkAutoTCallIProc |
| 70 | 69 |
| 71 Call a function when this goes out of scope. The template uses two | 70 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. | 71 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 | 72 If release() 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 | 73 reference is null when the destructor is called, we do not call the |
| 75 function. | 74 function. |
| 76 */ | 75 */ |
| 77 template <typename T, int (*P)(T*)> class SkAutoTCallIProc | 76 template <typename T, int (*P)(T*)> class SkAutoTCallIProc |
| 78 : public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> { | 77 : public std::unique_ptr<T, SkFunctionWrapper<int, T, P>> { |
| 79 public: | 78 public: |
| 80 SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(o
bj) {} | 79 SkAutoTCallIProc(T* obj): std::unique_ptr<T, SkFunctionWrapper<int, T, P>>(o
bj) {} |
| 81 | 80 |
| 82 operator T*() const { return this->get(); } | 81 operator T*() const { return this->get(); } |
| 83 T* detach() { return this->release(); } | |
| 84 }; | 82 }; |
| 85 | 83 |
| 86 /** \class SkAutoTDelete | 84 /** \class SkAutoTDelete |
| 87 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<
T> | 85 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
> | 86 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 | 87 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 | 88 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 | 89 thread-compatible, and once you dereference it, you get the threadsafety |
| 92 guarantees of T. | 90 guarantees of T. |
| 93 | 91 |
| 94 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 92 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
| 95 */ | 93 */ |
| 96 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { | 94 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { |
| 97 public: | 95 public: |
| 98 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} | 96 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} |
| 99 | 97 |
| 100 operator T*() const { return this->get(); } | 98 operator T*() const { return this->get(); } |
| 101 void free() { this->reset(nullptr); } | 99 void free() { this->reset(nullptr); } |
| 102 T* detach() { return this->release(); } | |
| 103 | 100 |
| 104 // See SkAutoTUnref for why we do this. | 101 // See SkAutoTUnref for why we do this. |
| 105 explicit operator bool() const { return this->get() != nullptr; } | 102 explicit operator bool() const { return this->get() != nullptr; } |
| 106 }; | 103 }; |
| 107 | 104 |
| 108 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { | 105 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { |
| 109 public: | 106 public: |
| 110 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} | 107 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} |
| 111 | 108 |
| 112 void free() { this->reset(nullptr); } | 109 void free() { this->reset(nullptr); } |
| 113 T* detach() { return this->release(); } | |
| 114 }; | 110 }; |
| 115 | 111 |
| 116 /** Allocate an array of T elements, and free the array in the destructor | 112 /** Allocate an array of T elements, and free the array in the destructor |
| 117 */ | 113 */ |
| 118 template <typename T> class SkAutoTArray : SkNoncopyable { | 114 template <typename T> class SkAutoTArray : SkNoncopyable { |
| 119 public: | 115 public: |
| 120 SkAutoTArray() { | 116 SkAutoTArray() { |
| 121 fArray = NULL; | 117 fArray = NULL; |
| 122 SkDEBUGCODE(fCount = 0;) | 118 SkDEBUGCODE(fCount = 0;) |
| 123 } | 119 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 */ | 310 */ |
| 315 void free() { | 311 void free() { |
| 316 this->reset(0); | 312 this->reset(0); |
| 317 } | 313 } |
| 318 | 314 |
| 319 /** | 315 /** |
| 320 * Transfer ownership of the ptr to the caller, setting the internal | 316 * Transfer ownership of the ptr to the caller, setting the internal |
| 321 * pointer to NULL. Note that this differs from get(), which also returns | 317 * pointer to NULL. Note that this differs from get(), which also returns |
| 322 * the pointer, but it does not transfer ownership. | 318 * the pointer, but it does not transfer ownership. |
| 323 */ | 319 */ |
| 324 T* detach() { | 320 T* release() { |
| 325 T* ptr = fPtr; | 321 T* ptr = fPtr; |
| 326 fPtr = NULL; | 322 fPtr = NULL; |
| 327 return ptr; | 323 return ptr; |
| 328 } | 324 } |
| 329 | 325 |
| 330 private: | 326 private: |
| 331 T* fPtr; | 327 T* fPtr; |
| 332 }; | 328 }; |
| 333 | 329 |
| 334 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab
le { | 330 template <size_t kCountRequested, typename T> class SkAutoSTMalloc : SkNoncopyab
le { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 * Returns void* because this object does not initialize the | 472 * Returns void* because this object does not initialize the |
| 477 * memory. Use placement new for types that require a cons. | 473 * memory. Use placement new for types that require a cons. |
| 478 */ | 474 */ |
| 479 void* get() { return fStorage.get(); } | 475 void* get() { return fStorage.get(); } |
| 480 const void* get() const { return fStorage.get(); } | 476 const void* get() const { return fStorage.get(); } |
| 481 private: | 477 private: |
| 482 SkAlignedSStorage<sizeof(T)*N> fStorage; | 478 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 483 }; | 479 }; |
| 484 | 480 |
| 485 #endif | 481 #endif |
| OLD | NEW |