aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Hal Canary <halcanary@google.com>2018-05-04 14:57:47 -0400
committerGravatar Skia Commit-Bot <skia-commit-bot@chromium.org>2018-05-04 21:09:03 +0000
commit694ebb348a4487840dcd5a84eb587a7706d50775 (patch)
tree30371e066b147396788c7f187af1b151f33902cd /src
parentfa7e9a813e44f283b14f686eeb857d0d00735811 (diff)
SkSinglyLinkedList: use unique_ptr to manage ownership
Change-Id: I7b0fe6bd601b9ae0e9042ff28aa5b4dec9a9639b Reviewed-on: https://skia-review.googlesource.com/126180 Reviewed-by: Ben Wagner <bungeman@google.com> Commit-Queue: Hal Canary <halcanary@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/core/SkSinglyLinkedList.h56
1 files changed, 24 insertions, 32 deletions
diff --git a/src/core/SkSinglyLinkedList.h b/src/core/SkSinglyLinkedList.h
index b011204613..981ce76f68 100644
--- a/src/core/SkSinglyLinkedList.h
+++ b/src/core/SkSinglyLinkedList.h
@@ -9,24 +9,23 @@
#include <utility>
+#include "SkMakeUnique.h"
#include "SkTypes.h"
template <typename T> class SkSinglyLinkedList {
struct Node;
public:
- SkSinglyLinkedList() : fHead(nullptr), fTail(nullptr) {}
+ SkSinglyLinkedList() {}
~SkSinglyLinkedList() { this->reset(); }
void reset() {
SkASSERT(fHead != nullptr || nullptr == fTail);
// Use a while loop rather than recursion to avoid stack overflow.
- Node* node = fHead;
+ std::unique_ptr<Node> node = std::move(fHead);
while (node) {
- Node* next = node->fNext;
- SkASSERT(next != nullptr || node == fTail);
- delete node;
- node = next;
+ std::unique_ptr<Node> next = std::move(node->fNext);
+ SkASSERT(next || node.get() == fTail);
+ node = std::move(next);
}
- fHead = nullptr;
fTail = nullptr;
}
T* back() { return fTail ? &fTail->fData : nullptr; }
@@ -35,62 +34,55 @@ public:
#ifdef SK_DEBUG
int count() { // O(n), debug only.
int count = 0;
- for (Node* node = fHead; node; node = node->fNext) {
+ for (Node* node = fHead.get(); node; node = node->fNext.get()) {
++count;
}
return count;
}
#endif
void pop_front() {
- if (Node* node = fHead) {
- fHead = node->fNext;
- delete node;
- if (fHead == nullptr) {
+ if (fHead) {
+ fHead = std::move(fHead->fNext);
+ if (!fHead) {
fTail = nullptr;
}
}
}
template <class... Args> T* emplace_front(Args&&... args) {
- Node* n = new Node(std::forward<Args>(args)...);
- n->fNext = fHead;
+ fHead = skstd::make_unique<Node>(std::move(fHead), std::forward<Args>(args)...);
if (!fTail) {
- fTail = n;
- SkASSERT(!fHead);
+ fTail = fHead.get();
}
- fHead = n;
- return &n->fData;
+ return &fHead->fData;
}
template <class... Args> T* emplace_back(Args&&... args) {
- Node* n = new Node(std::forward<Args>(args)...);
- if (fTail) {
- fTail->fNext = n;
- } else {
- fHead = n;
- }
- fTail = n;
- return &n->fData;
+ std::unique_ptr<Node>* dst = fTail ? &fTail->fNext : &fHead;
+ *dst = skstd::make_unique<Node>(nullptr, std::forward<Args>(args)...);
+ fTail = dst->get();
+ return &fTail->fData;
}
class ConstIter {
public:
- void operator++() { fNode = fNode->fNext; }
+ void operator++() { fNode = fNode->fNext.get(); }
const T& operator*() const { return fNode->fData; }
bool operator!=(const ConstIter& rhs) const { return fNode != rhs.fNode; }
ConstIter(const Node* n) : fNode(n) {}
private:
const Node* fNode;
};
- ConstIter begin() const { return ConstIter(fHead); }
+ ConstIter begin() const { return ConstIter(fHead.get()); }
ConstIter end() const { return ConstIter(nullptr); }
private:
struct Node {
T fData;
- Node* fNext;
+ std::unique_ptr<Node> fNext;
template <class... Args>
- Node(Args&&... args) : fData(std::forward<Args>(args)...), fNext(nullptr) {}
+ Node(std::unique_ptr<Node> n, Args&&... args)
+ : fData(std::forward<Args>(args)...), fNext(std::move(n)) {}
};
- Node* fHead;
- Node* fTail;
+ std::unique_ptr<Node> fHead;
+ Node* fTail = nullptr;
SkSinglyLinkedList(const SkSinglyLinkedList<T>&) = delete;
SkSinglyLinkedList& operator=(const SkSinglyLinkedList<T>&) = delete;
};