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

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

Issue 208393006: Have SkTArray explicitly call default constructors. (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: Created 6 years, 9 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 | « no previous file | 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 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #ifndef SkTArray_DEFINED 8 #ifndef SkTArray_DEFINED
9 #define SkTArray_DEFINED 9 #define SkTArray_DEFINED
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 self->fItemArray[i].~T(); 46 self->fItemArray[i].~T();
47 } 47 }
48 } 48 }
49 49
50 } 50 }
51 51
52 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_ COPY>*, int); 52 template <typename T, bool MEM_COPY> void* operator new(size_t, SkTArray<T, MEM_ COPY>*, int);
53 53
54 /** When MEM_COPY is true T will be bit copied when moved. 54 /** When MEM_COPY is true T will be bit copied when moved.
55 When MEM_COPY is false, T will be copy constructed / destructed. 55 When MEM_COPY is false, T will be copy constructed / destructed.
56 In all cases T's constructor will be called on allocation, 56 In all cases T will be default-initialized on allocation,
57 and its destructor will be called from this object's destructor. 57 and its destructor will be called from this object's destructor.
58 */ 58 */
59 template <typename T, bool MEM_COPY> class SkTArray { 59 template <typename T, bool MEM_COPY> class SkTArray {
60 public: 60 public:
61 /** 61 /**
62 * Creates an empty array with no initial storage 62 * Creates an empty array with no initial storage
63 */ 63 */
64 SkTArray() { 64 SkTArray() {
65 fCount = 0; 65 fCount = 0;
66 fReserveCount = gMIN_ALLOC_COUNT; 66 fReserveCount = gMIN_ALLOC_COUNT;
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 * Number of elements in the array. 166 * Number of elements in the array.
167 */ 167 */
168 int count() const { return fCount; } 168 int count() const { return fCount; }
169 169
170 /** 170 /**
171 * Is the array empty. 171 * Is the array empty.
172 */ 172 */
173 bool empty() const { return !fCount; } 173 bool empty() const { return !fCount; }
174 174
175 /** 175 /**
176 * Adds 1 new default-constructed T value and returns in by reference. Note 176 * Adds 1 new default-initialized T value and returns it by reference. Note
177 * the reference only remains valid until the next call that adds or removes 177 * the reference only remains valid until the next call that adds or removes
178 * elements. 178 * elements.
179 */ 179 */
180 T& push_back() { 180 T& push_back() {
181 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 181 T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
182 SkNEW_PLACEMENT(newT, T); 182 SkNEW_PLACEMENT(newT, T);
183 return *newT; 183 return *newT;
184 } 184 }
185 185
186 /** 186 /**
187 * Version of above that uses a copy constructor to initialize the new item 187 * Version of above that uses a copy constructor to initialize the new item
188 */ 188 */
189 T& push_back(const T& t) { 189 T& push_back(const T& t) {
190 T* newT = reinterpret_cast<T*>(this->push_back_raw(1)); 190 T* newT = reinterpret_cast<T*>(this->push_back_raw(1));
191 SkNEW_PLACEMENT_ARGS(newT, T, (t)); 191 SkNEW_PLACEMENT_ARGS(newT, T, (t));
192 return *newT; 192 return *newT;
193 } 193 }
194 194
195 /** 195 /**
196 * Allocates n more default T values, and returns the address of the start 196 * Allocates n more default-initialized T values, and returns the address of
197 * of that new range. Note: this address is only valid until the next API 197 * the start of that new range. Note: this address is only valid until the
198 * call made on the array that might add or remove elements. 198 * next API call made on the array that might add or remove elements.
199 */ 199 */
200 T* push_back_n(int n) { 200 T* push_back_n(int n) {
201 SkASSERT(n >= 0); 201 SkASSERT(n >= 0);
202 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n)); 202 T* newTs = reinterpret_cast<T*>(this->push_back_raw(n));
203 for (int i = 0; i < n; ++i) { 203 for (int i = 0; i < n; ++i) {
204 SkNEW_PLACEMENT(newTs + i, T); 204 SkNEW_PLACEMENT(newTs + i, T);
205 } 205 }
206 return newTs; 206 return newTs;
207 } 207 }
208 208
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 SkASSERT(n >= 0); 250 SkASSERT(n >= 0);
251 SkASSERT(fCount >= n); 251 SkASSERT(fCount >= n);
252 fCount -= n; 252 fCount -= n;
253 for (int i = 0; i < n; ++i) { 253 for (int i = 0; i < n; ++i) {
254 fItemArray[fCount + i].~T(); 254 fItemArray[fCount + i].~T();
255 } 255 }
256 this->checkRealloc(0); 256 this->checkRealloc(0);
257 } 257 }
258 258
259 /** 259 /**
260 * Pushes or pops from the back to resize. Pushes will be default 260 * Pushes or pops from the back to resize. Pushes will be default
bungeman-skia 2014/03/24 15:45:48 Interestingly, this comment is actually already co
261 * initialized. 261 * initialized.
262 */ 262 */
263 void resize_back(int newCount) { 263 void resize_back(int newCount) {
264 SkASSERT(newCount >= 0); 264 SkASSERT(newCount >= 0);
265 265
266 if (newCount > fCount) { 266 if (newCount > fCount) {
267 this->push_back_n(newCount - fCount); 267 this->push_back_n(newCount - fCount);
268 } else if (newCount < fCount) { 268 } else if (newCount < fCount) {
269 this->pop_back_n(fCount - newCount); 269 this->pop_back_n(fCount - newCount);
270 } 270 }
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 SkSTArray& operator= (const INHERITED& array) { 520 SkSTArray& operator= (const INHERITED& array) {
521 INHERITED::operator=(array); 521 INHERITED::operator=(array);
522 return *this; 522 return *this;
523 } 523 }
524 524
525 private: 525 private:
526 SkAlignedSTStorage<N,T> fStorage; 526 SkAlignedSTStorage<N,T> fStorage;
527 }; 527 };
528 528
529 #endif 529 #endif
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698