Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Collection/Queue.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_QUEUE_H
00029 #define SAF_COLLECTION_QUEUE_H
00030 
00031 #include "../Type.h"
00032 #include "../Algo/Swap.h"
00033 #include "Deque.h"
00034 
00035 namespace Saf
00036 {
00037     namespace Collection
00038     {
00060         template <class ValType, class ContainerType = Deque<ValType> > 
00061         class Queue
00062         {
00063         protected:
00064             typedef Queue<ValType,ContainerType> MyType;
00065 
00066         private:
00068             ContainerType m_storage;
00073             Size m_elems;
00074 
00075         public:
00077             Queue()
00078                 : m_elems(0)
00079             {}
00080 
00082             Queue(const MyType& queue)
00083                 : m_storage(queue.m_storage), m_elems(queue.m_elems)
00084             {}
00085 
00087             explicit Queue(const ContainerType& cont)
00088                 : m_storage(cont)
00089             {
00090                 m_elems = Algo::Range::Distance(cont.Begin(), cont.End());
00091             }
00092 
00094             MyType& operator=(MyType& queue)
00095             {
00096                 m_storage = queue.m_storage;
00097                 m_elems = queue.m_elems;
00098                 return *this;
00099             }
00100 
00102             MyType& Swap(MyType& queue)
00103             {
00104                 Algo::Swap(m_storage, queue.m_storage);
00105                 Algo::Swap(m_elems, queue.m_elems);
00106                 return *this;
00107             }
00108 
00110             bool IsEmpty() const
00111             {
00112                 return m_storage.IsEmpty();
00113             }
00114 
00116             Size Elements() const
00117             {
00118                 return m_elems;
00119             }
00120 
00122             ValType& Front()
00123             {
00124                 return *m_storage.Front();
00125             }
00126 
00128             const ValType& Front() const
00129             {
00130                 return *m_storage.Front();
00131             }
00132 
00134             ValType& Back()
00135             {
00136                 return *m_storage.Back();
00137             }
00138 
00140             const ValType& Back() const
00141             {
00142                 return *m_storage.Back();
00143             }
00144 
00146             MyType& Push(const ValType& val)
00147             {
00148                 m_storage.PushBack(val);
00149                 ++m_elems;
00150                 return *this;
00151             }
00152 
00154             MyType& Pop()
00155             {
00156                 --m_elems;
00157                 m_storage.PopFront();
00158                 return *this;
00159             }
00160 
00162             const ContainerType& Container() const
00163             {
00164                 return m_storage;
00165             }
00166         };
00167     }
00168 
00170     namespace Algo
00171     {
00172         // Swap specialization for a queue container.
00173         template <class ValType, class ContainerType>
00174         struct SwapFunc< Collection::Queue<ValType,ContainerType> >
00175         {
00176             void operator()(Collection::Queue<ValType,ContainerType>& x, 
00177                 Collection::Queue<ValType,ContainerType>& y)
00178             {
00179                 x.Swap(y);
00180             }
00181         };
00182     }
00184 }
00185 
00186 #endif