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

Unified Diff: include/core/SkTemplates.h

Issue 16951004: Enhancements and a fix to templated containers. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 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 side-by-side diff with in-line comments
Download patch
« include/core/SkTArray.h ('K') | « include/core/SkTArray.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: include/core/SkTemplates.h
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index e8a8b61ec88d58c73558bf038791e87f57d67934..49940027609945c9258057a68c45bf8f08d5ca8d 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -228,9 +228,36 @@ private:
*/
template <size_t N, typename T> class SkAutoSTArray : SkNoncopyable {
public:
+ /** Initialize with no objects */
+ SkAutoSTArray() {
+ fArray = NULL;
+ fCount = 0;
+ }
+
/** Allocate count number of T elements
*/
SkAutoSTArray(size_t count) {
+ fArray = NULL;
+ fCount = 0;
+ this->reset(count);
+ }
+
+ ~SkAutoSTArray() {
+ this->reset(0);
+ }
+
+ /** Destroys previous objects in the array and default constructs count number of objects */
+ void reset(size_t count) {
+ T* start = fArray;
+ T* iter = start + fCount;
+ while (iter > start) {
+ (--iter)->~T();
+ }
+
robertphillips 2013/06/13 14:52:42 could recycle the memory if "fCount > count && cou
bsalomon 2013/06/13 15:03:29 Done, but only for exact fit. Not sure how much ex
+ if (fCount > N) {
+ sk_free(fArray);
+ }
+
fCount = count;
if (count > N) {
fArray = (T*) sk_malloc_throw(count * sizeof(T));
@@ -240,24 +267,13 @@ public:
fArray = NULL;
return;
}
- T* iter = fArray;
+ iter = fArray;
T* stop = fArray + count;
while (iter < stop) {
SkNEW_PLACEMENT(iter++, T);
}
}
- ~SkAutoSTArray() {
- T* start = fArray;
- T* iter = start + fCount;
- while (iter > start) {
- (--iter)->~T();
- }
- if (fCount > N) {
- sk_free(fArray);
- }
- }
-
/** Return the number of T elements in the array
*/
size_t count() const { return fCount; }
« include/core/SkTArray.h ('K') | « include/core/SkTArray.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698