00001 //---------------------------------------------------------------------------- 00002 /** @file 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef BOARD_ITERATOR_HPP 00007 #define BOARD_ITERATOR_HPP 00008 00009 #include "Benzene.hpp" 00010 #include "Hex.hpp" 00011 #include "SafeBool.hpp" 00012 00013 _BEGIN_BENZENE_NAMESPACE_ 00014 00015 //---------------------------------------------------------------------- 00016 00017 /** 00018 * Iterates on an array of HexPoints. The array must end with 00019 * INVALID_POINT, otherwise BoardIterator will just keep on truckin'. 00020 * 00021 * BoardIterator implements the bool operator, so we can do stuff 00022 * like this: 00023 * 00024 * @code for (BoardIterator i(brd.cells()); i; ++i) {...} @endcode 00025 * 00026 * (note the "i" test for validity). operator bool() returns false 00027 * if the iterator is currently pointing at INVALID_POINT and true 00028 * otherwise. 00029 * 00030 * Because BoardIterator uses operator bool(), comparisons like @code 00031 * if (x == y) @endcode will do very different things that you would 00032 * think. To disallow iterator comparisons like the above (instead 00033 * use @code if (*x == *y) @endcode ), we have employed the Safe Bool 00034 * Idiom. Comparing two BoardIterators will result in a compilation 00035 * error. 00036 */ 00037 class BoardIterator : public SafeBool<BoardIterator> 00038 { 00039 public: 00040 00041 /** Empty iterator. */ 00042 BoardIterator(); 00043 00044 /** Destructor. */ 00045 ~BoardIterator(); 00046 00047 /** Iterates over the vector of points begining with start. */ 00048 BoardIterator(const HexPoint* start); 00049 00050 /** Iterates over the vector of points. */ 00051 BoardIterator(const std::vector<HexPoint>& start); 00052 00053 /** Returns the HexPoint at the current location. */ 00054 HexPoint operator*() const; 00055 00056 /** Move to the next point in the list. Incrementing past 00057 an INVALID_POINT gives undefined behavoir. */ 00058 BoardIterator& operator++(); 00059 00060 /** Used by the SafeBool Idiom. */ 00061 bool boolean_test() const; 00062 00063 protected: 00064 const HexPoint* m_point; 00065 }; 00066 00067 inline BoardIterator::BoardIterator() 00068 : m_point(NULL) 00069 { 00070 } 00071 00072 inline BoardIterator::BoardIterator(const HexPoint* start) 00073 : m_point(start) 00074 { 00075 } 00076 00077 inline BoardIterator::BoardIterator(const std::vector<HexPoint>& start) 00078 : m_point(&start[0]) 00079 { 00080 } 00081 00082 inline BoardIterator::~BoardIterator() 00083 { 00084 } 00085 00086 inline HexPoint BoardIterator::operator*() const 00087 { 00088 return *m_point; 00089 } 00090 00091 inline bool BoardIterator::boolean_test() const 00092 { 00093 return (*m_point != INVALID_POINT); 00094 } 00095 00096 inline BoardIterator& BoardIterator::operator++() 00097 { 00098 ++m_point; 00099 return (*this); 00100 } 00101 00102 //---------------------------------------------------------------------- 00103 00104 _END_BENZENE_NAMESPACE_ 00105 00106 #endif // BOARD_ITERATOR_HPP