/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/iterator

  • Committer: edam
  • Date: 2012-02-25 01:31:43 UTC
  • Revision ID: tim@ed.am-20120225013143-9fet2y2d3fjlrwez
added ulibc

Show diffs side-by-side

added added

removed removed

 
1
/*      Copyright (C) 2004 Garrett A. Kajmowicz
 
2
 
 
3
        This file is part of the uClibc++ Library.
 
4
 
 
5
        This library is free software; you can redistribute it and/or
 
6
        modify it under the terms of the GNU Lesser General Public
 
7
        License as published by the Free Software Foundation; either
 
8
        version 2.1 of the License, or (at your option) any later version.
 
9
 
 
10
        This library is distributed in the hope that it will be useful,
 
11
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
        Lesser General Public License for more details.
 
14
 
 
15
        You should have received a copy of the GNU Lesser General Public
 
16
        License along with this library; if not, write to the Free Software
 
17
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
*/
 
19
 
 
20
#include <basic_definitions>
 
21
#include <iosfwd>
 
22
#include <cstddef>
 
23
#include <char_traits>
 
24
#include <iterator_base>
 
25
 
 
26
 
 
27
 
 
28
#ifndef __STD_HEADER_ITERATOR
 
29
#define __STD_HEADER_ITERATOR 1
 
30
 
 
31
#pragma GCC visibility push(default)
 
