Skip to content

Commit

Permalink
Bug 1253424 - part 2 - add nsTransactionStack::IsEmpty; r=erahm
Browse files Browse the repository at this point in the history
Checking to see whether the stack is empty is a reasonably common
operation.  We can make the code clearer and more efficient (no need to
refcount to check the emptiness of the stack) in several cases.
  • Loading branch information
froydnj committed Mar 3, 2016
1 parent d422cbb commit e3a01c4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
28 changes: 9 additions & 19 deletions editor/txmgr/nsTransactionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,13 @@ nsTransactionManager::UndoTransaction()
// executing a transaction's DoTransaction() method! If this happens,
// the UndoTransaction() request is ignored, and we return NS_ERROR_FAILURE.

RefPtr<nsTransactionItem> tx = mDoStack.Peek();

if (tx) {
if (!mDoStack.IsEmpty()) {
return NS_ERROR_FAILURE;
}

// Peek at the top of the undo stack. Don't remove the transaction
// until it has successfully completed.
tx = mUndoStack.Peek();
RefPtr<nsTransactionItem> tx = mUndoStack.Peek();

// Bail if there's nothing on the stack.
if (!tx) {
Expand Down Expand Up @@ -155,15 +153,13 @@ nsTransactionManager::RedoTransaction()
// executing a transaction's DoTransaction() method! If this happens,
// the RedoTransaction() request is ignored, and we return NS_ERROR_FAILURE.

RefPtr<nsTransactionItem> tx = mDoStack.Peek();

if (tx) {
if (!mDoStack.IsEmpty()) {
return NS_ERROR_FAILURE;
}

// Peek at the top of the redo stack. Don't remove the transaction
// until it has successfully completed.
tx = mRedoStack.Peek();
RefPtr<nsTransactionItem> tx = mRedoStack.Peek();

// Bail if there's nothing on the stack.
if (!tx) {
Expand Down Expand Up @@ -331,9 +327,7 @@ nsTransactionManager::SetMaxTransactionCount(int32_t aMaxCount)
// SetMaxTransactionCount() request is ignored, and we return
// NS_ERROR_FAILURE.

RefPtr<nsTransactionItem> tx = mDoStack.Peek();

if (tx) {
if (!mDoStack.IsEmpty()) {
return NS_ERROR_FAILURE;
}

Expand Down Expand Up @@ -364,7 +358,7 @@ nsTransactionManager::SetMaxTransactionCount(int32_t aMaxCount)
// the bottom of the stack and pop towards the top.

while (numUndoItems > 0 && (numRedoItems + numUndoItems) > aMaxCount) {
tx = mUndoStack.PopBottom();
RefPtr<nsTransactionItem> tx = mUndoStack.PopBottom();

if (!tx) {
return NS_ERROR_FAILURE;
Expand All @@ -377,7 +371,7 @@ nsTransactionManager::SetMaxTransactionCount(int32_t aMaxCount)
// the bottom of the stack and pop towards the top.

while (numRedoItems > 0 && (numRedoItems + numUndoItems) > aMaxCount) {
tx = mRedoStack.PopBottom();
RefPtr<nsTransactionItem> tx = mRedoStack.PopBottom();

if (!tx) {
return NS_ERROR_FAILURE;
Expand Down Expand Up @@ -487,15 +481,11 @@ nsTransactionManager::BatchTopUndo()
nsresult
nsTransactionManager::RemoveTopUndo()
{
RefPtr<nsTransactionItem> lastUndo;

lastUndo = mUndoStack.Peek();
if (!lastUndo) {
if (mUndoStack.IsEmpty()) {
return NS_OK;
}

lastUndo = mUndoStack.Pop();

RefPtr<nsTransactionItem> lastUndo = mUndoStack.Pop();
return NS_OK;
}

Expand Down
1 change: 1 addition & 0 deletions editor/txmgr/nsTransactionStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class nsTransactionStack
already_AddRefed<nsTransactionItem> GetItem(int32_t aIndex);
void Clear();
int32_t GetSize() { return mDeque.size(); }
bool IsEmpty() const { return mDeque.empty(); }

void DoUnlink() { Clear(); }
void DoTraverse(nsCycleCollectionTraversalCallback &cb);
Expand Down

0 comments on commit e3a01c4

Please sign in to comment.