[RTL]
[reactos.git] / reactos / tools / rbuild_helper / stringz_iterator.h
1 /*
2 Copyright (c) 2009 KJK::Hyperion
3
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the "Software"),
6 to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 and/or sell copies of the Software, and to permit persons to whom the
9 Software is furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 DEALINGS IN THE SOFTWARE.
21 */
22
23 #ifndef KJK_STRINGZ_ITERATOR_H_
24 #define KJK_STRINGZ_ITERATOR_H_
25
26 #include <cstddef>
27 #include <iterator>
28
29 namespace kjk
30 {
31 template<class Type, class Traits = std::char_traits<Type> > class stringz_iterator;
32
33 template<class CharT> stringz_iterator<CharT> stringz_begin(const CharT *);
34 template<class Traits, class CharT> stringz_iterator<CharT, Traits> stringz_begin(const CharT *);
35
36 template<class CharT> stringz_iterator<CharT> stringz_end(const CharT *);
37 template<class Traits, class CharT> stringz_iterator<CharT, Traits> stringz_end(const CharT *);
38
39 template<class Type, class Traits>
40 class stringz_iterator: public std::iterator<std::forward_iterator_tag, Type, std::ptrdiff_t, const Type *, const Type&>
41 {
42 private:
43 template<class CharT2> friend stringz_iterator<CharT2> stringz_begin(const CharT2 *);
44 template<class Traits2, class CharT2> friend stringz_iterator<CharT2, Traits2> stringz_begin(const CharT2 *);
45
46 template<class CharT2> friend stringz_iterator<CharT2> stringz_end(const CharT2 *);
47 template<class Traits2, class CharT2> friend stringz_iterator<CharT2, Traits2> stringz_end(const CharT2 *);
48
49 // FIXME: this sucks because GCC sucks
50 typedef const Type * pointer_;
51 typedef const Type& reference_;
52
53 pointer_ m_p;
54
55 static pointer_ set_pointer(pointer_ p)
56 {
57 if(p != 0 && Traits::eq_int_type(Traits::to_int_type(*p), 0))
58 return 0;
59 else
60 return p;
61 }
62
63 explicit stringz_iterator(pointer_ p): m_p(set_pointer(p)) { }
64
65 public:
66 stringz_iterator(): m_p() {}
67 stringz_iterator(const stringz_iterator& That): m_p(set_pointer(That.m_p)) { }
68
69 const stringz_iterator& operator=(const stringz_iterator& That)
70 {
71 m_p = set_pointer(That.m_p);
72 return *this;
73 }
74
75 bool operator==(const stringz_iterator& That) const
76 {
77 return m_p == That.m_p;
78 }
79
80 bool operator!=(const stringz_iterator& That) const
81 {
82 return m_p != That.m_p;
83 }
84
85 reference_ operator*() const { return *m_p; }
86
87 const stringz_iterator& operator++()
88 {
89 m_p = set_pointer(m_p + 1);
90 return *this;
91 }
92
93 stringz_iterator operator++(int)
94 {
95 stringz_iterator oldValue(*this);
96 ++ (*this);
97 return oldValue;
98 }
99
100 pointer_ base() const { return m_p; }
101 };
102
103 template<class CharT>
104 stringz_iterator<CharT> stringz_begin(const CharT * p)
105 {
106 return stringz_iterator<CharT>(p);
107 }
108
109 template<class Traits, class CharT>
110 stringz_iterator<CharT> stringz_begin(const CharT * p)
111 {
112 return stringz_iterator<CharT, Traits>(p);
113 }
114
115 template<class CharT>
116 stringz_iterator<CharT> stringz_end(const CharT *)
117 {
118 return stringz_iterator<CharT>(0);
119 }
120
121 template<class Traits, class CharT>
122 stringz_iterator<CharT> stringz_end(const CharT *)
123 {
124 return stringz_iterator<CharT, Traits>(0);
125 }
126 }
127
128 #endif
129
130 // EOF