Back to index

lightning-sunbird  0.9+nobinonly
nsTransactionStack.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsITransaction.h"
00039 #include "nsTransactionItem.h"
00040 #include "nsTransactionStack.h"
00041 #include "nsCOMPtr.h"
00042 
00043 nsTransactionStack::nsTransactionStack()
00044   : mQue(0)
00045 {
00046   nsTransactionReleaseFunctor* theFunctor=new nsTransactionReleaseFunctor();
00047   mQue.SetDeallocator(theFunctor);
00048 } 
00049 
00050 nsTransactionStack::~nsTransactionStack()
00051 {
00052   Clear();
00053 }
00054 
00055 nsresult
00056 nsTransactionStack::Push(nsTransactionItem *aTransaction)
00057 {
00058   if (!aTransaction)
00059     return NS_ERROR_NULL_POINTER;
00060 
00061   /* nsDeque's Push() method adds new items at the back
00062    * of the deque.
00063    */
00064   mQue.Push(aTransaction);
00065 
00066   return NS_OK;
00067 }
00068 
00069 nsresult
00070 nsTransactionStack::Pop(nsTransactionItem **aTransaction)
00071 {
00072   if (!aTransaction)
00073     return NS_ERROR_NULL_POINTER;
00074 
00075   /* nsDeque is a FIFO, so the top of our stack is actually
00076    * the back of the deque.
00077    */
00078   *aTransaction = (nsTransactionItem *)mQue.Pop();
00079 
00080   return NS_OK;
00081 }
00082 
00083 nsresult
00084 nsTransactionStack::PopBottom(nsTransactionItem **aTransaction)
00085 {
00086   if (!aTransaction)
00087     return NS_ERROR_NULL_POINTER;
00088 
00089   /* nsDeque is a FIFO, so the bottom of our stack is actually
00090    * the front of the deque.
00091    */
00092   *aTransaction = (nsTransactionItem *)mQue.PopFront();
00093 
00094   return NS_OK;
00095 }
00096 
00097 nsresult
00098 nsTransactionStack::Peek(nsTransactionItem **aTransaction)
00099 {
00100   if (!aTransaction)
00101     return NS_ERROR_NULL_POINTER;
00102 
00103   if (!mQue.GetSize()) {
00104     *aTransaction = 0;
00105     return NS_OK;
00106   }
00107 
00108   *aTransaction = (nsTransactionItem *)(mQue.Last());
00109 
00110   return NS_OK;
00111 }
00112 
00113 nsresult
00114 nsTransactionStack::GetItem(PRInt32 aIndex, nsTransactionItem **aTransaction)
00115 {
00116   if (!aTransaction)
00117     return NS_ERROR_NULL_POINTER;
00118 
00119   if (aIndex < 0 || aIndex >= mQue.GetSize())
00120     return NS_ERROR_FAILURE;
00121 
00122   *aTransaction = (nsTransactionItem *)(mQue.ObjectAt(aIndex));
00123 
00124   return NS_OK;
00125 }
00126 
00127 nsresult
00128 nsTransactionStack::Clear(void)
00129 {
00130   nsTransactionItem *tx = 0;
00131   nsresult result    = NS_OK;
00132 
00133   /* Pop all transactions off the stack and release them. */
00134 
00135   result = Pop(&tx);
00136 
00137   if (NS_FAILED(result))
00138     return result;
00139 
00140   while (tx) {
00141     delete tx;
00142 
00143     result = Pop(&tx);
00144 
00145     if (NS_FAILED(result))
00146       return result;
00147   }
00148 
00149   return NS_OK;
00150 }
00151 
00152 nsresult
00153 nsTransactionStack::GetSize(PRInt32 *aStackSize)
00154 {
00155   if (!aStackSize)
00156     return NS_ERROR_NULL_POINTER;
00157 
00158   *aStackSize = mQue.GetSize();
00159 
00160   return NS_OK;
00161 }
00162 
00163 nsTransactionRedoStack::~nsTransactionRedoStack()
00164 {
00165   Clear();
00166 }
00167 
00168 nsresult
00169 nsTransactionRedoStack::Clear(void)
00170 {
00171   nsTransactionItem *tx = 0;
00172   nsresult result       = NS_OK;
00173 
00174   /* When clearing a Redo stack, we have to clear from the
00175    * bottom of the stack towards the top!
00176    */
00177 
00178   result = PopBottom(&tx);
00179 
00180   if (NS_FAILED(result))
00181     return result;
00182 
00183   while (tx) {
00184     delete tx;
00185 
00186     result = PopBottom(&tx);
00187 
00188     if (NS_FAILED(result))
00189       return result;
00190   }
00191 
00192   return NS_OK;
00193 }
00194 
00195 void *
00196 nsTransactionReleaseFunctor::operator()(void *aObject)
00197 {
00198   nsTransactionItem *item = (nsTransactionItem *)aObject;
00199   delete item;
00200   return 0;
00201 }