1 // Position types -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 27.4.1 - Types
38 // ISO C++ 14882: 27.4.3 - Template class fpos
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
52 _GLIBCXX_BEGIN_NAMESPACE(std)
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
62 * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
65 * In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
66 * implementation defined type.
67 * Note: In versions of GCC up to and including GCC 3.3, streamoff
71 #ifdef _GLIBCXX_HAVE_INT64_T
72 typedef int64_t streamoff;
74 typedef long long streamoff;
77 /// Integral type for I/O operation counts and buffer sizes.
78 typedef ptrdiff_t streamsize; // Signed integral type
80 template<typename _StateT>
84 * @brief Class representing stream positions.
86 * The standard places no requirements upon the template parameter StateT.
87 * In this implementation StateT must be DefaultConstructible,
88 * CopyConstructible and Assignable. The standard only requires that fpos
89 * should contain a member of type StateT. In this implementation it also
90 * contains an offset stored as a signed integer.
92 * @param StateT Type passed to and returned from state().
94 template<typename _StateT>
102 // The standard doesn't require that fpos objects can be default
103 // constructed. This implementation provides a default
104 // constructor that initializes the offset to 0 and default
105 // constructs the state.
107 : _M_off(0), _M_state() { }
109 // The standard requires that fpos objects can be constructed
110 // from streamoff objects using the constructor syntax, and
111 // fails to give any meaningful semantics. In this
112 // implementation implicit conversion is also allowed, and this
113 // constructor stores the streamoff as the offset and default
114 // constructs the state.
115 /// Construct position from offset.
116 fpos(streamoff __off)
117 : _M_off(__off), _M_state() { }
119 /// Convert to streamoff.
120 operator streamoff() const { return _M_off; }
122 /// Remember the value of @a st.
127 /// Return the last set value of @a st.
132 // The standard requires that this operator must be defined, but
133 // gives no semantics. In this implemenation it just adds it's
134 // argument to the stored offset and returns *this.
135 /// Add offset to this position.
137 operator+=(streamoff __off)
143 // The standard requires that this operator must be defined, but
144 // gives no semantics. In this implemenation it just subtracts
145 // it's argument from the stored offset and returns *this.
146 /// Subtract offset from this position.
148 operator-=(streamoff __off)
154 // The standard requires that this operator must be defined, but
155 // defines it's semantics only in terms of operator-. In this
156 // implementation it constructs a copy of *this, adds the
157 // argument to that copy using operator+= and then returns the
159 /// Add position and offset.
161 operator+(streamoff __off) const
168 // The standard requires that this operator must be defined, but
169 // defines it's semantics only in terms of operator+. In this
170 // implementation it constructs a copy of *this, subtracts the
171 // argument from that copy using operator-= and then returns the
173 /// Subtract offset from position.
175 operator-(streamoff __off) const
182 // The standard requires that this operator must be defined, but
183 // defines it's semantics only in terms of operator+. In this
184 // implementation it returns the difference between the offset
185 // stored in *this and in the argument.
186 /// Subtract position to return offset.
188 operator-(const fpos& __other) const
189 { return _M_off - __other._M_off; }
192 // The standard only requires that operator== must be an
193 // equivalence relation. In this implementation two fpos<StateT>
194 // objects belong to the same equivalence class if the contained
195 // offsets compare equal.
196 /// Test if equivalent to another position.
197 template<typename _StateT>
199 operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
200 { return streamoff(__lhs) == streamoff(__rhs); }
202 template<typename _StateT>
204 operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
205 { return streamoff(__lhs) != streamoff(__rhs); }
207 // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
208 // as implementation defined types, but clause 27.2 requires that
209 // they must both be typedefs for fpos<mbstate_t>
210 /// File position for char streams.
211 typedef fpos<mbstate_t> streampos;
212 /// File position for wchar_t streams.
213 typedef fpos<mbstate_t> wstreampos;
215 _GLIBCXX_END_NAMESPACE