File: iterator.h

package info (click to toggle)
xstow 1.0.2-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,064 kB
  • ctags: 1,073
  • sloc: cpp: 7,264; sh: 1,216; makefile: 196; awk: 26
file content (133 lines) | stat: -rw-r--r-- 3,925 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * $Log: iterator.h,v $
 * Revision 1.2  2005/07/04 21:59:42  martin
 * added logging to all files
 *
 */
#ifndef iterator_h
#define iterator_h

/**
   This template is so simple that it can be used without
   extending the effektive size of the binary. It simple has no effekt to
   the binary size cause it can completly removed by the optimizer.
*/

#include <iterator>

  /** template that returns the real type of a pointer */
  template<typename T> struct remove_pointer 
  { 
    typedef typename T::value_type result;
  };

  template<typename T> struct remove_pointer<T*>
  {
    typedef T result;
  };

  /** template that returns a pointer of a type */
  template<typename T> struct get_pointer 
  { 
    typedef T *result;
  };

  template<typename T> struct get_pointer<T*>
  { 
    typedef T *result;
  };

  template<typename T> struct pointer_traits
  {
    typedef typename T::pointer pointer;
    typedef typename T::value_type value_type;
  }; 

  template<typename T> struct pointer_traits<T*>
  {
    typedef T* pointer;
    typedef T  value_type;
  }; 

  
  /** 
      \class Iterator
      \brief iterator forward class template for pointers and smart pointers

      If a container holds pointers, or smart pointers the iterator access 
      on the real objects behind the iterators is tough.

      This class forwards the real objects, standing behind the pointers.

      By specialicing the pointer_traits template it's easy using this class
      with smart pointers that do not have the required typedefs.

      An example of the usage of this class can be found there: \link Iterator_Example.cc Iterator_Example.cc \endlink
  */
  template<typename iterator, typename _pointer = typename remove_pointer<iterator>::result > struct Iterator
  {
    typedef typename std::iterator_traits<iterator>::iterator_category iterator_category;
    typedef typename std::iterator_traits<iterator>::difference_type   difference_type;

    typedef typename pointer_traits<_pointer>::value_type value_type;
    typedef typename pointer_traits<_pointer>::pointer    pointer;
    typedef typename pointer_traits<_pointer>::value_type &reference;

    iterator it; ///< the original iterator

    Iterator() {}

    Iterator( iterator it ) : it( it ) {}    

    bool operator!=( const iterator &i ) const { return it != i; }
    bool operator!=( const Iterator<iterator,_pointer> &i ) const { return it != i.it; }
    
    bool operator==( const iterator &i ) const { return it == i; }
    bool operator==( const Iterator<iterator,_pointer> &i ) const { return it == i.it; }

    Iterator<iterator,_pointer>& operator=( const iterator& i ) { return it = i; }
    Iterator<iterator,_pointer>& operator--() { --it; return *this; }
    Iterator<iterator,_pointer>& operator++() { ++it; return *this; }
    
    bool operator<( const Iterator<iterator,_pointer> &i ) const
    { 
      return it < i.it;
    }

    bool operator>( const Iterator<iterator,_pointer> &i ) const
    { 
      return it > i.it;
    }
    
    bool operator<=( const Iterator<iterator,_pointer> &i ) const
    { 
      return it <= i.it;
    }

    bool operator>=( const Iterator<iterator,_pointer> &i ) const
    { 
      return it >= i.it;
    }

    pointer operator->() { return *it; }
    reference operator*() { return *(*it); }
    reference operator[]( int i ) { return *(it[i]); }
    
    Iterator<iterator,_pointer>& operator+=( int i ) { it += i; return *this; }
    Iterator<iterator,_pointer>& operator-=( int i ) { it -= i; return *this; }

    /// returns the address of the current value
    pointer address() const { return *it; }
  };

  template<typename T, typename U> Iterator<T,U> operator+( const Iterator<T,U> &a, int i )
  {
    return Iterator<T,U>(a) + i;
  }

  template<typename T, typename U> Iterator<T,U> operator-( const Iterator<T,U> &a, int i )
  {
    return Iterator<T,U>(a) - i;
  }

#endif