1 | // Position types -*- C++ -*- |
2 | |
3 | // Copyright (C) 1997-2021 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/postypes.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{iosfwd} |
28 | */ |
29 | |
30 | // |
31 | // ISO C++ 14882: 27.4.1 - Types |
32 | // ISO C++ 14882: 27.4.3 - Template class fpos |
33 | // |
34 | |
35 | #ifndef _GLIBCXX_POSTYPES_H |
36 | #define _GLIBCXX_POSTYPES_H 1 |
37 | |
38 | #pragma GCC system_header |
39 | |
40 | #include <cwchar> // For mbstate_t |
41 | |
42 | // XXX If <stdint.h> is really needed, make sure to define the macros |
43 | // before including it, in order not to break <tr1/cstdint> (and <cstdint> |
44 | // in C++11). Reconsider all this as soon as possible... |
45 | #if (defined(_GLIBCXX_HAVE_INT64_T) && !defined(_GLIBCXX_HAVE_INT64_T_LONG) \ |
46 | && !defined(_GLIBCXX_HAVE_INT64_T_LONG_LONG)) |
47 | |
48 | #ifndef __STDC_LIMIT_MACROS |
49 | # define _UNDEF__STDC_LIMIT_MACROS |
50 | # define __STDC_LIMIT_MACROS |
51 | #endif |
52 | #ifndef __STDC_CONSTANT_MACROS |
53 | # define _UNDEF__STDC_CONSTANT_MACROS |
54 | # define __STDC_CONSTANT_MACROS |
55 | #endif |
56 | #include <stdint.h> // For int64_t |
57 | #ifdef _UNDEF__STDC_LIMIT_MACROS |
58 | # undef __STDC_LIMIT_MACROS |
59 | # undef _UNDEF__STDC_LIMIT_MACROS |
60 | #endif |
61 | #ifdef _UNDEF__STDC_CONSTANT_MACROS |
62 | # undef __STDC_CONSTANT_MACROS |
63 | # undef _UNDEF__STDC_CONSTANT_MACROS |
64 | #endif |
65 | |
66 | #endif |
67 | |
68 | namespace std _GLIBCXX_VISIBILITY(default) |
69 | { |
70 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
71 | |
72 | // The types streamoff, streampos and wstreampos and the class |
73 | // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2, |
74 | // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbiage, the |
75 | // behaviour of these types is mostly implementation defined or |
76 | // unspecified. The behaviour in this implementation is as noted |
77 | // below. |
78 | |
79 | /** |
80 | * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>. |
81 | * |
82 | * In clauses 21.1.3.1 and 27.4.1 streamoff is described as an |
83 | * implementation defined type. |
84 | * Note: In versions of GCC up to and including GCC 3.3, streamoff |
85 | * was typedef long. |
86 | */ |
87 | #ifdef _GLIBCXX_HAVE_INT64_T_LONG |
88 | typedef long streamoff; |
89 | #elif defined(_GLIBCXX_HAVE_INT64_T_LONG_LONG) |
90 | typedef long long streamoff; |
91 | #elif defined(_GLIBCXX_HAVE_INT64_T) |
92 | typedef int64_t streamoff; |
93 | #else |
94 | typedef long long streamoff; |
95 | #endif |
96 | |
97 | /// Integral type for I/O operation counts and buffer sizes. |
98 | typedef ptrdiff_t streamsize; // Signed integral type |
99 | |
100 | /** |
101 | * @brief Class representing stream positions. |
102 | * |
103 | * The standard places no requirements upon the template parameter StateT. |
104 | * In this implementation StateT must be DefaultConstructible, |
105 | * CopyConstructible and Assignable. The standard only requires that fpos |
106 | * should contain a member of type StateT. In this implementation it also |
107 | * contains an offset stored as a signed integer. |
108 | * |
109 | * @param StateT Type passed to and returned from state(). |
110 | */ |
111 | template<typename _StateT> |
112 | class fpos |
113 | { |
114 | private: |
115 | streamoff _M_off; |
116 | _StateT _M_state; |
117 | |
118 | public: |
119 | // The standard doesn't require that fpos objects can be default |
120 | // constructed. This implementation provides a default |
121 | // constructor that initializes the offset to 0 and default |
122 | // constructs the state. |
123 | fpos() |
124 | : _M_off(0), _M_state() { } |
125 | |
126 | // The standard requires that fpos objects can be constructed |
127 | // from streamoff objects using the constructor syntax, and |
128 | // fails to give any meaningful semantics. In this |
129 | // implementation implicit conversion is also allowed, and this |
130 | // constructor stores the streamoff as the offset and default |
131 | // constructs the state. |
132 | /// Construct position from offset. |
133 | fpos(streamoff __off) |
134 | : _M_off(__off), _M_state() { } |
135 | |
136 | #if __cplusplus >= 201103L |
137 | fpos(const fpos&) = default; |
138 | fpos& operator=(const fpos&) = default; |
139 | ~fpos() = default; |
140 | #endif |
141 | |
142 | /// Convert to streamoff. |
143 | operator streamoff() const { return _M_off; } |
144 | |
145 | /// Remember the value of @a st. |
146 | void |
147 | state(_StateT __st) |
148 | { _M_state = __st; } |
149 | |
150 | /// Return the last set value of @a st. |
151 | _StateT |
152 | state() const |
153 | { return _M_state; } |
154 | |
155 | // The standard requires that this operator must be defined, but |
156 | // gives no semantics. In this implementation it just adds its |
157 | // argument to the stored offset and returns *this. |
158 | /// Add offset to this position. |
159 | fpos& |
160 | operator+=(streamoff __off) |
161 | { |
162 | _M_off += __off; |
163 | return *this; |
164 | } |
165 | |
166 | // The standard requires that this operator must be defined, but |
167 | // gives no semantics. In this implementation it just subtracts |
168 | // its argument from the stored offset and returns *this. |
169 | /// Subtract offset from this position. |
170 | fpos& |
171 | operator-=(streamoff __off) |
172 | { |
173 | _M_off -= __off; |
174 | return *this; |
175 | } |
176 | |
177 | // The standard requires that this operator must be defined, but |
178 | // defines its semantics only in terms of operator-. In this |
179 | // implementation it constructs a copy of *this, adds the |
180 | // argument to that copy using operator+= and then returns the |
181 | // copy. |
182 | /// Add position and offset. |
183 | fpos |
184 | operator+(streamoff __off) const |
185 | { |
186 | fpos __pos(*this); |
187 | __pos += __off; |
188 | return __pos; |
189 | } |
190 | |
191 | // The standard requires that this operator must be defined, but |
192 | // defines its semantics only in terms of operator+. In this |
193 | // implementation it constructs a copy of *this, subtracts the |
194 | // argument from that copy using operator-= and then returns the |
195 | // copy. |
196 | /// Subtract offset from position. |
197 | fpos |
198 | operator-(streamoff __off) const |
199 | { |
200 | fpos __pos(*this); |
201 | __pos -= __off; |
202 | return __pos; |
203 | } |
204 | |
205 | // The standard requires that this operator must be defined, but |
206 | // defines its semantics only in terms of operator+. In this |
207 | // implementation it returns the difference between the offset |
208 | // stored in *this and in the argument. |
209 | /// Subtract position to return offset. |
210 | streamoff |
211 | operator-(const fpos& __other) const |
212 | { return _M_off - __other._M_off; } |
213 | }; |
214 | |
215 | // The standard only requires that operator== must be an |
216 | // equivalence relation. In this implementation two fpos<StateT> |
217 | // objects belong to the same equivalence class if the contained |
218 | // offsets compare equal. |
219 | /// Test if equivalent to another position. |
220 | template<typename _StateT> |
221 | inline bool |
222 | operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs) |
223 | { return streamoff(__lhs) == streamoff(__rhs); } |
224 | |
225 | template<typename _StateT> |
226 | inline bool |
227 | operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs) |
228 | { return streamoff(__lhs) != streamoff(__rhs); } |
229 | |
230 | // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos |
231 | // as implementation defined types, but clause 27.2 requires that |
232 | // they must both be typedefs for fpos<mbstate_t> |
233 | /// File position for char streams. |
234 | typedef fpos<mbstate_t> streampos; |
235 | /// File position for wchar_t streams. |
236 | typedef fpos<mbstate_t> wstreampos; |
237 | |
238 | #ifdef _GLIBCXX_USE_CHAR8_T |
239 | /// File position for char8_t streams. |
240 | typedef fpos<mbstate_t> u8streampos; |
241 | #endif |
242 | |
243 | #if __cplusplus >= 201103L |
244 | /// File position for char16_t streams. |
245 | typedef fpos<mbstate_t> u16streampos; |
246 | /// File position for char32_t streams. |
247 | typedef fpos<mbstate_t> u32streampos; |
248 | #endif |
249 | |
250 | _GLIBCXX_END_NAMESPACE_VERSION |
251 | } // namespace |
252 | |
253 | #endif |
254 | |