MyGUI  3.2.1
MyGUI_Enumerator.h
Go to the documentation of this file.
00001 /*
00002  * This source file is part of MyGUI. For the latest info, see http://mygui.info/
00003  * Distributed under the MIT License
00004  * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
00005  */
00006 
00007 #ifndef __MYGUI_ENUMERATOR_H__
00008 #define __MYGUI_ENUMERATOR_H__
00009 
00010 #include <assert.h>
00011 
00012 namespace MyGUI
00013 {
00014 
00047     template<typename T>
00048     class Enumerator
00049     {
00050     private:
00051         Enumerator()
00052         {
00053         }
00054 
00055     public:
00056         explicit Enumerator(const T& _container) :
00057             m_first(true),
00058             m_current(_container.begin()),
00059             m_end(_container.end())
00060         {
00061         }
00062 
00063         Enumerator(typename T::const_iterator _first, typename T::const_iterator _end) :
00064             m_first(true),
00065             m_current(_first),
00066             m_end(_end)
00067         {
00068         }
00069 
00070         bool next()
00071         {
00072             if (m_current == m_end)
00073                 return false;
00074             else if (m_first)
00075             {
00076                 m_first = false;
00077                 return true;
00078             }
00079             ++ m_current;
00080             if (m_current == m_end)
00081                 return false;
00082             return true;
00083         }
00084 
00085         typename T::const_reference operator->() const
00086         {
00087             assert(m_current != m_end);
00088             return (*m_current);
00089         }
00090 
00091         typename T::const_reference current()
00092         {
00093             assert(m_current != m_end);
00094             return (*m_current);
00095         }
00096 
00097     private:
00098         bool m_first;
00099         typename T::const_iterator m_current;
00100         typename T::const_iterator m_end;
00101     };
00102 
00103 } // namespace MyGUI
00104 
00105 #endif // __MYGUI_ENUMERATOR_H__