stack.cpp

Go to the documentation of this file.
00001 // $Id: stack.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 /*
00099 //*/
00100 
00101 // Implementation of the Stack and MarkedStack classes 
00102 
00103 #include "camtypes.h" 
00104 #include "stack.h"
00105 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 //#include "ensure.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "listitem.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00108 
00109 DECLARE_SOURCE("$Revision: 1282 $");
00110 
00111 // Declare smart memory handling in Debug builds
00112 #define new CAM_DEBUG_NEW
00113 
00114 /********************************************************************************************
00115 
00116 >   Stack::Stack()
00117 
00118     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00119     Created:    8/2/94
00120     Inputs:     -
00121     Outputs:    -
00122     Returns:    -
00123     Purpose:    Stack constructor
00124     Errors:     -
00125     SeeAlso:    -
00126 
00127 ********************************************************************************************/
00128 
00129 
00130 Stack::Stack()
00131 {
00132 }
00133 
00134 /********************************************************************************************
00135 
00136 >   Stack::~Stack()
00137 
00138     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00139     Created:    8/2/94
00140     Inputs:     -
00141     Outputs:    -
00142     Returns:    -
00143     Purpose:    Stack destructor. The items on the stack are not deleted
00144     Errors:     -
00145     SeeAlso:    -
00146 
00147 ********************************************************************************************/
00148 
00149 Stack::~Stack()
00150 {
00151     
00152 }
00153 
00154 /********************************************************************************************
00155 
00156 >   void Stack::Push(ListItem* Item)
00157 
00158     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00159     Created:    8/2/94
00160     Inputs:     -
00161     Outputs:    -
00162     Returns:    -
00163     Purpose:    Pushes Item onto the stack 
00164     Errors:     -
00165     SeeAlso:    -
00166 
00167 ********************************************************************************************/
00168 
00169 void Stack::Push(ListItem* Item)
00170 {
00171     stack.AddTail(Item); // Add the item to the top of the stack 
00172 }
00173 
00174 /********************************************************************************************
00175 
00176 >   ListItem* Stack::Pop(void)
00177 
00178     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00179     Created:    8/2/94
00180     Inputs:     -
00181     Outputs:    -
00182     Returns:    The top item on the stack, or NULL if the stack is empty 
00183     Purpose:    Removes the item from the top of the stack, and returns it 
00184     Errors:     -
00185     SeeAlso:    -
00186 
00187 ********************************************************************************************/
00188 
00189 ListItem* Stack::Pop(void)
00190 {   
00191     return (stack.RemoveTail()); 
00192 }
00193 
00194 /********************************************************************************************
00195 
00196 >   ListItem* Stack::GetTop(void)
00197 
00198     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00199     Created:    14/2/94
00200     Inputs:     -
00201     Outputs:    -
00202     Returns:    The item at the top of the stack
00203     Purpose:    Allows you to find out what item is on the top of the stack (the item is not
00204                 removed). 
00205     Errors:     -
00206     SeeAlso:    -
00207 
00208 ********************************************************************************************/
00209 
00210 ListItem* Stack::GetTop(void)
00211 {
00212     return (stack.GetTail()); 
00213 }
00214 
00215 
00216 /********************************************************************************************
00217 
00218 >   void Stack::DeleteAll()
00219 
00220     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00221     Created:    8/2/94
00222     Inputs:     -
00223     Outputs:    -
00224     Returns:    -
00225     Purpose:    Removes and deletes all items on the stack 
00226     Errors:     -
00227     SeeAlso:    -
00228 
00229 ********************************************************************************************/
00230 
00231 void Stack::DeleteAll()
00232 {
00233     stack.DeleteAll(); // Call the list's DeleteAll method 
00234 }
00235 
00236 /********************************************************************************************
00237 
00238 >   INT32 Stack::Size()
00239 
00240     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00241     Created:    8/2/94
00242     Inputs:     -
00243     Outputs:    -
00244     Returns:    The number of items on the stack 
00245     Purpose:    For finding the size of the stack 
00246     Errors:     -
00247     SeeAlso:    -
00248 
00249 ********************************************************************************************/
00250 
00251 INT32 Stack::Size()
00252 {
00253     return stack.GetCount(); 
00254 }
00255 
00256 // ------------------------------------------------------------------------------------------
00257 // MarkedStack methods 
00258 
00259 /********************************************************************************************
00260 
00261 >   MarkedStack::MarkedStack()
00262 
00263     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00264     Created:    8/2/94
00265     Inputs:     -
00266     Outputs:    -
00267     Returns:    -
00268     Purpose:    MarkedStack constructor 
00269     Errors:     -
00270     SeeAlso:    -
00271 
00272 ********************************************************************************************/
00273 
00274 MarkedStack::MarkedStack()
00275 {
00276 }
00277 
00278 /********************************************************************************************
00279 
00280 >   MarkedStack::~MarkedStack()
00281 
00282     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00283     Created:    8/2/94
00284     Inputs:     -
00285     Outputs:    -
00286     Returns:    -
00287     Purpose:    MarkedStack destructor. The items on the stack are not deleted
00288     Errors:     -
00289     SeeAlso:    -
00290 
00291 ********************************************************************************************/
00292 
00293 MarkedStack::~MarkedStack()
00294 {
00295 }
00296 
00297 /********************************************************************************************
00298 
00299 >   MarkedStack::Push(ListItem* Item)
00300 
00301     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00302     Created:    8/2/94
00303     Inputs:     -
00304     Outputs:    -
00305     Returns:    -
00306     Purpose:    Pushes Item onto the stack 
00307     Errors:     -
00308     SeeAlso:    -
00309 
00310 ********************************************************************************************/
00311 
00312 
00313 void MarkedStack::Push(ListItem* Item)
00314 {
00315     AddTail(Item); // Add the item to the top of the stack 
00316 }
00317 
00318 /********************************************************************************************
00319 
00320 >   ListItem* MarkedStack::Pop(void)
00321 
00322     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00323     Created:    8/2/94
00324     Inputs:     -
00325     Outputs:    -
00326     Returns:    The top item on the stack, or NULL if the stack is empty 
00327     Purpose:    Removes the item from the top of the stack, and returns it 
00328     Errors:     -
00329     SeeAlso:    -
00330 
00331 ********************************************************************************************/
00332 
00333 ListItem* MarkedStack::Pop(void)
00334 {   
00335     return (RemoveTail()); 
00336 }
00337 
00338 
00339 /********************************************************************************************
00340 
00341 >   BOOL MarkedStack::Mark(void)
00342 
00343     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00344     Created:    8/2/94
00345     Inputs:     -
00346     Outputs:    -
00347     Returns:    TRUE if we could create Mark, FALSE if we are out of memory 
00348 
00349     Purpose:    Marks the current Top of the stack so that we can easily return to the 
00350                 same state by calling Release(). 
00351 
00352     Errors:     ERRORIF is called if we are out of memory 
00353     SeeAlso:    MarkedStack::Release()
00354 
00355 ********************************************************************************************/
00356 
00357 BOOL MarkedStack::Mark(void)
00358 {           
00359     // Firstly create a mark                                                    
00360     ListItemPtrItem* pMark = new ListItemPtrItem(); 
00361     // Return with an error if we could not create the mark
00362     if (pMark == NULL)
00363         return FALSE; 
00364     pMark->pListItem = GetTail();   // This will be NULL if the list is empty 
00365 
00366     // Push the mark onto the MarkStack
00367     MarkStack.Push(pMark); 
00368     return TRUE; // Success
00369 }   
00370 
00371 /********************************************************************************************
00372 
00373 >   void MarkedStack::Release(void)
00374 
00375     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00376     Created:    8/2/94
00377     Inputs:     -
00378     Outputs:    -
00379     Returns:    -
00380     Purpose:    Pops and then deletes items from the stack until we find the first mark. 
00381                 If there are no marks then all items on the stack are removed and deleted
00382     Errors:     -
00383     SeeAlso:    MarkedStack::Mark
00384 
00385 ********************************************************************************************/
00386 
00387          
00388 void MarkedStack::Release(void)
00389 {
00390     // Find the mark to stop at 
00391     ListItemPtrItem* MarkRec = (ListItemPtrItem*)MarkStack.Pop();
00392     ListItem* Mark; 
00393     if (MarkRec == NULL)                        // There are no marks to find 
00394     {
00395         Mark = NULL; 
00396     } else 
00397     {
00398         Mark = MarkRec->pListItem; 
00399     }  
00400     ListItem* TopOfStack = GetTail(); 
00401     ListItem* AsGoodAsDead; 
00402 
00403     while (TopOfStack != Mark)          // Loop until we find Mark
00404     {
00405         ENSURE(TopOfStack != NULL, "A Mark could not be found in the Marked stack"); 
00406         TopOfStack = GetPrev(TopOfStack);
00407         AsGoodAsDead = RemoveTail();
00408         delete AsGoodAsDead; 
00409     }
00410     if (MarkRec != NULL)
00411     {
00412         delete (MarkRec); // Restored State to that pointed to by Mark, so delete it 
00413     }
00414 } 
00415 
00416 /********************************************************************************************
00417 
00418 >   void MarkedStack::DeleteAll()   
00419 
00420     Author:     Simon_Maneggio (Xara Group Ltd) <camelotdev@xara.com>
00421     Created:    8/2/94
00422     Inputs:     -
00423     Outputs:    -
00424     Returns:    -
00425     Purpose:    Deletes all marks and elements in the MarkedStack
00426     Errors:     -
00427     SeeAlso:    -
00428 
00429 ********************************************************************************************/
00430 
00431 void MarkedStack::DeleteAll()
00432 {
00433     ListItem* TopOfStack;
00434     TopOfStack = GetTail(); 
00435     while (TopOfStack != NULL)   // loop while the stack is not empty 
00436     {
00437         Release(); // Deletes mark and all items before the mark        
00438         TopOfStack = GetTail(); 
00439     }   
00440     // Make sure there are no marks left
00441     ENSURE(MarkStack.Pop() == NULL, "All marks have not been deleted from the MarkedStack"); 
00442 }   
00443 

Generated on Sat Nov 10 03:47:04 2007 for Camelot by  doxygen 1.4.4