FDO API Reference Feature Data Objects

Stack.h

Go to the documentation of this file.
00001 #ifndef _STACK_H_
00002 #define _STACK_H_
00003 // 
00004 
00005 //
00006 // Copyright (C) 2004-2006  Autodesk, Inc.
00007 // 
00008 // This library is free software; you can redistribute it and/or
00009 // modify it under the terms of version 2.1 of the GNU Lesser
00010 // General Public License as published by the Free Software Foundation.
00011 // 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // Lesser General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this library; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 //
00021 
00022 #ifdef _WIN32
00023 #pragma once
00024 #endif
00025 
00026 /// \cond DOXYGEN-IGNORE
00027 /// \brief
00028 /// FdoStack is an abstract template for a Stack ADT. This class is not yet
00029 /// part of the FDOCommon API. However, doc comments have been added since it might
00030 /// be added to the API in the future. All doc comments will need to change to
00031 /// if this happens.
00032 template <class OBJ, class EXC> class FdoStack : private FdoCollection<OBJ,EXC>
00033 {
00034 
00035 public:
00036     /// \brief
00037     /// Checks if the stack is empty
00038     /// 
00039     /// \return
00040     /// Returns true if the stack is empty
00041     /// 
00042     virtual FdoBoolean IsEmpty() const
00043     {
00044         return (FdoCollection <OBJ, EXC>::GetCount() == 0);
00045     }
00046 
00047     /// \brief
00048     /// Gets the top item on the stack without popping it
00049     /// 
00050     /// \return
00051     /// Returns the top item. Returns NULL if the stack is empty
00052     /// 
00053     virtual OBJ* Peek(void) const
00054     {
00055         if ( IsEmpty() ) 
00056             return NULL;
00057 
00058         return GetItem(FdoCollection <OBJ, EXC>::GetCount() - 1);
00059     }
00060 
00061     /// \brief
00062     /// Pushes the given item onto the stack
00063     /// 
00064     /// \param value 
00065     /// Input the item to push. This item becomes the top item.
00066     /// 
00067     /// \return
00068     /// Returns nothing
00069     /// 
00070     virtual void Push(OBJ* value)
00071     {
00072         Add(value);
00073     }
00074 
00075     /// \brief
00076     /// Removes all items from the stack
00077     /// 
00078     /// \return
00079     /// Returns nothing
00080     /// 
00081     virtual void Clear()
00082     {
00083         FdoCollection<OBJ,EXC>::Clear();
00084     }
00085 
00086     /// \brief
00087     /// Pops the top item off the stack. The previous top item becomes the top item.
00088     /// An exception is thrown if the stack is empty
00089     /// 
00090     /// \param value 
00091     /// Input value
00092     /// 
00093     /// \return
00094     /// Returns nothing
00095     /// 
00096     virtual OBJ* Pop(void)
00097     {
00098         if ( IsEmpty() )
00099             throw EXC::Create(
00100                 FdoException::NLSGetMessage(
00101                     FDO_NLSID(FDO_39_STACKPOP),
00102                     "Stack is empty, cannot pop."
00103                 )
00104             );
00105 
00106 
00107         OBJ* pItem = Peek();
00108         RemoveAt(FdoCollection <OBJ, EXC>::GetCount() - 1);
00109 
00110         return pItem;
00111     }
00112 
00113     /// \brief
00114     /// Increase the reference count.
00115     /// 
00116     /// \return
00117     /// Returns the new reference count (value for debugging use only).
00118     /// 
00119     FDO_API_COMMON FdoInt32 AddRef() { return FdoCollection<OBJ,EXC>::AddRef(); }
00120 
00121     /// \brief
00122     /// Decrease the reference count.
00123     /// 
00124     /// \return
00125     /// Returns the new reference count (value for debugging use only).
00126     /// 
00127     FDO_API_COMMON FdoInt32 Release() { return FdoCollection<OBJ,EXC>::Release(); }
00128 
00129 protected:
00130     /// \brief
00131     /// Gets an item at a given position in the stack. This
00132     /// function is protected so that FdoStack implementors can decide
00133     /// whether to expose it or restrict access to the top element only.
00134     /// 
00135     /// \param level 
00136     /// Input Get the element at this level. If 0 then
00137     /// the top element is returned. If 1 then the element just below the
00138     /// top element is returned, and so on.
00139     /// 
00140     /// \return
00141     /// Returns the requested item. Returns NULL if the stack is empty
00142     /// or level is out of range (less than 0 or greater or equal to 
00143     /// the number of elements in the stack
00144     /// 
00145     FDO_API_COMMON   virtual OBJ* Peek(int level) const
00146     {
00147         if ( level < 0 || (level >= FdoCollection <OBJ, EXC>::GetCount()) ) 
00148             return NULL;
00149 
00150         return GetItem(FdoCollection <OBJ, EXC>::GetCount() - level - 1);
00151     }
00152 
00153 private:
00154 
00155 };
00156 /// \endcond
00157 
00158 #endif
00159 
00160 

Comments or suggestions? Send us feedback.