aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/core/SkTemplates.h
diff options
context:
space:
mode:
authorGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-06-13 15:13:46 +0000
committerGravatar bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-06-13 15:13:46 +0000
commitd51041469a9a45562d88e9ff137c6726562e8c40 (patch)
tree28158966c82c72a2ea36cef7a50363de1a4eed5d /include/core/SkTemplates.h
parent7fb04bcc31a4deca7445b403b9952f594d49e968 (diff)
Enhancements and a fix to templated containers.
R=robertphillips@google.com Review URL: https://codereview.chromium.org/16951004 git-svn-id: http://skia.googlecode.com/svn/trunk@9582 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/core/SkTemplates.h')
-rw-r--r--include/core/SkTemplates.h52
1 files changed, 36 insertions, 16 deletions
diff --git a/include/core/SkTemplates.h b/include/core/SkTemplates.h
index e8a8b61ec8..3060f9ef27 100644
--- a/include/core/SkTemplates.h
+++ b/include/core/SkTemplates.h
@@ -228,33 +228,53 @@ 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) {
- fCount = count;
- if (count > N) {
- fArray = (T*) sk_malloc_throw(count * sizeof(T));
- } else if (count > 0) {
- fArray = (T*) fStorage;
- } else {
- fArray = NULL;
- return;
- }
- T* iter = fArray;
- T* stop = fArray + count;
- while (iter < stop) {
- SkNEW_PLACEMENT(iter++, T);
- }
+ 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();
}
- if (fCount > N) {
- sk_free(fArray);
+
+ if (fCount != count) {
+ if (count > N) {
+ sk_free(fArray);
+ }
+
+ if (count > N) {
+ fArray = (T*) sk_malloc_throw(count * sizeof(T));
+ } else if (count > 0) {
+ fArray = (T*) fStorage;
+ } else {
+ fArray = NULL;
+ return;
+ }
+
+ fCount = count;
+ }
+
+ iter = fArray;
+ T* stop = fArray + count;
+ while (iter < stop) {
+ SkNEW_PLACEMENT(iter++, T);
}
}