Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: include/core/SkTemplates.h

Issue 15739013: Use macros for new and delete in SkTemplates.h (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « include/core/SkPostConfig.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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 if (count > N) { 229 if (count > N) {
230 fArray = new T[count]; 230 fArray = SkNEW_ARRAY(T, count);
231 } else if (count) { 231 } else if (count) {
232 fArray = new (fStorage) T[count]; 232 fArray = SkNEW_PLACEMENT_ARRAY(fStorage, T, count);
bungeman-skia 2013/05/25 03:22:07 I think this is a bug. You can never be sure how m
233 } else { 233 } else {
234 fArray = NULL; 234 fArray = NULL;
235 } 235 }
236 fCount = count; 236 fCount = count;
237 } 237 }
238 238
239 ~SkAutoSTArray() { 239 ~SkAutoSTArray() {
240 if (fCount > N) { 240 if (fCount > N) {
241 delete[] fArray; 241 SkDELETE_ARRAY(fArray);
242 } else { 242 } else {
243 T* start = fArray; 243 T* start = fArray;
244 T* iter = start + fCount; 244 T* iter = start + fCount;
245 while (iter > start) { 245 while (iter > start) {
246 (--iter)->~T(); 246 (--iter)->~T();
247 } 247 }
248 } 248 }
249 } 249 }
250 250
251 /** Return the number of T elements in the array 251 /** Return the number of T elements in the array
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
421 /** 421 /**
422 * Returns void* because this object does not initialize the 422 * Returns void* because this object does not initialize the
423 * memory. Use placement new for types that require a cons. 423 * memory. Use placement new for types that require a cons.
424 */ 424 */
425 void* get() { return fStorage.get(); } 425 void* get() { return fStorage.get(); }
426 private: 426 private:
427 SkAlignedSStorage<sizeof(T)*N> fStorage; 427 SkAlignedSStorage<sizeof(T)*N> fStorage;
428 }; 428 };
429 429
430 #endif 430 #endif
OLDNEW
« no previous file with comments | « include/core/SkPostConfig.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698