00001 00002 // iterator.hpp: defines the enum_iterator type 00003 // 00004 // Copyright 2005 Frank Laub 00005 // Distributed under the Boost Software License, Version 1.0. (See 00006 // accompanying file LICENSE_1_0.txt or copy at 00007 // http://www.boost.org/LICENSE_1_0.txt) 00008 // 00009 00010 #ifndef BOOST_ENUM_ITERATOR_HPP 00011 #define BOOST_ENUM_ITERATOR_HPP 00012 00013 // MS compatible compilers support #pragma once 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 00015 # pragma once 00016 #endif 00017 00018 #include <boost/iterator/iterator_facade.hpp> 00019 00020 namespace boost { 00021 namespace detail { 00022 00023 template <typename T> 00024 BOOST_DEDUCED_TYPENAME T::domain enum_cast( 00025 BOOST_DEDUCED_TYPENAME T::index_type index) 00026 { 00027 return static_cast<BOOST_DEDUCED_TYPENAME T::domain>(index); 00028 } 00029 00030 template <typename T> 00031 class enum_iterator 00032 : public boost::iterator_facade 00033 < enum_iterator<T> 00034 , const T 00035 , boost::random_access_traversal_tag> 00036 { 00037 typedef 00038 boost::iterator_facade 00039 < enum_iterator<T> 00040 , const T 00041 , boost::random_access_traversal_tag> 00042 facade; 00043 00044 typedef enum_iterator<T> this_type; 00045 00046 public: 00047 enum_iterator(size_t index) 00048 : m_value(enum_cast<T>(index)) 00049 , m_index(index) 00050 {} 00051 00052 private: 00053 friend class boost::iterator_core_access; 00054 00055 const T& dereference() const 00056 { 00057 return m_value; 00058 } 00059 00060 void increment() 00061 { 00062 ++m_index; 00063 m_value = enum_cast<T>(m_index); 00064 } 00065 00066 void decrement() 00067 { 00068 --m_index; 00069 m_value = enum_cast<T>(m_index); 00070 } 00071 00072 bool equal(const this_type& rhs) const 00073 { 00074 return m_index == rhs.m_index; 00075 } 00076 00077 void advance(BOOST_DEDUCED_TYPENAME facade::difference_type n) 00078 { 00079 m_index += n; 00080 m_value = enum_cast<T>(m_index); 00081 } 00082 00083 BOOST_DEDUCED_TYPENAME facade::difference_type distance_to( 00084 const this_type& rhs) const 00085 { 00086 return rhs.m_index - m_index; 00087 } 00088 00089 private: 00090 T m_value; 00091 size_t m_index; 00092 }; 00093 00094 } // detail 00095 } // boost 00096 00097 #endif