aimstil  5.0.5
basic_iterator.h
Go to the documentation of this file.
1 #ifndef TIL_BASIC_ITERATOR_H
2 #define TIL_BASIC_ITERATOR_H
3 
4 // includes from STL
5 #include <iterator>
6 
7 // includes from BOOST
8 #include "boost/type_traits.hpp"
9 
10 // includes from TIL
11 #include "til/til_common.h"
12 
13 namespace til
14 {
19  // TODO: it seems that std::iterator does not define const_reference...
20  // is it standard or just MSVC?
21  template < typename T >
22  class basic_iterator : public std::iterator<std::random_access_iterator_tag, T>
23  {
24  public://
25  typedef std::iterator<std::random_access_iterator_tag, T> Base;
26  //TODO: why is this crap necessary?? Should peep into some of the stl algo
27  // and look, if indeed value_type of const_iterators are const, how they
28  // deal with that.
29  typedef typename boost::remove_const<typename Base::value_type>::type value_type;
30  public: // constructors & destructor
31  basic_iterator() : Base(), m_pointer() {}
32  basic_iterator(T * p) : Base(), m_pointer(p) {}
33  template < typename X >
34  basic_iterator(basic_iterator<X> i) : Base(), m_pointer((T*)i) {}
35  public: // operators
36  void operator++() { ++m_pointer; }
37  // NB: it probably doesn't make sense to have a const operator*. That's probably
38  // why iterators don't define const_reference...
39  //const T & operator*() const { return *m_pointer; }
40  T & operator*() { return *m_pointer; }
41  operator T* () { return m_pointer; }
42  T * operator->() { return m_pointer; }
43  public: // friends
44  template < typename T1, typename T2 >
45  friend bool operator==(const basic_iterator<T1> & i1, const basic_iterator<T2> & i2);
46  template < typename T1, typename T2 >
47  friend bool operator!=(const basic_iterator<T1> & i1, const basic_iterator<T2> & i2);
48  private: // data
49  T * m_pointer;
50  };
51 
52  // NB: actually, the fact that we have two template parameters T1 and T2 won't
53  // enable us to compare pointers on different object. This is just a mean to compare
54  // const and non-const pointers.
55  template < typename T1, typename T2 >
56  inline
57  bool operator==(const basic_iterator<T1> & i1, const basic_iterator<T2> & i2)
58  {
59  //return static_cast<T1*>(i1) == static_cast<T2*>(i2);
60  return i1.m_pointer == i2.m_pointer;
61  }
62 
63  template < typename T1, typename T2 >
64  inline
65  bool operator!=(const basic_iterator<T1> & i1, const basic_iterator<T2> & i2)
66  {
67  //return static_cast<T1*>(i1) != static_cast<T2*>(i2);
68  return i1.m_pointer != i2.m_pointer;
69  }
70 
71 } // namespace til
72 
73 #endif
74 
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
friend bool operator!=(const basic_iterator< T1 > &i1, const basic_iterator< T2 > &i2)
friend bool operator==(const basic_iterator< T1 > &i1, const basic_iterator< T2 > &i2)
General macros, definitions and functions.
A simple wrapper around T*.
std::iterator< std::random_access_iterator_tag, T > Base
basic_iterator(basic_iterator< X > i)
boost::remove_const< typename Base::value_type >::type value_type