| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold | 131 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold |
| 132 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is | 132 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is |
| 133 thread-compatible, and once you dereference it, you get the threadsafety | 133 thread-compatible, and once you dereference it, you get the threadsafety |
| 134 guarantees of T. | 134 guarantees of T. |
| 135 | 135 |
| 136 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) | 136 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*) |
| 137 */ | 137 */ |
| 138 template <typename T> class SkAutoTDelete : SkNoncopyable { | 138 template <typename T> class SkAutoTDelete : SkNoncopyable { |
| 139 public: | 139 public: |
| 140 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} | 140 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} |
| 141 ~SkAutoTDelete() { SkDELETE(fObj); } | 141 ~SkAutoTDelete() { delete fObj; } |
| 142 | 142 |
| 143 T* get() const { return fObj; } | 143 T* get() const { return fObj; } |
| 144 operator T*() const { return fObj; } | 144 operator T*() const { return fObj; } |
| 145 T& operator*() const { SkASSERT(fObj); return *fObj; } | 145 T& operator*() const { SkASSERT(fObj); return *fObj; } |
| 146 T* operator->() const { SkASSERT(fObj); return fObj; } | 146 T* operator->() const { SkASSERT(fObj); return fObj; } |
| 147 | 147 |
| 148 void reset(T* obj) { | 148 void reset(T* obj) { |
| 149 if (fObj != obj) { | 149 if (fObj != obj) { |
| 150 SkDELETE(fObj); | 150 delete fObj; |
| 151 fObj = obj; | 151 fObj = obj; |
| 152 } | 152 } |
| 153 } | 153 } |
| 154 | 154 |
| 155 /** | 155 /** |
| 156 * Delete the owned object, setting the internal pointer to NULL. | 156 * Delete the owned object, setting the internal pointer to NULL. |
| 157 */ | 157 */ |
| 158 void free() { | 158 void free() { |
| 159 SkDELETE(fObj); | 159 delete fObj; |
| 160 fObj = NULL; | 160 fObj = NULL; |
| 161 } | 161 } |
| 162 | 162 |
| 163 /** | 163 /** |
| 164 * Transfer ownership of the object to the caller, setting the internal | 164 * Transfer ownership of the object to the caller, setting the internal |
| 165 * pointer to NULL. Note that this differs from get(), which also returns | 165 * pointer to NULL. Note that this differs from get(), which also returns |
| 166 * the pointer, but it does not transfer ownership. | 166 * the pointer, but it does not transfer ownership. |
| 167 */ | 167 */ |
| 168 T* detach() { | 168 T* detach() { |
| 169 T* obj = fObj; | 169 T* obj = fObj; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 193 T& operator*() const { SkASSERT(fObj); return *fObj; } | 193 T& operator*() const { SkASSERT(fObj); return *fObj; } |
| 194 T* operator->() const { SkASSERT(fObj); return fObj; } | 194 T* operator->() const { SkASSERT(fObj); return fObj; } |
| 195 | 195 |
| 196 private: | 196 private: |
| 197 T* fObj; | 197 T* fObj; |
| 198 }; | 198 }; |
| 199 | 199 |
| 200 template <typename T> class SkAutoTDeleteArray : SkNoncopyable { | 200 template <typename T> class SkAutoTDeleteArray : SkNoncopyable { |
| 201 public: | 201 public: |
| 202 SkAutoTDeleteArray(T array[]) : fArray(array) {} | 202 SkAutoTDeleteArray(T array[]) : fArray(array) {} |
| 203 ~SkAutoTDeleteArray() { SkDELETE_ARRAY(fArray); } | 203 ~SkAutoTDeleteArray() { delete[] fArray; } |
| 204 | 204 |
| 205 T* get() const { return fArray; } | 205 T* get() const { return fArray; } |
| 206 void free() { SkDELETE_ARRAY(fArray); fArray = NULL; } | 206 void free() { |
| 207 delete[] fArray; |
| 208 fArray = NULL; |
| 209 } |
| 207 T* detach() { T* array = fArray; fArray = NULL; return array; } | 210 T* detach() { T* array = fArray; fArray = NULL; return array; } |
| 208 | 211 |
| 209 void reset(T array[]) { | 212 void reset(T array[]) { |
| 210 if (fArray != array) { | 213 if (fArray != array) { |
| 211 SkDELETE_ARRAY(fArray); | 214 delete[] fArray; |
| 212 fArray = array; | 215 fArray = array; |
| 213 } | 216 } |
| 214 } | 217 } |
| 215 | 218 |
| 216 private: | 219 private: |
| 217 T* fArray; | 220 T* fArray; |
| 218 }; | 221 }; |
| 219 | 222 |
| 220 /** Allocate an array of T elements, and free the array in the destructor | 223 /** Allocate an array of T elements, and free the array in the destructor |
| 221 */ | 224 */ |
| 222 template <typename T> class SkAutoTArray : SkNoncopyable { | 225 template <typename T> class SkAutoTArray : SkNoncopyable { |
| 223 public: | 226 public: |
| 224 SkAutoTArray() { | 227 SkAutoTArray() { |
| 225 fArray = NULL; | 228 fArray = NULL; |
| 226 SkDEBUGCODE(fCount = 0;) | 229 SkDEBUGCODE(fCount = 0;) |
| 227 } | 230 } |
| 228 /** Allocate count number of T elements | 231 /** Allocate count number of T elements |
| 229 */ | 232 */ |
| 230 explicit SkAutoTArray(int count) { | 233 explicit SkAutoTArray(int count) { |
| 231 SkASSERT(count >= 0); | 234 SkASSERT(count >= 0); |
| 232 fArray = NULL; | 235 fArray = NULL; |
| 233 if (count) { | 236 if (count) { |
| 234 fArray = SkNEW_ARRAY(T, count); | 237 fArray = new T[count]; |
| 235 } | 238 } |
| 236 SkDEBUGCODE(fCount = count;) | 239 SkDEBUGCODE(fCount = count;) |
| 237 } | 240 } |
| 238 | 241 |
| 239 /** Reallocates given a new count. Reallocation occurs even if new count equ
als old count. | 242 /** Reallocates given a new count. Reallocation occurs even if new count equ
als old count. |
| 240 */ | 243 */ |
| 241 void reset(int count) { | 244 void reset(int count) { |
| 242 SkDELETE_ARRAY(fArray); | 245 delete[] fArray; |
| 243 SkASSERT(count >= 0); | 246 SkASSERT(count >= 0); |
| 244 fArray = NULL; | 247 fArray = NULL; |
| 245 if (count) { | 248 if (count) { |
| 246 fArray = SkNEW_ARRAY(T, count); | 249 fArray = new T[count]; |
| 247 } | 250 } |
| 248 SkDEBUGCODE(fCount = count;) | 251 SkDEBUGCODE(fCount = count;) |
| 249 } | 252 } |
| 250 | 253 |
| 251 ~SkAutoTArray() { | 254 ~SkAutoTArray() { delete[] fArray; } |
| 252 SkDELETE_ARRAY(fArray); | |
| 253 } | |
| 254 | 255 |
| 255 /** Return the array of T elements. Will be NULL if count == 0 | 256 /** Return the array of T elements. Will be NULL if count == 0 |
| 256 */ | 257 */ |
| 257 T* get() const { return fArray; } | 258 T* get() const { return fArray; } |
| 258 | 259 |
| 259 /** Return the nth element in the array | 260 /** Return the nth element in the array |
| 260 */ | 261 */ |
| 261 T& operator[](int index) const { | 262 T& operator[](int index) const { |
| 262 SkASSERT((unsigned)index < (unsigned)fCount); | 263 SkASSERT((unsigned)index < (unsigned)fCount); |
| 263 return fArray[index]; | 264 return fArray[index]; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 } else { | 323 } else { |
| 323 fArray = NULL; | 324 fArray = NULL; |
| 324 } | 325 } |
| 325 | 326 |
| 326 fCount = count; | 327 fCount = count; |
| 327 } | 328 } |
| 328 | 329 |
| 329 iter = fArray; | 330 iter = fArray; |
| 330 T* stop = fArray + count; | 331 T* stop = fArray + count; |
| 331 while (iter < stop) { | 332 while (iter < stop) { |
| 332 SkNEW_PLACEMENT(iter++, T); | 333 new (iter++) T; |
| 333 } | 334 } |
| 334 } | 335 } |
| 335 | 336 |
| 336 /** Return the number of T elements in the array | 337 /** Return the number of T elements in the array |
| 337 */ | 338 */ |
| 338 int count() const { return fCount; } | 339 int count() const { return fCount; } |
| 339 | 340 |
| 340 /** Return the array of T elements. Will be NULL if count == 0 | 341 /** Return the array of T elements. Will be NULL if count == 0 |
| 341 */ | 342 */ |
| 342 T* get() const { return fArray; } | 343 T* get() const { return fArray; } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 ////////////////////////////////////////////////////////////////////////////////
////////////////// | 493 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
| 493 | 494 |
| 494 /** | 495 /** |
| 495 * Pass the object and the storage that was offered during SkInPlaceNewCheck, a
nd this will | 496 * Pass the object and the storage that was offered during SkInPlaceNewCheck, a
nd this will |
| 496 * safely destroy (and free if it was dynamically allocated) the object. | 497 * safely destroy (and free if it was dynamically allocated) the object. |
| 497 */ | 498 */ |
| 498 template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) { | 499 template <typename T> void SkInPlaceDeleteCheck(T* obj, void* storage) { |
| 499 if (storage == obj) { | 500 if (storage == obj) { |
| 500 obj->~T(); | 501 obj->~T(); |
| 501 } else { | 502 } else { |
| 502 SkDELETE(obj); | 503 delete obj; |
| 503 } | 504 } |
| 504 } | 505 } |
| 505 | 506 |
| 506 /** | 507 /** |
| 507 * Allocates T, using storage if it is large enough, and allocating on the heap
(via new) if | 508 * Allocates T, using storage if it is large enough, and allocating on the heap
(via new) if |
| 508 * storage is not large enough. | 509 * storage is not large enough. |
| 509 * | 510 * |
| 510 * obj = SkInPlaceNewCheck<Type>(storage, size); | 511 * obj = SkInPlaceNewCheck<Type>(storage, size); |
| 511 * ... | 512 * ... |
| 512 * SkInPlaceDeleteCheck(obj, storage); | 513 * SkInPlaceDeleteCheck(obj, storage); |
| 513 */ | 514 */ |
| 514 template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) { | 515 template <typename T> T* SkInPlaceNewCheck(void* storage, size_t size) { |
| 515 return (sizeof(T) <= size) ? new (storage) T : SkNEW(T); | 516 return (sizeof(T) <= size) ? new (storage) T : new T; |
| 516 } | 517 } |
| 517 | 518 |
| 518 template <typename T, typename A1, typename A2, typename A3> | 519 template <typename T, typename A1, typename A2, typename A3> |
| 519 T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, con
st A3& a3) { | 520 T* SkInPlaceNewCheck(void* storage, size_t size, const A1& a1, const A2& a2, con
st A3& a3) { |
| 520 return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : SkNEW_ARGS(T, (a1
, a2, a3)); | 521 return (sizeof(T) <= size) ? new (storage) T(a1, a2, a3) : new T(a1, a2, a3)
; |
| 521 } | 522 } |
| 522 | 523 |
| 523 /** | 524 /** |
| 524 * Reserves memory that is aligned on double and pointer boundaries. | 525 * Reserves memory that is aligned on double and pointer boundaries. |
| 525 * Hopefully this is sufficient for all practical purposes. | 526 * Hopefully this is sufficient for all practical purposes. |
| 526 */ | 527 */ |
| 527 template <size_t N> class SkAlignedSStorage : SkNoncopyable { | 528 template <size_t N> class SkAlignedSStorage : SkNoncopyable { |
| 528 public: | 529 public: |
| 529 size_t size() const { return N; } | 530 size_t size() const { return N; } |
| 530 void* get() { return fData; } | 531 void* get() { return fData; } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 550 * Returns void* because this object does not initialize the | 551 * Returns void* because this object does not initialize the |
| 551 * memory. Use placement new for types that require a cons. | 552 * memory. Use placement new for types that require a cons. |
| 552 */ | 553 */ |
| 553 void* get() { return fStorage.get(); } | 554 void* get() { return fStorage.get(); } |
| 554 const void* get() const { return fStorage.get(); } | 555 const void* get() const { return fStorage.get(); } |
| 555 private: | 556 private: |
| 556 SkAlignedSStorage<sizeof(T)*N> fStorage; | 557 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 557 }; | 558 }; |
| 558 | 559 |
| 559 #endif | 560 #endif |
| OLD | NEW |