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 
00028 /// \brief
00029 /// FdoStack is an abstract template for a Stack ADT. This class is not yet
00030 /// part of the FDOCommon API. However, doc comments have been added since it might
00031 /// be added to the API in the future. All doc comments will need to change to
00032 /// if this happens.
00033 template <class OBJ, class EXC> class FdoStack : private FdoCollection<OBJ,EXC>
00034 {
00035 
00036 public:
00037     /// \brief
00038     /// Checks if the stack is empty
00039     /// 
00040     /// \return
00041     /// Returns true if the stack is empty
00042     /// 
00043     virtual FdoBoolean IsEmpty() const
00044     {
00045         return (FdoCollection <OBJ, EXC>::GetCount() == 0);
00046     }
00047 
00048     /// \brief
00049     /// Gets the top item on the stack without popping it
00050     /// 
00051     /// \return
00052     /// Returns the top item. Returns NULL if the stack is empty
00053     /// 
00054     virtual OBJ* Peek(void) const
00055     {
00056         if ( IsEmpty() ) 
00057             return NULL;
00058 
00059         return GetItem(FdoCollection <OBJ, EXC>::GetCount() - 1);
00060     }
00061 
00062     /// \brief
00063     /// Pushes the given item onto the stack
00064     /// 
00065     /// \param value 
00066     /// Input the item to push. This item becomes the top item.
00067     /// 
00068     /// \return
00069     /// Returns nothing
00070     /// 
00071     virtual void Push(OBJ* value)
00072     {
00073         Add(value);
00074     }
00075 
00076     /// \brief
00077     /// Removes all items from the stack
00078     /// 
00079     /// \return
00080     /// Returns nothing
00081     /// 
00082     virtual void Clear()
00083     {
00084         FdoCollection<OBJ,EXC>::Clear();
00085     }
00086 
00087     /// \brief
00088     /// Pops the top item off the stack. The previous top item becomes the top item.
00089     /// An exception is thrown if the stack is empty
00090     /// 
00091     /// \param value 
00092     /// Input value
00093     /// 
00094     /// \return
00095     /// Returns nothing
00096     /// 
00097     virtual OBJ* Pop(void)
00098     {
00099         if ( IsEmpty() )
00100             throw EXC::Create(
00101                 FdoException::NLSGetMessage(
00102                     FDO_NLSID(FDO_39_STACKPOP),
00103                     "Stack is empty, cannot pop."
00104                 )
00105             );
00106 
00107 
00108         OBJ* pItem = Peek();
00109         RemoveAt(FdoCollection <OBJ, EXC>::GetCount() - 1);
00110 
00111         return pItem;
00112     }
00113 
00114     /// \brief
00115     /// Increase the reference count.
00116     /// 
00117     /// \return
00118     /// Returns the new reference count (value for debugging use only).
00119     /// 
00120     FDO_API_COMMON FdoInt32 AddRef() { return FdoCollection<OBJ,EXC>::AddRef(); }
00121 
00122     /// \brief
00123     /// Decrease the reference count.
00124     /// 
00125     /// \return
00126     /// Returns the new reference count (value for debugging use only).
00127     /// 
00128     FDO_API_COMMON FdoInt32 Release() { return FdoCollection<OBJ,EXC>::Release(); }
00129 
00130 protected:
00131     /// \brief
00132     /// Gets an item at a given position in the stack. This
00133     /// function is protected so that FdoStack implementors can decide
00134     /// whether to expose it or restrict access to the top element only.
00135     /// 
00136     /// \param level 
00137     /// Input Get the element at this level. If 0 then
00138     /// the top element is returned. If 1 then the element just below the
00139     /// top element is returned, and so on.
00140     /// 
00141     /// \return
00142     /// Returns the requested item. Returns NULL if the stack is empty
00143     /// or level is out of range (less than 0 or greater or equal to 
00144     /// the number of elements in the stack
00145     /// 
00146     FDO_API_COMMON   virtual OBJ* Peek(int level) const
00147     {
00148         if ( level < 0 || (level >= FdoCollection <OBJ, EXC>::GetCount()) ) 
00149             return NULL;
00150 
00151         return GetItem(FdoCollection <OBJ, EXC>::GetCount() - level - 1);
00152     }
00153 
00154 private:
00155 
00156 };
00157 /// \endcond
00158 
00159 #endif
00160 
00161 

Comments or suggestions? Send us feedback.