]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/postypes.h
Remove trailing whitespace (see ChangeLog for longwinded description).
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / postypes.h
1 // Position types -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 27.4.1 - Types
33 // ISO C++ 14882: 27.4.3 - Template class fpos
34 //
35
36 /** @file postypes.h
37 * This is an internal header file, included by other library headers.
38 * You should not attempt to use it directly.
39 */
40
41 #ifndef _GLIBCXX_POSTYPES_H
42 #define _GLIBCXX_POSTYPES_H 1
43
44 #pragma GCC system_header
45
46 #include <cwchar> // For mbstate_t
47
48 #ifdef _GLIBCXX_HAVE_STDINT_H
49 #include <stdint.h> // For int64_t
50 #endif
51
52 namespace std
53 {
54 // The types streamoff, streampos and wstreampos and the class
55 // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
56 // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
57 // behaviour of these types is mostly implementation defined or
58 // unspecified. The behaviour in this implementation is as noted
59 // below.
60
61 #ifdef _GLIBCXX_HAVE_INT64_T
62 typedef int64_t __streamoff_base_type;
63 #else
64 typedef long long __streamoff_base_type;
65 #endif
66
67 /// Integral type for I/O operation counts and buffer sizes.
68 typedef ptrdiff_t streamsize; // Signed integral type
69
70 template<typename _StateT>
71 class fpos;
72
73 // Class streamoff is an implementation defined type that meets the
74 // requirements for streamoff. It stores an offset as a signed
75 // integer. Note: this class is an implementation detail.
76 class streamoff
77 {
78 private:
79 __streamoff_base_type _M_off;
80
81 public:
82 // Nothing in the standard requires that streamoff can be default
83 // constructed. In this implementation a default constructor that
84 // stores the value 0 is provided.
85 streamoff()
86 : _M_off(0) { }
87
88 // The standard only requires that streamoff can be constructed
89 // from streamsize using the constructor syntax. This
90 // implementation also allows implicit conversion from integer
91 // types to streamoff.
92 streamoff(__streamoff_base_type __off)
93 : _M_off(__off) { }
94
95 // The standard requires that streamoff can be constructed from
96 // instances of fpos using the constructor syntax, but gives no
97 // semantics for this construction. In this implementation it
98 // extracts the offset stored by the fpos object.
99 // Note: In versions of GCC up to and including GCC 3.3, implicit
100 // conversion from fpos to streamoff was allowed. This constructor
101 // has now been made explicit to improve type safety.
102 template<typename _StateT>
103 explicit
104 streamoff(const fpos<_StateT>&);
105
106 // The standard requires that streamsize can be constructed from
107 // streamoff using the constructor syntax. This implementation
108 // also allows implicit conversion. This allows streamoff objects
109 // to be used in arithmetic expressions and to be compared against
110 // each other and integer types.
111 operator __streamoff_base_type() const
112 { return _M_off; }
113
114 // This implementation allows the use of operators +=, -=, ++ and
115 // -- on streamoff objects.
116 streamoff&
117 operator+=(__streamoff_base_type __off)
118 {
119 _M_off += __off;
120 return *this;
121 }
122
123 streamoff&
124 operator-=(__streamoff_base_type __off)
125 {
126 _M_off -= __off;
127 return *this;
128 }
129 };
130
131 /**
132 * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
133 *
134 * @if maint
135 * In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
136 * implementation defined type. In this implementation it is a
137 * distinct class type.
138 * Note: In versions of GCC up to and including GCC 3.3, streamoff
139 * was typedef long.
140 * @endif
141 */
142 typedef class streamoff streamoff;
143
144 /**
145 * @brief Class representing stream positions.
146 *
147 * The standard places no requirements upon the template parameter StateT.
148 * In this implementation StateT must be DefaultConstructible,
149 * CopyConstructible and Assignable. The standard only requires that fpos
150 * should contain a member of type StateT. In this implementation it also
151 * contains an offset stored as a signed integer.
152 *
153 * @param StateT Type passed to and returned from state().
154 */
155 template<typename _StateT>
156 class fpos
157 {
158 private:
159 friend class streamoff;
160
161 __streamoff_base_type _M_off;
162 _StateT _M_state;
163
164 public:
165 // The standard doesn't require that fpos objects can be default
166 // constructed. This implementation provides a default
167 // constructor that initializes the offset to 0 and default
168 // constructs the state.
169 fpos()
170 : _M_off(0), _M_state() { }
171
172 // The standard requires implicit conversion from integers to
173 // fpos, but gives no meaningful semantics for this
174 // conversion. In this implementation this constructor stores
175 // the integer as the offset and default constructs the state.
176 /// Construct position from integer.
177 fpos(__streamoff_base_type __off)
178 : _M_off(__off), _M_state() { }
179
180 // The standard requires that fpos objects can be constructed
181 // from streamoff objects using the constructor syntax, and
182 // fails to give any meaningful semantics. In this
183 // implementation implicit conversion is also allowed, and this
184 // constructor stores the streamoff as the offset and default
185 // constructs the state.
186 /// Construct position from offset.
187 fpos(const streamoff& __off)
188 : _M_off(__off), _M_state() { }
189
190 /// Remember the value of @a st.
191 void
192 state(_StateT __st)
193 { _M_state = __st; }
194
195 /// Return the last set value of @a st.
196 _StateT
197 state() const
198 { return _M_state; }
199
200 // The standard only requires that operator== must be an
201 // equivalence relation. In this implementation two fpos<StateT>
202 // objects belong to the same equivalence class if the contained
203 // offsets compare equal.
204 /// Test if equivalent to another position.
205 bool
206 operator==(const fpos& __other) const
207 { return _M_off == __other._M_off; }
208
209 /// Test if not equivalent to another position.
210 bool
211 operator!=(const fpos& __other) const
212 { return _M_off != __other._M_off; }
213
214 // The standard requires that this operator must be defined, but
215 // gives no semantics. In this implemenation it just adds it's
216 // argument to the stored offset and returns *this.
217 /// Add offset to this position.
218 fpos&
219 operator+=(const streamoff& __off)
220 {
221 _M_off += __off;
222 return *this;
223 }
224
225 // The standard requires that this operator must be defined, but
226 // gives no semantics. In this implemenation it just subtracts
227 // it's argument from the stored offset and returns *this.
228 /// Subtract offset from this position.
229 fpos&
230 operator-=(const streamoff& __off)
231 {
232 _M_off -= __off;
233 return *this;
234 }
235
236 // The standard requires that this operator must be defined, but
237 // defines it's semantics only in terms of operator-. In this
238 // implementation it constructs a copy of *this, adds the
239 // argument to that copy using operator+= and then returns the
240 // copy.
241 /// Add position and offset.
242 fpos
243 operator+(const streamoff& __off) const
244 {
245 fpos __pos(*this);
246 __pos += __off;
247 return __pos;
248 }
249
250 // The standard requires that this operator must be defined, but
251 // defines it's semantics only in terms of operator+. In this
252 // implementation it constructs a copy of *this, subtracts the
253 // argument from that copy using operator-= and then returns the
254 // copy.
255 /// Subtract offset from position.
256 fpos
257 operator-(const streamoff& __off) const
258 {
259 fpos __pos(*this);
260 __pos -= __off;
261 return __pos;
262 }
263
264 // The standard requires that this operator must be defined, but
265 // defines it's semantics only in terms of operator+. In this
266 // implementation it returns the difference between the offset
267 // stored in *this and in the argument.
268 /// Subtract position to return offset.
269 streamoff
270 operator-(const fpos& __other) const
271 { return _M_off - __other._M_off; }
272 };
273
274 /// Construct offset from position.
275 template<typename _StateT>
276 inline
277 streamoff::streamoff(const fpos<_StateT>& __pos)
278 : _M_off(__pos._M_off) { }
279
280 // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
281 // as implementation defined types, but clause 27.2 requires that
282 // they must both be typedefs for fpos<mbstate_t>
283 /// File position for char streams.
284 typedef fpos<mbstate_t> streampos;
285 /// File position for wchar_t streams.
286 typedef fpos<mbstate_t> wstreampos;
287 } // namespace std
288
289 #endif