| 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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 SkAutoTCallIProc(T* obj): fObj(obj) {} | 94 SkAutoTCallIProc(T* obj): fObj(obj) {} |
| 95 ~SkAutoTCallIProc() { if (fObj) P(fObj); } | 95 ~SkAutoTCallIProc() { if (fObj) P(fObj); } |
| 96 T* detach() { T* obj = fObj; fObj = NULL; return obj; } | 96 T* detach() { T* obj = fObj; fObj = NULL; return obj; } |
| 97 private: | 97 private: |
| 98 T* fObj; | 98 T* fObj; |
| 99 }; | 99 }; |
| 100 | 100 |
| 101 template <typename T> class SkAutoTDelete : SkNoncopyable { | 101 template <typename T> class SkAutoTDelete : SkNoncopyable { |
| 102 public: | 102 public: |
| 103 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} | 103 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} |
| 104 ~SkAutoTDelete() { delete fObj; } | 104 ~SkAutoTDelete() { SkDELETE(fObj); } |
| 105 | 105 |
| 106 T* get() const { return fObj; } | 106 T* get() const { return fObj; } |
| 107 T& operator*() const { SkASSERT(fObj); return *fObj; } | 107 T& operator*() const { SkASSERT(fObj); return *fObj; } |
| 108 T* operator->() const { SkASSERT(fObj); return fObj; } | 108 T* operator->() const { SkASSERT(fObj); return fObj; } |
| 109 | 109 |
| 110 void reset(T* obj) { | 110 void reset(T* obj) { |
| 111 if (fObj != obj) { | 111 if (fObj != obj) { |
| 112 delete fObj; | 112 SkDELETE(fObj); |
| 113 fObj = obj; | 113 fObj = obj; |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 /** | 117 /** |
| 118 * Delete the owned object, setting the internal pointer to NULL. | 118 * Delete the owned object, setting the internal pointer to NULL. |
| 119 */ | 119 */ |
| 120 void free() { | 120 void free() { |
| 121 delete fObj; | 121 SkDELETE(fObj); |
| 122 fObj = NULL; | 122 fObj = NULL; |
| 123 } | 123 } |
| 124 | 124 |
| 125 /** | 125 /** |
| 126 * Transfer ownership of the object to the caller, setting the internal | 126 * Transfer ownership of the object to the caller, setting the internal |
| 127 * pointer to NULL. Note that this differs from get(), which also returns | 127 * pointer to NULL. Note that this differs from get(), which also returns |
| 128 * the pointer, but it does not transfer ownership. | 128 * the pointer, but it does not transfer ownership. |
| 129 */ | 129 */ |
| 130 T* detach() { | 130 T* detach() { |
| 131 T* obj = fObj; | 131 T* obj = fObj; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 SkAutoTArray() { | 175 SkAutoTArray() { |
| 176 fArray = NULL; | 176 fArray = NULL; |
| 177 SkDEBUGCODE(fCount = 0;) | 177 SkDEBUGCODE(fCount = 0;) |
| 178 } | 178 } |
| 179 /** Allocate count number of T elements | 179 /** Allocate count number of T elements |
| 180 */ | 180 */ |
| 181 explicit SkAutoTArray(int count) { | 181 explicit SkAutoTArray(int count) { |
| 182 SkASSERT(count >= 0); | 182 SkASSERT(count >= 0); |
| 183 fArray = NULL; | 183 fArray = NULL; |
| 184 if (count) { | 184 if (count) { |
| 185 fArray = new T[count]; | 185 fArray = SkNEW_ARRAY(T, count); |
| 186 } | 186 } |
| 187 SkDEBUGCODE(fCount = count;) | 187 SkDEBUGCODE(fCount = count;) |
| 188 } | 188 } |
| 189 | 189 |
| 190 /** Reallocates given a new count. Reallocation occurs even if new count equ
als old count. | 190 /** Reallocates given a new count. Reallocation occurs even if new count equ
als old count. |
| 191 */ | 191 */ |
| 192 void reset(int count) { | 192 void reset(int count) { |
| 193 delete[] fArray; | 193 SkDELETE_ARRAY(fArray); |
| 194 SkASSERT(count >= 0); | 194 SkASSERT(count >= 0); |
| 195 fArray = NULL; | 195 fArray = NULL; |
| 196 if (count) { | 196 if (count) { |
| 197 fArray = new T[count]; | 197 fArray = SkNEW_ARRAY(T, count); |
| 198 } | 198 } |
| 199 SkDEBUGCODE(fCount = count;) | 199 SkDEBUGCODE(fCount = count;) |
| 200 } | 200 } |
| 201 | 201 |
| 202 ~SkAutoTArray() { | 202 ~SkAutoTArray() { |
| 203 delete[] fArray; | 203 SkDELETE_ARRAY(fArray); |
| 204 } | 204 } |
| 205 | 205 |
| 206 /** Return the array of T elements. Will be NULL if count == 0 | 206 /** Return the array of T elements. Will be NULL if count == 0 |
| 207 */ | 207 */ |
| 208 T* get() const { return fArray; } | 208 T* get() const { return fArray; } |
| 209 | 209 |
| 210 /** Return the nth element in the array | 210 /** Return the nth element in the array |
| 211 */ | 211 */ |
| 212 T& operator[](int index) const { | 212 T& operator[](int index) const { |
| 213 SkASSERT((unsigned)index < (unsigned)fCount); | 213 SkASSERT((unsigned)index < (unsigned)fCount); |
| 214 return fArray[index]; | 214 return fArray[index]; |
| 215 } | 215 } |
| 216 | 216 |
| 217 private: | 217 private: |
| 218 T* fArray; | 218 T* fArray; |
| 219 SkDEBUGCODE(int fCount;) | 219 SkDEBUGCODE(int fCount;) |
| 220 }; | 220 }; |
| 221 | 221 |
| 222 /** Wraps SkAutoTArray, with room for up to N elements preallocated | 222 /** Wraps SkAutoTArray, with room for up to N elements preallocated |
| 223 */ | 223 */ |
| 224 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { | 224 template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable { |
| 225 public: | 225 public: |
| 226 /** Allocate count number of T elements | 226 /** Allocate count number of T elements |
| 227 */ | 227 */ |
| 228 SkAutoSTArray(size_t count) { | 228 SkAutoSTArray(size_t count) { |
| 229 fCount = count; |
| 229 if (count > N) { | 230 if (count > N) { |
| 230 fArray = new T[count]; | 231 fArray = (T*) sk_malloc_throw(count * sizeof(T)); |
| 231 } else if (count) { | 232 } else if (count > 0) { |
| 232 fArray = new (fStorage) T[count]; | 233 fArray = (T*) fStorage; |
| 233 } else { | 234 } else { |
| 234 fArray = NULL; | 235 fArray = NULL; |
| 236 return; |
| 235 } | 237 } |
| 236 fCount = count; | 238 T* iter = fArray; |
| 239 T* stop = fArray + count; |
| 240 while (iter < stop) { |
| 241 SkNEW_PLACEMENT(iter++, T); |
| 242 } |
| 237 } | 243 } |
| 238 | 244 |
| 239 ~SkAutoSTArray() { | 245 ~SkAutoSTArray() { |
| 246 T* start = fArray; |
| 247 T* iter = start + fCount; |
| 248 while (iter > start) { |
| 249 (--iter)->~T(); |
| 250 } |
| 240 if (fCount > N) { | 251 if (fCount > N) { |
| 241 delete[] fArray; | 252 sk_free(fArray); |
| 242 } else { | |
| 243 T* start = fArray; | |
| 244 T* iter = start + fCount; | |
| 245 while (iter > start) { | |
| 246 (--iter)->~T(); | |
| 247 } | |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 | 255 |
| 251 /** Return the number of T elements in the array | 256 /** Return the number of T elements in the array |
| 252 */ | 257 */ |
| 253 size_t count() const { return fCount; } | 258 size_t count() const { return fCount; } |
| 254 | 259 |
| 255 /** Return the array of T elements. Will be NULL if count == 0 | 260 /** Return the array of T elements. Will be NULL if count == 0 |
| 256 */ | 261 */ |
| 257 T* get() const { return fArray; } | 262 T* get() const { return fArray; } |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 /** | 426 /** |
| 422 * Returns void* because this object does not initialize the | 427 * Returns void* because this object does not initialize the |
| 423 * memory. Use placement new for types that require a cons. | 428 * memory. Use placement new for types that require a cons. |
| 424 */ | 429 */ |
| 425 void* get() { return fStorage.get(); } | 430 void* get() { return fStorage.get(); } |
| 426 private: | 431 private: |
| 427 SkAlignedSStorage<sizeof(T)*N> fStorage; | 432 SkAlignedSStorage<sizeof(T)*N> fStorage; |
| 428 }; | 433 }; |
| 429 | 434 |
| 430 #endif | 435 #endif |
| OLD | NEW |