MyGUI  3.0.1
MyGUI_Enumerator.h
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #ifndef __MYGUI_ENUMERATOR_H__
24 #define __MYGUI_ENUMERATOR_H__
25 
26 #include <assert.h>
27 
28 namespace MyGUI
29 {
30 
63  template<typename T>
64  class Enumerator
65  {
66  private:
67  Enumerator() { }
68 
69  public:
70  explicit Enumerator(const T& _container) :
71  m_first(true),
72  m_current(_container.begin()),
73  m_end(_container.end())
74  {
75  }
76 
77  Enumerator(typename T::const_iterator _first, typename T::const_iterator _end) :
78  m_first(true),
79  m_current(_first),
80  m_end(_end)
81  {
82  }
83 
84  bool next()
85  {
86  if (m_current == m_end) return false;
87  else if (m_first)
88  {
89  m_first = false;
90  return true;
91  }
92  ++ m_current;
93  if (m_current == m_end) return false;
94  return true;
95  }
96 
97  typename T::const_reference operator->() const { assert(m_current != m_end); return (*m_current); }
98  typename T::const_reference current() { assert(m_current != m_end); return (*m_current); }
99 
100  private:
101  bool m_first;
102  typename T::const_iterator m_current;
103  typename T::const_iterator m_end;
104  };
105 
106 } // namespace MyGUI
107 
108 #endif // __MYGUI_ENUMERATOR_H__