1 // Position types -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 27.4.1 - Types
39 // ISO C++ 14882: 27.4.3 - Template class fpos
42 #ifndef _GLIBCXX_POSTYPES_H
43 #define _GLIBCXX_POSTYPES_H 1
45 #pragma GCC system_header
47 #include <cwchar> // For mbstate_t
49 #ifdef _GLIBCXX_HAVE_STDINT_H
50 #include <stdint.h> // For int64_t
53 _GLIBCXX_BEGIN_NAMESPACE(std)
55 // The types streamoff, streampos and wstreampos and the class
56 // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
57 // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
58 // behaviour of these types is mostly implementation defined or
59 // unspecified. The behaviour in this implementation is as noted
63 * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
66 * In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
67 * implementation defined type.
68 * Note: In versions of GCC up to and including GCC 3.3, streamoff
72 #ifdef _GLIBCXX_HAVE_INT64_T
73 typedef int64_t streamoff;
75 typedef long long streamoff;
78 /// Integral type for I/O operation counts and buffer sizes.
79 typedef ptrdiff_t streamsize; // Signed integral type
82 * @brief Class representing stream positions.
84 * The standard places no requirements upon the template parameter StateT.
85 * In this implementation StateT must be DefaultConstructible,
86 * CopyConstructible and Assignable. The standard only requires that fpos
87 * should contain a member of type StateT. In this implementation it also
88 * contains an offset stored as a signed integer.
90 * @param StateT Type passed to and returned from state().
92 template<typename _StateT>
100 // The standard doesn't require that fpos objects can be default
101 // constructed. This implementation provides a default
102 // constructor that initializes the offset to 0 and default
103 // constructs the state.
105 : _M_off(0), _M_state() { }
107 // The standard requires that fpos objects can be constructed
108 // from streamoff objects using the constructor syntax, and
109 // fails to give any meaningful semantics. In this
110 // implementation implicit conversion is also allowed, and this
111 // constructor stores the streamoff as the offset and default
112 // constructs the state.
113 /// Construct position from offset.
114 fpos(streamoff __off)
115 : _M_off(__off), _M_state() { }
117 /// Convert to streamoff.
118 operator streamoff() const { return _M_off; }
120 /// Remember the value of @a st.
125 /// Return the last set value of @a st.
130 // The standard requires that this operator must be defined, but
131 // gives no semantics. In this implemenation it just adds it's
132 // argument to the stored offset and returns *this.
133 /// Add offset to this position.
135 operator+=(streamoff __off)
141 // The standard requires that this operator must be defined, but
142 // gives no semantics. In this implemenation it just subtracts
143 // it's argument from the stored offset and returns *this.
144 /// Subtract offset from this position.
146 operator-=(streamoff __off)
152 // The standard requires that this operator must be defined, but
153 // defines it's semantics only in terms of operator-. In this
154 // implementation it constructs a copy of *this, adds the
155 // argument to that copy using operator+= and then returns the
157 /// Add position and offset.
159 operator+(streamoff __off) const
166 // The standard requires that this operator must be defined, but
167 // defines it's semantics only in terms of operator+. In this
168 // implementation it constructs a copy of *this, subtracts the
169 // argument from that copy using operator-= and then returns the
171 /// Subtract offset from position.
173 operator-(streamoff __off) const
180 // The standard requires that this operator must be defined, but
181 // defines it's semantics only in terms of operator+. In this
182 // implementation it returns the difference between the offset
183 // stored in *this and in the argument.
184 /// Subtract position to return offset.
186 operator-(const fpos& __other) const
187 { return _M_off - __other._M_off; }
190 // The standard only requires that operator== must be an
191 // equivalence relation. In this implementation two fpos<StateT>
192 // objects belong to the same equivalence class if the contained
193 // offsets compare equal.
194 /// Test if equivalent to another position.
195 template<typename _StateT>
197 operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
198 { return streamoff(__lhs) == streamoff(__rhs); }
200 template<typename _StateT>
202 operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
203 { return streamoff(__lhs) != streamoff(__rhs); }
205 // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
206 // as implementation defined types, but clause 27.2 requires that
207 // they must both be typedefs for fpos<mbstate_t>
208 /// File position for char streams.
209 typedef fpos<mbstate_t> streampos;
210 /// File position for wchar_t streams.
211 typedef fpos<mbstate_t> wstreampos;
213 _GLIBCXX_END_NAMESPACE