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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 thread-compatible, and once you dereference it, you get the threadsafety | 89 thread-compatible, and once you dereference it, you get the threadsafety |
90 guarantees of T. | 90 guarantees of T. |
91 | 91 |
92 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*) |
93 */ | 93 */ |
94 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { | 94 template <typename T> class SkAutoTDelete : public std::unique_ptr<T> { |
95 public: | 95 public: |
96 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} | 96 SkAutoTDelete(T* obj = NULL) : std::unique_ptr<T>(obj) {} |
97 | 97 |
98 operator T*() const { return this->get(); } | 98 operator T*() const { return this->get(); } |
99 void free() { this->reset(nullptr); } | |
100 | 99 |
101 // See SkAutoTUnref for why we do this. | 100 // See SkAutoTUnref for why we do this. |
102 explicit operator bool() const { return this->get() != nullptr; } | 101 explicit operator bool() const { return this->get() != nullptr; } |
103 }; | 102 }; |
104 | 103 |
105 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { | 104 template <typename T> class SkAutoTDeleteArray : public std::unique_ptr<T[]> { |
106 public: | 105 public: |
107 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} | 106 SkAutoTDeleteArray(T array[]) : std::unique_ptr<T[]>(array) {} |
108 | |
109 void free() { this->reset(nullptr); } | |
110 }; | 107 }; |
111 | 108 |
112 /** Allocate an array of T elements, and free the array in the destructor | 109 /** Allocate an array of T elements, and free the array in the destructor |
113 */ | 110 */ |
114 template <typename T> class SkAutoTArray : SkNoncopyable { | 111 template <typename T> class SkAutoTArray : SkNoncopyable { |
115 public: | 112 public: |
116 SkAutoTArray() { | 113 SkAutoTArray() { |
117 fArray = NULL; | 114 fArray = NULL; |
118 SkDEBUGCODE(fCount = 0;) | 115 SkDEBUGCODE(fCount = 0;) |
119 } | 116 } |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 ~SkAutoTMalloc() { | 271 ~SkAutoTMalloc() { |
275 sk_free(fPtr); | 272 sk_free(fPtr); |
276 } | 273 } |
277 | 274 |
278 /** Resize the memory area pointed to by the current ptr preserving contents
. */ | 275 /** Resize the memory area pointed to by the current ptr preserving contents
. */ |
279 void realloc(size_t count) { | 276 void realloc(size_t count) { |
280 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); | 277 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); |
281 } | 278 } |
282 | 279 |
283 /** Resize the memory area pointed to by the current ptr without preserving
contents. */ | 280 /** Resize the memory area pointed to by the current ptr without preserving
contents. */ |
284 T* reset(size_t count) { | 281 T* reset(size_t count = 0) { |
285 sk_free(fPtr); | 282 sk_free(fPtr); |
286 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW); | 283 fPtr = count ? (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW) :
nullptr; |
287 return fPtr; | 284 return fPtr; |
288 } | 285 } |
289 | 286 |
290 T* get() const { return fPtr; } | 287 T* get() const { return fPtr; } |
291 | 288 |
292 operator T*() { | 289 operator T*() { |
293 return fPtr; | 290 return fPtr; |
294 } | 291 } |
295 | 292 |
296 operator const T*() const { | 293 operator const T*() const { |
297 return fPtr; | 294 return fPtr; |
298 } | 295 } |
299 | 296 |
300 T& operator[](int index) { | 297 T& operator[](int index) { |
301 return fPtr[index]; | 298 return fPtr[index]; |
302 } | 299 } |
303 | 300 |
304 const T& operator[](int index) const { | 301 const T& operator[](int index) const { |
305 return fPtr[index]; | 302 return fPtr[index]; |
306 } | 303 } |
307 | 304 |
308 /** | 305 /** |
309 * Releases the block back to the heap | |
310 */ | |
311 void free() { | |
312 this->reset(0); | |
313 } | |
314 | |
315 /** | |
316 * Transfer ownership of the ptr to the caller, setting the internal | 306 * Transfer ownership of the ptr to the caller, setting the internal |
317 * pointer to NULL. Note that this differs from get(), which also returns | 307 * pointer to NULL. Note that this differs from get(), which also returns |
318 * the pointer, but it does not transfer ownership. | 308 * the pointer, but it does not transfer ownership. |
319 */ | 309 */ |
320 T* release() { | 310 T* release() { |
321 T* ptr = fPtr; | 311 T* ptr = fPtr; |
322 fPtr = NULL; | 312 fPtr = NULL; |
323 return ptr; | 313 return ptr; |
324 } | 314 } |
325 | 315 |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 * Returns void* because this object does not initialize the | 462 * Returns void* because this object does not initialize the |
473 * memory. Use placement new for types that require a cons. | 463 * memory. Use placement new for types that require a cons. |
474 */ | 464 */ |
475 void* get() { return fStorage.get(); } | 465 void* get() { return fStorage.get(); } |
476 const void* get() const { return fStorage.get(); } | 466 const void* get() const { return fStorage.get(); } |
477 private: | 467 private: |
478 SkAlignedSStorage<sizeof(T)*N> fStorage; | 468 SkAlignedSStorage<sizeof(T)*N> fStorage; |
479 }; | 469 }; |
480 | 470 |
481 #endif | 471 #endif |
OLD | NEW |