]>
Commit | Line | Data |
---|---|---|
b812c14e | 1 | // Position types -*- C++ -*- |
2 | ||
fbd26352 | 3 | // Copyright (C) 1997-2019 Free Software Foundation, Inc. |
b812c14e | 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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
b812c14e | 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 | ||
6bc9506f | 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/>. | |
b812c14e | 24 | |
5846aeac | 25 | /** @file bits/postypes.h |
b812c14e | 26 | * This is an internal header file, included by other library headers. |
5846aeac | 27 | * Do not attempt to use it directly. @headername{iosfwd} |
b812c14e | 28 | */ |
29 | ||
944beac5 | 30 | // |
31 | // ISO C++ 14882: 27.4.1 - Types | |
32 | // ISO C++ 14882: 27.4.3 - Template class fpos | |
33 | // | |
34 | ||
b812c14e | 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 | ||
b0a4df61 | 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> | |
1eb7eb4d | 44 | // in C++11). Reconsider all this as soon as possible... |
e061cb0f | 45 | #if (defined(_GLIBCXX_HAVE_INT64_T) && !defined(_GLIBCXX_HAVE_INT64_T_LONG) \ |
46 | && !defined(_GLIBCXX_HAVE_INT64_T_LONG_LONG)) | |
b0a4df61 | 47 | |
02bfa9dc | 48 | #ifndef __STDC_LIMIT_MACROS |
b0a4df61 | 49 | # define _UNDEF__STDC_LIMIT_MACROS |
02bfa9dc | 50 | # define __STDC_LIMIT_MACROS |
51 | #endif | |
52 | #ifndef __STDC_CONSTANT_MACROS | |
b0a4df61 | 53 | # define _UNDEF__STDC_CONSTANT_MACROS |
02bfa9dc | 54 | # define __STDC_CONSTANT_MACROS |
55 | #endif | |
56 | #include <stdint.h> // For int64_t | |
b0a4df61 | 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 | ||
02bfa9dc | 66 | #endif |
67 | ||
2948dd21 | 68 | namespace std _GLIBCXX_VISIBILITY(default) |
69 | { | |
70 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
1069247d | 71 | |
b812c14e | 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, | |
9fc1117c | 74 | // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbiage, the |
b812c14e | 75 | // behaviour of these types is mostly implementation defined or |
76 | // unspecified. The behaviour in this implementation is as noted | |
440c107f | 77 | // below. |
78 | ||
8832296b | 79 | /** |
80 | * @brief Type used by fpos, char_traits<char>, and char_traits<wchar_t>. | |
81 | * | |
8832296b | 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. | |
8832296b | 86 | */ |
e061cb0f | 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) | |
02bfa9dc | 92 | typedef int64_t streamoff; |
440c107f | 93 | #else |
8832296b | 94 | typedef long long streamoff; |
440c107f | 95 | #endif |
96 | ||
ed73ad37 | 97 | /// Integral type for I/O operation counts and buffer sizes. |
b812c14e | 98 | typedef ptrdiff_t streamsize; // Signed integral type |
99 | ||
ed73ad37 | 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 | */ | |
b812c14e | 111 | template<typename _StateT> |
112 | class fpos | |
113 | { | |
114 | private: | |
8832296b | 115 | streamoff _M_off; |
bae9b8af | 116 | _StateT _M_state; |
b812c14e | 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 | ||
b812c14e | 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. | |
ed73ad37 | 132 | /// Construct position from offset. |
8832296b | 133 | fpos(streamoff __off) |
b812c14e | 134 | : _M_off(__off), _M_state() { } |
135 | ||
89554925 | 136 | #if __cplusplus >= 201103L |
137 | fpos(const fpos&) = default; | |
138 | fpos& operator=(const fpos&) = default; | |
139 | ~fpos() = default; | |
140 | #endif | |
141 | ||
8832296b | 142 | /// Convert to streamoff. |
143 | operator streamoff() const { return _M_off; } | |
144 | ||
ed73ad37 | 145 | /// Remember the value of @a st. |
b812c14e | 146 | void |
147 | state(_StateT __st) | |
148 | { _M_state = __st; } | |
149 | ||
ed73ad37 | 150 | /// Return the last set value of @a st. |
b812c14e | 151 | _StateT |
152 | state() const | |
153 | { return _M_state; } | |
154 | ||
b812c14e | 155 | // The standard requires that this operator must be defined, but |
9fc1117c | 156 | // gives no semantics. In this implementation it just adds its |
b812c14e | 157 | // argument to the stored offset and returns *this. |
ed73ad37 | 158 | /// Add offset to this position. |
b812c14e | 159 | fpos& |
8832296b | 160 | operator+=(streamoff __off) |
b812c14e | 161 | { |
162 | _M_off += __off; | |
163 | return *this; | |
164 | } | |
165 | ||
166 | // The standard requires that this operator must be defined, but | |
9fc1117c | 167 | // gives no semantics. In this implementation it just subtracts |
168 | // its argument from the stored offset and returns *this. | |
ed73ad37 | 169 | /// Subtract offset from this position. |
b812c14e | 170 | fpos& |
8832296b | 171 | operator-=(streamoff __off) |
b812c14e | 172 | { |
173 | _M_off -= __off; | |
174 | return *this; | |
175 | } | |
176 | ||
177 | // The standard requires that this operator must be defined, but | |
9fc1117c | 178 | // defines its semantics only in terms of operator-. In this |
b812c14e | 179 | // implementation it constructs a copy of *this, adds the |
180 | // argument to that copy using operator+= and then returns the | |
181 | // copy. | |
ed73ad37 | 182 | /// Add position and offset. |
b812c14e | 183 | fpos |
8832296b | 184 | operator+(streamoff __off) const |
b812c14e | 185 | { |
186 | fpos __pos(*this); | |
187 | __pos += __off; | |
188 | return __pos; | |
189 | } | |
190 | ||
191 | // The standard requires that this operator must be defined, but | |
9fc1117c | 192 | // defines its semantics only in terms of operator+. In this |
b812c14e | 193 | // implementation it constructs a copy of *this, subtracts the |
194 | // argument from that copy using operator-= and then returns the | |
195 | // copy. | |
ed73ad37 | 196 | /// Subtract offset from position. |
b812c14e | 197 | fpos |
8832296b | 198 | operator-(streamoff __off) const |
b812c14e | 199 | { |
200 | fpos __pos(*this); | |
201 | __pos -= __off; | |
202 | return __pos; | |
203 | } | |
204 | ||
205 | // The standard requires that this operator must be defined, but | |
9fc1117c | 206 | // defines its semantics only in terms of operator+. In this |
b812c14e | 207 | // implementation it returns the difference between the offset |
208 | // stored in *this and in the argument. | |
ed73ad37 | 209 | /// Subtract position to return offset. |
b812c14e | 210 | streamoff |
211 | operator-(const fpos& __other) const | |
212 | { return _M_off - __other._M_off; } | |
213 | }; | |
214 | ||
1c1730d9 | 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 | ||
b812c14e | 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> | |
ed73ad37 | 233 | /// File position for char streams. |
b812c14e | 234 | typedef fpos<mbstate_t> streampos; |
ed73ad37 | 235 | /// File position for wchar_t streams. |
b812c14e | 236 | typedef fpos<mbstate_t> wstreampos; |
1069247d | 237 | |
0c8766b1 | 238 | #if __cplusplus >= 201103L |
7a4b9af6 | 239 | /// File position for char16_t streams. |
240 | typedef fpos<mbstate_t> u16streampos; | |
241 | /// File position for char32_t streams. | |
242 | typedef fpos<mbstate_t> u32streampos; | |
243 | #endif | |
244 | ||
2948dd21 | 245 | _GLIBCXX_END_NAMESPACE_VERSION |
246 | } // namespace | |
b812c14e | 247 | |
248 | #endif |