michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsISupportsUtils.h" michael@0: #include "nsTransactionItem.h" michael@0: #include "nsTransactionStack.h" michael@0: #include "nscore.h" michael@0: michael@0: nsTransactionStack::nsTransactionStack(nsTransactionStack::Type aType) michael@0: : mType(aType) michael@0: { michael@0: } michael@0: michael@0: nsTransactionStack::~nsTransactionStack() michael@0: { michael@0: Clear(); michael@0: } michael@0: michael@0: void michael@0: nsTransactionStack::Push(nsTransactionItem *aTransaction) michael@0: { michael@0: if (!aTransaction) { michael@0: return; michael@0: } michael@0: michael@0: // The stack's bottom is the front of the deque, and the top is the back. michael@0: mDeque.push_back(aTransaction); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransactionStack::Pop() michael@0: { michael@0: if (mDeque.empty()) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr ret = mDeque.back().forget(); michael@0: mDeque.pop_back(); michael@0: return ret.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransactionStack::PopBottom() michael@0: { michael@0: if (mDeque.empty()) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr ret = mDeque.front().forget(); michael@0: mDeque.pop_front(); michael@0: return ret.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransactionStack::Peek() michael@0: { michael@0: if (mDeque.empty()) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr ret = mDeque.back(); michael@0: return ret.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsTransactionStack::GetItem(int32_t aIndex) michael@0: { michael@0: if (aIndex < 0 || aIndex >= static_cast(mDeque.size())) { michael@0: return nullptr; michael@0: } michael@0: nsRefPtr ret = mDeque[aIndex]; michael@0: return ret.forget(); michael@0: } michael@0: michael@0: void michael@0: nsTransactionStack::Clear() michael@0: { michael@0: while (!mDeque.empty()) { michael@0: nsRefPtr tx = mType == FOR_UNDO ? Pop() : PopBottom(); michael@0: }; michael@0: } michael@0: michael@0: void michael@0: nsTransactionStack::DoTraverse(nsCycleCollectionTraversalCallback &cb) michael@0: { michael@0: int32_t size = mDeque.size(); michael@0: for (int32_t i = 0; i < size; ++i) { michael@0: nsTransactionItem* item = mDeque[i]; michael@0: if (item) { michael@0: NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "transaction stack mDeque[i]"); michael@0: cb.NoteNativeChild(item, NS_CYCLE_COLLECTION_PARTICIPANT(nsTransactionItem)); michael@0: } michael@0: } michael@0: }