MyGUI  3.0.1
MyGUI_Enumerator.h
Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #ifndef __MYGUI_ENUMERATOR_H__
00024 #define __MYGUI_ENUMERATOR_H__
00025 
00026 #include <assert.h>
00027 
00028 namespace MyGUI
00029 {
00030 
00063     template<typename T>
00064     class Enumerator
00065     {
00066     private:
00067         Enumerator() { }
00068 
00069     public:
00070         explicit Enumerator(const T& _container) :
00071             m_first(true),
00072             m_current(_container.begin()),
00073             m_end(_container.end())
00074         {
00075         }
00076 
00077         Enumerator(typename T::const_iterator _first, typename T::const_iterator _end) :
00078             m_first(true),
00079             m_current(_first),
00080             m_end(_end)
00081         {
00082         }
00083 
00084         bool next()
00085         {
00086             if (m_current == m_end) return false;
00087             else if (m_first)
00088             {
00089                 m_first = false;
00090                 return true;
00091             }
00092             ++ m_current;
00093             if (m_current == m_end) return false;
00094             return true;
00095         }
00096 
00097         typename T::const_reference operator->() const { assert(m_current != m_end); return (*m_current); }
00098         typename T::const_reference current() { assert(m_current != m_end); return (*m_current); }
00099 
00100     private:
00101         bool m_first;
00102         typename T::const_iterator m_current;
00103         typename T::const_iterator m_end;
00104     };
00105 
00106 } // namespace MyGUI
00107 
00108 #endif // __MYGUI_ENUMERATOR_H__