Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#include "mozilla/TransactionStack.h"
7
8
#include "nsCOMPtr.h"
9
#include "nsCycleCollectionParticipant.h"
10
#include "nsISupportsUtils.h"
11
#include "nscore.h"
12
#include "TransactionItem.h"
13
14
namespace mozilla {
15
16
class TransactionStackDeallocator final : public nsDequeFunctor {
17
virtual void operator()(void* aObject) override {
18
RefPtr<TransactionItem> releaseMe =
19
dont_AddRef(static_cast<TransactionItem*>(aObject));
20
}
21
};
22
23
TransactionStack::TransactionStack(Type aType)
24
: nsDeque(new TransactionStackDeallocator()), mType(aType) {}
25
26
TransactionStack::~TransactionStack() { Clear(); }
27
28
void TransactionStack::Push(TransactionItem* aTransactionItem) {
29
if (!aTransactionItem) {
30
return;
31
}
32
33
RefPtr<TransactionItem> item(aTransactionItem);
34
Push(item.forget());
35
}
36
37
void TransactionStack::Push(
38
already_AddRefed<TransactionItem> aTransactionItem) {
39
RefPtr<TransactionItem> item(aTransactionItem);
40
if (!item) {
41
return;
42
}
43
44
nsDeque::Push(item.forget().take());
45
}
46
47
already_AddRefed<TransactionItem> TransactionStack::Pop() {
48
RefPtr<TransactionItem> item =
49
dont_AddRef(static_cast<TransactionItem*>(nsDeque::Pop()));
50
return item.forget();
51
}
52
53
already_AddRefed<TransactionItem> TransactionStack::PopBottom() {
54
RefPtr<TransactionItem> item =
55
dont_AddRef(static_cast<TransactionItem*>(nsDeque::PopFront()));
56
return item.forget();
57
}
58
59
already_AddRefed<TransactionItem> TransactionStack::Peek() {
60
RefPtr<TransactionItem> item = static_cast<TransactionItem*>(nsDeque::Peek());
61
return item.forget();
62
}
63
64
already_AddRefed<TransactionItem> TransactionStack::GetItem(int32_t aIndex) {
65
if (aIndex < 0 || aIndex >= static_cast<int32_t>(nsDeque::GetSize())) {
66
return nullptr;
67
}
68
RefPtr<TransactionItem> item =
69
static_cast<TransactionItem*>(nsDeque::ObjectAt(aIndex));
70
return item.forget();
71
}
72
73
void TransactionStack::Clear() {
74
while (GetSize() != 0) {
75
RefPtr<TransactionItem> item = mType == FOR_UNDO ? Pop() : PopBottom();
76
}
77
}
78
79
void TransactionStack::DoTraverse(nsCycleCollectionTraversalCallback& cb) {
80
size_t size = GetSize();
81
for (size_t i = 0; i < size; ++i) {
82
TransactionItem* item = static_cast<TransactionItem*>(nsDeque::ObjectAt(i));
83
if (item) {
84
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "transaction stack mDeque[i]");
85
cb.NoteNativeChild(item,
86
NS_CYCLE_COLLECTION_PARTICIPANT(TransactionItem));
87
}
88
}
89
}
90
91
} // namespace mozilla