SkTArray: clean up, no change to behaviour
Change-Id: I15883216995a0ffe1ee1b183291cf0ea5867f613
Reviewed-on: https://skia-review.googlesource.com/c/161042
Commit-Queue: Hal Canary <halcanary@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/include/private/SkTArray.h b/include/private/SkTArray.h
index 75cd001..155f79a 100644
--- a/include/private/SkTArray.h
+++ b/include/private/SkTArray.h
@@ -63,9 +63,7 @@
if (this == &that) {
return *this;
}
- for (int i = 0; i < fCount; ++i) {
- fItemArray[i].~T();
- }
+ this->forEach([](T& o) { o.~T(); });
fCount = 0;
this->checkRealloc(that.count());
fCount = that.count();
@@ -76,9 +74,7 @@
if (this == &that) {
return *this;
}
- for (int i = 0; i < fCount; ++i) {
- fItemArray[i].~T();
- }
+ this->forEach([](T& o) { o.~T(); });
fCount = 0;
this->checkRealloc(that.count());
fCount = that.count();
@@ -88,9 +84,7 @@
}
~SkTArray() {
- for (int i = 0; i < fCount; ++i) {
- fItemArray[i].~T();
- }
+ this->forEach([](T& o) { o.~T(); });
if (fOwnMemory) {
sk_free(fMemArray);
}
@@ -109,16 +103,12 @@
*/
void reset(int n) {
SkASSERT(n >= 0);
- for (int i = 0; i < fCount; ++i) {
- fItemArray[i].~T();
- }
+ this->forEach([](T& o) { o.~T(); });
// Set fCount to 0 before calling checkRealloc so that no elements are moved.
fCount = 0;
this->checkRealloc(n);
fCount = n;
- for (int i = 0; i < fCount; ++i) {
- new (fItemArray + i) T;
- }
+ this->forEach([](T& o) { new (&o) T; });
fReserved = false;
}
@@ -126,9 +116,7 @@
* Resets to a copy of a C array and resets any reserve count.
*/
void reset(const T* array, int count) {
- for (int i = 0; i < fCount; ++i) {
- fItemArray[i].~T();
- }
+ this->forEach([](T& o) { o.~T(); });
fCount = 0;
this->checkRealloc(count);
fCount = count;
@@ -176,33 +164,23 @@
* the reference only remains valid until the next call that adds or removes
* elements.
*/
- T& push_back() {
- void* newT = this->push_back_raw(1);
- return *new (newT) T;
- }
+ T& push_back() { return *new (this->push_back_raw(1)) T; }
/**
* Version of above that uses a copy constructor to initialize the new item
*/
- T& push_back(const T& t) {
- void* newT = this->push_back_raw(1);
- return *new (newT) T(t);
- }
+ T& push_back(const T& t) { return *new (this->push_back_raw(1)) T(t); }
/**
* Version of above that uses a move constructor to initialize the new item
*/
- T& push_back(T&& t) {
- void* newT = this->push_back_raw(1);
- return *new (newT) T(std::move(t));
- }
+ T& push_back(T&& t) { return *new (this->push_back_raw(1)) T(std::move(t)); }
/**
* Construct a new T at the back of this array.
*/
template<class... Args> T& emplace_back(Args&&... args) {
- void* newT = this->push_back_raw(1);
- return *new (newT) T(std::forward<Args>(args)...);
+ return *new (this->push_back_raw(1)) T(std::forward<Args>(args)...);
}
/**
@@ -212,11 +190,9 @@
*/
T* push_back_n(int n) {
SkASSERT(n >= 0);
- void* newTs = this->push_back_raw(n);
- for (int i = 0; i < n; ++i) {
- new (static_cast<char*>(newTs) + i * sizeof(T)) T;
- }
- return static_cast<T*>(newTs);
+ T* newTs = this->push_back_raw(n);
+ ForEach(newTs, newTs + n, [](T& o) { new (&o) T; });
+ return newTs;
}
/**
@@ -225,38 +201,30 @@
*/
T* push_back_n(int n, const T& t) {
SkASSERT(n >= 0);
- void* newTs = this->push_back_raw(n);
- for (int i = 0; i < n; ++i) {
- new (static_cast<char*>(newTs) + i * sizeof(T)) T(t);
- }
- return static_cast<T*>(newTs);
+ T* newTs = this->push_back_raw(n);
+ ForEach(newTs, newTs + n, [&t](T& o) { new (&o) T(t); });
+ return newTs;
}
/**
* Version of above that uses a copy constructor to initialize the n items
* to separate T values.
*/
- T* push_back_n(int n, const T t[]) {
+ T* push_back_n(int n, const T* ts) {
SkASSERT(n >= 0);
- this->checkRealloc(n);
- for (int i = 0; i < n; ++i) {
- new (fItemArray + fCount + i) T(t[i]);
- }
- fCount += n;
- return fItemArray + fCount - n;
+ T* newTs = this->push_back_raw(n);
+ ForEach(newTs, newTs + n, [&ts](T& o) { new (&o) T(*ts++); });
+ return newTs;
}
/**
* Version of above that uses the move constructor to set n items.
*/
- T* move_back_n(int n, T* t) {
+ T* move_back_n(int n, T* ts) {
SkASSERT(n >= 0);
- this->checkRealloc(n);
- for (int i = 0; i < n; ++i) {
- new (fItemArray + fCount + i) T(std::move(t[i]));
- }
- fCount += n;
- return fItemArray + fCount - n;
+ T* newTs = this->push_back_raw(n);
+ ForEach(newTs, newTs + n, [&ts](T& o) { new (&o) T(std::move(*ts++)); });
+ return newTs;
}
/**
@@ -276,9 +244,7 @@
SkASSERT(n >= 0);
SkASSERT(fCount >= n);
fCount -= n;
- for (int i = 0; i < n; ++i) {
- fItemArray[fCount + i].~T();
- }
+ ForEach(this->end(), this->end() + n, [](T& o) { o.~T(); });
this->checkRealloc(0);
}
@@ -479,9 +445,7 @@
// MEM_MOVE == true implies that the type is trivially movable, and not necessarily
// trivially copyable (think sk_sp<>). So short of adding another template arg, we
// must be conservative and use copy construction.
- for (int i = 0; i < fCount; ++i) {
- new (fItemArray + i) T(src[i]);
- }
+ this->forEach([&src](T& o) { new (&o) T(*src++); });
}
template <bool E = MEM_MOVE> SK_WHEN(E, void) move(int dst, int src) {
@@ -496,19 +460,20 @@
fItemArray[src].~T();
}
template <bool E = MEM_MOVE> SK_WHEN(!E, void) move(void* dst) {
- for (int i = 0; i < fCount; ++i) {
- new (static_cast<char*>(dst) + sizeof(T) * i) T(std::move(fItemArray[i]));
- fItemArray[i].~T();
- }
+ T* tDst = static_cast<T*>(dst);
+ this->forEach([&tDst](T& o) {
+ new (tDst++) T(std::move(o));
+ o.~T();
+ } );
}
static constexpr int kMinHeapAllocCount = 8;
// Helper function that makes space for n objects, adjusts the count, but does not initialize
// the new objects.
- void* push_back_raw(int n) {
+ T* push_back_raw(int n) {
this->checkRealloc(n);
- void* ptr = fItemArray + fCount;
+ T* ptr = fItemArray + fCount;
fCount += n;
return ptr;
}
@@ -562,6 +527,12 @@
int fAllocCount;
bool fOwnMemory : 1;
bool fReserved : 1;
+
+ template <typename Fn>
+ static void ForEach(T* begin, T* end, Fn&& f) { while (begin != end) { f(*begin++); } }
+
+ template <typename Fn>
+ void forEach(Fn&& f) { ForEach(this->begin(), this->end(), std::move(f)); }
};
template <typename T, bool M> static inline void swap(SkTArray<T, M>& a, SkTArray<T, M>& b) {