NptStack.h

00001 /*****************************************************************
00002 |
00003 |   Neptune - Stack
00004 |
00005 |   (c) 2006 Gilles Boccon-Gibod
00006 |   Author: Gilles Boccon-Gibod (bok@bok.net)
00007 |
00008 ****************************************************************/
00009 
00010 #ifndef _NPT_STACK_H_
00011 #define _NPT_STACK_H_
00012 
00013 /*----------------------------------------------------------------------
00014 |   includes
00015 +---------------------------------------------------------------------*/
00016 #include "NptResults.h"
00017 #include "NptTypes.h"
00018 #include "NptList.h"
00019 
00020 /*----------------------------------------------------------------------
00021 |   NPT_Stack
00022 +---------------------------------------------------------------------*/
00023 template <typename T> 
00024 class NPT_Stack : public NPT_List<T>
00025 {
00026 public:
00027     // methods
00028     NPT_Result Push(const T& value) {
00029         return Add(value);
00030     }
00031 
00032     NPT_Result Peek(T& value) {
00033         // NOTE: we must use the this-> accessor here because the standard
00034         // requires it when the member to look up is in a parent template
00035         if (this->m_ItemCount == 0) return NPT_ERROR_NO_SUCH_ITEM;
00036         value = this->m_Tail->m_Data;
00037         return NPT_SUCCESS;
00038     }
00039 
00040     NPT_Result Pop(T& value) {
00041         // NOTE: we must use the this-> accessor here because the standard
00042         // requires it when the member to look up is in a parent template
00043         if (this->m_ItemCount == 0) return NPT_ERROR_NO_SUCH_ITEM;
00044         typename NPT_List<T>::Iterator tail = this->GetLastItem();
00045         value = *tail;
00046         return this->Erase(tail);
00047     }
00048 };
00049 
00050 #endif // _NPT_STACK_H_