1 // Position types -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 // ISO C++ 14882: 27.4.1 - Types
33 // ISO C++ 14882: 27.4.3 - Template class fpos
37 * This is an internal header file, included by other library headers.
38 * You should not attempt to use it directly.
41 #ifndef _GLIBCXX_POSTYPES_H
42 #define _GLIBCXX_POSTYPES_H 1
44 #pragma GCC system_header
46 #include <cwchar> // For mbstate_t
48 #ifdef _GLIBCXX_HAVE_STDINT_H
49 #include <stdint.h> // For int64_t
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
61 #ifdef _GLIBCXX_HAVE_INT64_T
62 typedef int64_t __streamoff_base_type
;
64 typedef long long __streamoff_base_type
;
67 /// Integral type for I/O operation counts and buffer sizes.
68 typedef ptrdiff_t streamsize
; // Signed integral type
70 template<typename _StateT
>
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.
79 __streamoff_base_type _M_off
;
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.
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
)
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
>
104 streamoff(const fpos
<_StateT
>&);
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
114 // This implementation allows the use of operators +=, -=, ++ and
115 // -- on streamoff objects.
117 operator+=(__streamoff_base_type __off
)
124 operator-=(__streamoff_base_type __off
)
132 * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
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
142 typedef class streamoff streamoff
;
145 * @brief Class representing stream positions.
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.
153 * @param StateT Type passed to and returned from state().
155 template<typename _StateT
>
159 friend class streamoff
;
161 __streamoff_base_type _M_off
;
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.
170 : _M_off(0), _M_state() { }
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() { }
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() { }
190 /// Remember the value of @a st.
195 /// Return the last set value of @a st.
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.
206 operator==(const fpos
& __other
) const
207 { return _M_off
== __other
._M_off
; }
209 /// Test if not equivalent to another position.
211 operator!=(const fpos
& __other
) const
212 { return _M_off
!= __other
._M_off
; }
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.
219 operator+=(const streamoff
& __off
)
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.
230 operator-=(const streamoff
& __off
)
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
241 /// Add position and offset.
243 operator+(const streamoff
& __off
) const
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
255 /// Subtract offset from position.
257 operator-(const streamoff
& __off
) const
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.
270 operator-(const fpos
& __other
) const
271 { return _M_off
- __other
._M_off
; }
274 /// Construct offset from position.
275 template<typename _StateT
>
277 streamoff::streamoff(const fpos
<_StateT
>& __pos
)
278 : _M_off(__pos
._M_off
) { }
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
;