32
 
 
33
namespace std{
 
34
 
 
35
  // subclause _lib.stream.iterators_, stream iterators:
 
36
        template <class T, class charT = char, class traits = char_traits<charT>, class Distance = ptrdiff_t> class istream_iterator;
 
37
        template <class T, class charT, class traits, class Distance> bool 
 
38
                operator==(const istream_iterator<T,charT,traits,Distance>& x, const istream_iterator<T,charT,traits,Distance>& y);
 
39
        template <class T, class charT, class traits, class Distance> bool 
 
40
                operator!=(const istream_iterator<T,charT,traits,Distance>& x, const istream_iterator<T,charT,traits,Distance>& y);
 
41
        template <class T, class charT = char, class traits = char_traits<charT> > class ostream_iterator;
 
42
        template<class charT, class traits = char_traits<charT> > class istreambuf_iterator;
 
43
        template <class charT, class traits> bool 
 
44
                operator==(const istreambuf_iterator<charT,traits>& a, const istreambuf_iterator<charT,traits>& b);
 
45
        template <class charT, class traits> bool 
 
46
                operator!=(const istreambuf_iterator<charT,traits>& a, const istreambuf_iterator<charT,traits>& b);
 
47
        template <class charT, class traits = char_traits<charT> > class ostreambuf_iterator;
 
48
 
 
49
        
 
50
        template < class T, class charT, class traits, class Distance > class _UCXXEXPORT istream_iterator
 
51
                : public iterator<input_iterator_tag,T,Distance,const T*, const T&>
 
52
        {
 
53
        public:
 
54
                typedef charT char_type;
 
55
                typedef traits traits_type;
 
56
                typedef basic_istream<charT,traits> istream_type;
 
57
                istream_iterator() : in_stream(0), value(0) {}
 
58
                istream_iterator(istream_type& s) : in_stream(&s), value() {
 
59
                        *in_stream >> value;
 
60
                }
 
61
                istream_iterator(const istream_iterator<T,charT,traits,Distance>& x)
 
62
                        : in_stream(x.in_stream), value(x.value)
 
63
                { }
 
64
                ~istream_iterator() { }
 
65
                const T& operator*() const{
 
66
                        return value;
 
67
                }
 
68
                const T* operator->() const{
 
69
                        return &value;
 
70
                }
 
71
                istream_iterator<T,charT,traits,Distance>& operator++() {
 
72
                        *in_stream >> value;
 
73
                        return *this;
 
74
                }
 
75
                istream_iterator<T,charT,traits,Distance>  operator++(int){
 
76
                        istream_iterator<T,charT,traits,Distance> tmp = *this;
 
77
                        *in_stream >> value;
 
78
                        return (tmp);
 
79
                }
 
80
                bool m_equal(const istream_iterator<T,charT,traits,Distance>& x) const{
 
81
                        return (in_stream == x.in_stream);
 
82
                }
 
83
        private:
 
84
                basic_istream<charT,traits>* in_stream;
 
85
                T value;
 
86
        };
 
87
 
 
88
        template <class T, class charT, class traits, class Distance> _UCXXEXPORT 
 
89
                bool operator==(const istream_iterator<T,charT,traits,Distance>& x,
 
90
                const istream_iterator<T,charT,traits,Distance>& y)
 
91
        {
 
92
                return x.m_equal(y);
 
93
        }
 
94
 
 
95
        template <class T, class charT, class traits, class Distance> _UCXXEXPORT 
 
96
                bool operator!=(const istream_iterator<T,charT,traits,Distance>& x,
 
97
                const istream_iterator<T,charT,traits,Distance>& y)
 
98
        {
 
99
                return !(x == y);
 
100
        }
 
101
 
 
102
        template <class T, class charT, class traits> class _UCXXEXPORT ostream_iterator
 
103
                : public iterator<output_iterator_tag,void,void,void,void>
 
104
        {
 
105
        public:
 
106
                typedef charT char_type;
 
107
                typedef traits traits_type;
 
108
                typedef basic_ostream<charT,traits> ostream_type;
 
109
 
 
110
                ostream_iterator(ostream_type& s) : out_stream(&s), delim(0) { }
 
111
                ostream_iterator(ostream_type& s, const charT* delimiter) : out_stream(&s), delim(delimiter) { }
 
112
                ostream_iterator(const ostream_iterator<T,charT,traits>& x) : out_stream(x.out_stream), delim(x.delim) { }
 
113
                ~ostream_iterator() { }
 
114
                ostream_iterator<T,charT,traits>& operator=(const T& value){
 
115
                        *out_stream << value;
 
116
                        if(delim != 0){
 
117
                                *out_stream << delim;
 
118
                        }
 
119
                        return (*this);
 
120
                }
 
121
                ostream_iterator<T,charT,traits>& operator*(){ return *this; }
 
122
                ostream_iterator<T,charT,traits>& operator++() { return *this; }
 
123
                ostream_iterator<T,charT,traits> operator++(int) { return *this; }
 
124
        private:
 
125
                basic_ostream<charT,traits>* out_stream;
 
126
                const char* delim;
 
127
        };
 
128
 
 
129
        template<class charT, class traits > class _UCXXEXPORT istreambuf_iterator : 
 
130
                public iterator<input_iterator_tag, charT, typename traits::off_type, charT*, charT&>
 
131
        {
 
132
        public:
 
133
                typedef charT                           char_type;
 
134
                typedef traits                          traits_type;
 
135
                typedef typename traits::int_type       int_type;
 
136
                typedef basic_streambuf<charT,traits>   streambuf_type;
 
137
                typedef basic_istream<charT,traits>     istream_type;
 
138
 
 
139
                class _UCXXEXPORT proxy{
 
140
                        charT val;
 
141
                        basic_streambuf<charT, traits> * buf;
 
142
                        
 
143
                        proxy(charT v, basic_streambuf<charT, traits> * b) : val(v), buf(b) { }
 
144
                public:
 
145
                        charT operator*() { return val; }
 
146
                };
 
147
 
 
148
                istreambuf_iterator() throw() : sbuf(0) { }
 
149
                istreambuf_iterator(istream_type& s) throw() : sbuf(s.rdbuf()) { }
 
150
                istreambuf_iterator(streambuf_type* s) throw() : sbuf(s) { }
 
151
                istreambuf_iterator(const proxy& p) throw() : sbuf(&p.buf) { }
 
152
 
 
153
                charT operator*() const{
 
154
                        return sbuf->sgetc();
 
155
                }
 
156
                istreambuf_iterator<charT,traits>& operator++(){
 
157
                        sbuf->sbumpc();
 
158
                        return *this;
 
159
                }
 
160
                proxy operator++(int){
 
161
                        istreambuf_iterator<charT,traits> tmp = *this;
 
162
                        sbuf->sbumpc();
 
163
                        return(tmp);
 
164
                }
 
165
 
 
166
                bool equal(const istreambuf_iterator& b) const{
 
167
                        return sbuf == b.sbuf || is_eof() && b.is_eof();
 
168
                }
 
169
        private:
 
170
                streambuf_type* sbuf;
 
171
                inline bool is_eof() const{
 
172
                        return sbuf == 0 || sbuf->sgetc() == traits_type::eof();
 
173
                }
 
174
        };
 
175
 
 
176
        template <class charT, class traits> _UCXXEXPORT bool
 
177
                operator==(const istreambuf_iterator<charT,traits>& a,
 
178
                const istreambuf_iterator<charT,traits>& b)
 
179
        {
 
180
                return a.equal(b);
 
181
        }
 
182
 
 
183
        template <class charT, class traits> bool _UCXXEXPORT 
 
184
                operator!=(const istreambuf_iterator<charT,traits>& a,
 
185
                const istreambuf_iterator<charT,traits>& b)
 
186
        {
 
187
                return !a.equal(b);
 
188
        }
 
189
 
 
190
        template <class charT, class traits> class _UCXXEXPORT ostreambuf_iterator
 
191
                : iterator<output_iterator_tag,void,void,void,void>
 
192
        {
 
193
        public:
 
194
                typedef charT                         char_type;
 
195
                typedef traits                        traits_type;
 
196
                typedef basic_streambuf<charT,traits> streambuf_type;
 
197
                typedef basic_ostream<charT,traits>   ostream_type;
 
198
        public:
 
199
                ostreambuf_iterator(ostream_type& s) throw() : sbuf(s.rdbuf()), f(false) { }
 
200
                ostreambuf_iterator(streambuf_type* s) throw() : sbuf(s), f(false) { }
 
201
                ostreambuf_iterator& operator=(charT c){
 
202
                        if(failed() == false){
 
203
                                if(sbuf->sputc(c) == traits::eof()){
 
204
                                        f = true;
 
205
                                }
 
206
                        }
 
207
                        return *this;
 
208
                }
 
209
                ostreambuf_iterator& operator*(){
 
210
                        return *this;
 
211
                }
 
212
                ostreambuf_iterator& operator++() { return *this; }
 
213
                ostreambuf_iterator operator++(int) { return *this; }
 
214
                bool failed() const throw(){
 
215
                        return f;
 
216
                }
 
217
 
 
218
        private:
 
219
                streambuf_type* sbuf;
 
220
                bool f;
 
221
        };
 
222
 
 
223
}
 
224
 
 
225
#pragma GCC visibility pop
 
226
 
 
227
#endif
 
228
 
 
229