Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Collection/Stack.h
Go to the documentation of this file.
00001 /*
00002     This file is part of Simple Application Framework (Saf) library.
00003     Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com>
00004 
00005     This library is free software: you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published 
00007     by the Free Software Foundation, either version 3 of the License, or
00008     (at your option) any later version.
00009 
00010     Saf is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with Simple Application Framework library. If not, 
00017     see <http://www.gnu.org/licenses/>.
00018 */
00019 
00028 #ifndef SAF_COLLECTION_STACK_H
00029 #define SAF_COLLECTION_STACK_H
00030 
00031 #include "../Type.h"
00032 #include "../Algo/Swap.h"
00033 #include "Deque.h"
00034 
00035 namespace Saf
00036 {
00037     namespace Collection
00038     {
00058         template <class ValType, class ContainerType = Deque<ValType> > 
00059         class Stack
00060         {
00061         protected:
00062             typedef Stack<ValType,ContainerType> MyType;
00063 
00064         private:
00066             ContainerType m_storage;
00071             Size m_elems;
00072 
00073         public:
00075             Stack()
00076                 : m_elems(0)
00077             {}
00078 
00080             Stack(const MyType& stack)
00081                 : m_storage(stack.m_storage), m_elems(stack.m_elems)
00082             {}
00083 
00085             explicit Stack(const ContainerType& cont)
00086                 : m_storage(cont)
00087             {
00088                 m_elems = Algo::Range::Distance(cont.Begin(), cont.End());
00089             }
00090 
00092             MyType& operator=(MyType& stack)
00093             {
00094                 m_storage = stack.m_storage;
00095                 m_elems = stack.m_elems;
00096                 return *this;
00097             }
00098 
00100             MyType& Swap(MyType& stack)
00101             {
00102                 Algo::Swap(m_storage, stack.m_storage);
00103                 Algo::Swap(m_elems, stack.m_elems);
00104                 return *this;
00105             }
00106 
00108             bool IsEmpty() const
00109             {
00110                 return m_storage.IsEmpty();
00111             }
00112 
00114             Size Elements() const
00115             {
00116                 return m_elems;
00117             }
00118 
00120             ValType& Top()
00121             {
00122                 return *m_storage.Back();
00123             }
00124 
00126             const ValType& Top() const
00127             {
00128                 return *m_storage.Back();
00129             }
00130 
00132             MyType& Push(const ValType& val)
00133             {
00134                 m_storage.PushBack(val);
00135                 ++m_elems;
00136                 return *this;
00137             }
00138 
00140             MyType& Pop()
00141             {
00142                 --m_elems;
00143                 m_storage.PopBack();
00144                 return *this;
00145             }
00146 
00148             const ContainerType& Container() const
00149             {
00150                 return m_storage;
00151             }
00152         };
00153     }
00154 
00156     namespace Algo
00157     {
00158         // Swap specialization for a stack container.
00159         template <class ValType, class ContainerType> 
00160         struct SwapFunc< Collection::Stack<ValType,ContainerType> >
00161         {
00162             void operator()(Collection::Stack<ValType,ContainerType>& x, 
00163                 Collection::Stack<ValType,ContainerType>& y)
00164             {
00165                 x.Swap(y);
00166             }
00167         };
00168     }
00170 }
00171 
00172 #endif