]>
Commit | Line | Data |
---|---|---|
4c4809c1 BK |
1 | // Position types -*- C++ -*- |
2 | ||
3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003 | |
4 | // Free Software Foundation, Inc. | |
5 | // | |
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) | |
10 | // any later version. | |
11 | ||
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. | |
16 | ||
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, | |
20 | // USA. | |
21 | ||
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. | |
30 | ||
31 | // | |
32 | // ISO C++ 14882: 27.4.1 - Types | |
33 | // ISO C++ 14882: 27.4.3 - Template class fpos | |
34 | // | |
35 | ||
36 | /** @file postypes.h | |
37 | * This is an internal header file, included by other library headers. | |
38 | * You should not attempt to use it directly. | |
39 | */ | |
40 | ||
41 | #ifndef _GLIBCXX_POSTYPES_H | |
42 | #define _GLIBCXX_POSTYPES_H 1 | |
43 | ||
44 | #pragma GCC system_header | |
45 | ||
46 | #include <cwchar> // For mbstate_t | |
47 | ||
3d05b345 PC |
48 | #ifdef _GLIBCXX_HAVE_STDINT_H |
49 | #include <stdint.h> // For int64_t | |
50 | #endif | |
51 | ||
4c4809c1 BK |
52 | namespace std |
53 | { | |
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 | |
3d05b345 PC |
59 | // below. |
60 | ||
61 | #ifdef _GLIBCXX_HAVE_INT64_T | |
62 | typedef int64_t __streamoff_base_type; | |
63 | #else | |
64 | typedef long long __streamoff_base_type; | |
65 | #endif | |
66 | ||
4c4809c1 BK |
67 | typedef ptrdiff_t streamsize; // Signed integral type |
68 | ||
69 | template<typename _StateT> | |
70 | class fpos; | |
71 | ||
72 | // Class streamoff is an implementation defined type that meets the | |
73 | // requirements for streamoff. It stores an offset as a signed | |
74 | // integer. Note: this class is an implementation detail. | |
75 | class streamoff | |
76 | { | |
77 | private: | |
78 | __streamoff_base_type _M_off; | |
79 | ||
80 | public: | |
81 | // Nothing in the standard requires that streamoff can be default | |
82 | // constructed. In this implementation a default constructor that | |
83 | // stores the value 0 is provided. | |
84 | streamoff() | |
85 | : _M_off(0) { } | |
86 | ||
87 | // The standard only requires that streamoff can be constructed | |
88 | // from streamsize using the constructor syntax. This | |
89 | // implementation also allows implicit conversion from integer | |
90 | // types to streamoff. | |
91 | streamoff(__streamoff_base_type __off) | |
92 | : _M_off(__off) { } | |
93 | ||
94 | // The standard requires that streamoff can be constructed from | |
95 | // instances of fpos using the constructor syntax, but gives no | |
96 | // semantics for this construction. In this implementation it | |
97 | // extracts the offset stored by the fpos object. | |
98 | // Note: In versions of GCC up to and including GCC 3.3, implicit | |
99 | // conversion from fpos to streamoff was allowed. This constructor | |
100 | // has now been made explicit to improve type safety. | |
101 | template<typename _StateT> | |
102 | explicit | |
103 | streamoff(const fpos<_StateT>&); | |
104 | ||
105 | // The standard requires that streamsize can be constructed from | |
106 | // streamoff using the constructor syntax. This implementation | |
107 | // also allows implicit conversion. This allows streamoff objects | |
108 | // to be used in arithmetic expressions and to be compared against | |
109 | // each other and integer types. | |
110 | operator __streamoff_base_type() const | |
111 | { return _M_off; } | |
112 | ||
113 | // This implementation allows the use of operators +=, -=, ++ and | |
114 | // -- on streamoff objects. | |
115 | streamoff& | |
116 | operator+=(__streamoff_base_type __off) | |
117 | { | |
118 | _M_off += __off; | |
119 | return *this; | |
120 | } | |
121 | ||
122 | streamoff& | |
123 | operator-=(__streamoff_base_type __off) | |
124 | { | |
125 | _M_off -= __off; | |
126 | return *this; | |
127 | } | |
128 | }; | |
129 | ||
130 | // In clauses 21.1.3.1 and 27.4.1 streamoff is described as an | |
131 | // implementation defined type. In this implementation it is a | |
132 | // distinct class type. | |
133 | // Note: In versions of GCC up to and including GCC 3.3, streamoff | |
134 | // was typedef long. | |
135 | typedef class streamoff streamoff; | |
136 | ||
137 | // The standard fails to place any requiremens on the template | |
138 | // argument StateT. In this implementation StateT must be | |
139 | // DefaultConstructible, CopyConstructible and Assignable. The | |
140 | // standard only requires that fpos should contain a member of type | |
141 | // StateT. In this implementation it also contains an offset stored | |
142 | // as a signed integer. | |
143 | template<typename _StateT> | |
144 | class fpos | |
145 | { | |
146 | private: | |
147 | friend class streamoff; | |
148 | ||
42134429 BK |
149 | __streamoff_base_type _M_off; |
150 | _StateT _M_state; | |
4c4809c1 BK |
151 | |
152 | public: | |
153 | // The standard doesn't require that fpos objects can be default | |
154 | // constructed. This implementation provides a default | |
155 | // constructor that initializes the offset to 0 and default | |
156 | // constructs the state. | |
157 | fpos() | |
158 | : _M_off(0), _M_state() { } | |
159 | ||
160 | // The standard requires implicit conversion from integers to | |
161 | // fpos, but gives no meaningful semantics for this | |
162 | // conversion. In this implementation this constructor stores | |
163 | // the integer as the offset and default constructs the state. | |
164 | fpos(__streamoff_base_type __off) | |
165 | : _M_off(__off), _M_state() { } | |
166 | ||
167 | // The standard requires that fpos objects can be constructed | |
168 | // from streamoff objects using the constructor syntax, and | |
169 | // fails to give any meaningful semantics. In this | |
170 | // implementation implicit conversion is also allowed, and this | |
171 | // constructor stores the streamoff as the offset and default | |
172 | // constructs the state. | |
173 | fpos(const streamoff& __off) | |
174 | : _M_off(__off), _M_state() { } | |
175 | ||
176 | void | |
177 | state(_StateT __st) | |
178 | { _M_state = __st; } | |
179 | ||
180 | _StateT | |
181 | state() const | |
182 | { return _M_state; } | |
183 | ||
184 | // The standard only requires that operator== must be an | |
185 | // equivalence relation. In this implementation two fpos<StateT> | |
186 | // objects belong to the same equivalence class if the contained | |
187 | // offsets compare equal. | |
188 | bool | |
189 | operator==(const fpos& __other) const | |
190 | { return _M_off == __other._M_off; } | |
191 | ||
192 | bool | |
193 | operator!=(const fpos& __other) const | |
194 | { return _M_off != __other._M_off; } | |
195 | ||
196 | // The standard requires that this operator must be defined, but | |
197 | // gives no semantics. In this implemenation it just adds it's | |
198 | // argument to the stored offset and returns *this. | |
199 | fpos& | |
200 | operator+=(const streamoff& __off) | |
201 | { | |
202 | _M_off += __off; | |
203 | return *this; | |
204 | } | |
205 | ||
206 | // The standard requires that this operator must be defined, but | |
207 | // gives no semantics. In this implemenation it just subtracts | |
208 | // it's argument from the stored offset and returns *this. | |
209 | fpos& | |
210 | operator-=(const streamoff& __off) | |
211 | { | |
212 | _M_off -= __off; | |
213 | return *this; | |
214 | } | |
215 | ||
216 | // The standard requires that this operator must be defined, but | |
217 | // defines it's semantics only in terms of operator-. In this | |
218 | // implementation it constructs a copy of *this, adds the | |
219 | // argument to that copy using operator+= and then returns the | |
220 | // copy. | |
221 | fpos | |
222 | operator+(const streamoff& __off) const | |
223 | { | |
224 | fpos __pos(*this); | |
225 | __pos += __off; | |
226 | return __pos; | |
227 | } | |
228 | ||
229 | // The standard requires that this operator must be defined, but | |
230 | // defines it's semantics only in terms of operator+. In this | |
231 | // implementation it constructs a copy of *this, subtracts the | |
232 | // argument from that copy using operator-= and then returns the | |
233 | // copy. | |
234 | fpos | |
235 | operator-(const streamoff& __off) const | |
236 | { | |
237 | fpos __pos(*this); | |
238 | __pos -= __off; | |
239 | return __pos; | |
240 | } | |
241 | ||
242 | // The standard requires that this operator must be defined, but | |
243 | // defines it's semantics only in terms of operator+. In this | |
244 | // implementation it returns the difference between the offset | |
245 | // stored in *this and in the argument. | |
246 | streamoff | |
247 | operator-(const fpos& __other) const | |
248 | { return _M_off - __other._M_off; } | |
249 | }; | |
250 | ||
251 | template<typename _StateT> | |
252 | inline | |
253 | streamoff::streamoff(const fpos<_StateT>& __pos) | |
254 | : _M_off(__pos._M_off) { } | |
255 | ||
256 | // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos | |
257 | // as implementation defined types, but clause 27.2 requires that | |
258 | // they must both be typedefs for fpos<mbstate_t> | |
259 | typedef fpos<mbstate_t> streampos; | |
260 | typedef fpos<mbstate_t> wstreampos; | |
261 | } // namespace std | |
262 | ||
263 | #endif |