Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / include / bits / postypes.h
blob2870a8e59a1f118a4dfacdd540fd3df33981744c
1 // Position types -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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,
21 // USA.
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.
32 /** @file postypes.h
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
51 #endif
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 verbiage, the
58 // behaviour of these types is mostly implementation defined or
59 // unspecified. The behaviour in this implementation is as noted
60 // below.
62 /**
63 * @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
68 * was typedef long.
69 */
70 #ifdef _GLIBCXX_HAVE_INT64_T
71 typedef int64_t streamoff;
72 #else
73 typedef long long streamoff;
74 #endif
76 /// Integral type for I/O operation counts and buffer sizes.
77 typedef ptrdiff_t streamsize; // Signed integral type
79 /**
80 * @brief Class representing stream positions.
82 * The standard places no requirements upon the template parameter StateT.
83 * In this implementation StateT must be DefaultConstructible,
84 * CopyConstructible and Assignable. The standard only requires that fpos
85 * should contain a member of type StateT. In this implementation it also
86 * contains an offset stored as a signed integer.
88 * @param StateT Type passed to and returned from state().
90 template<typename _StateT>
91 class fpos
93 private:
94 streamoff _M_off;
95 _StateT _M_state;
97 public:
98 // The standard doesn't require that fpos objects can be default
99 // constructed. This implementation provides a default
100 // constructor that initializes the offset to 0 and default
101 // constructs the state.
102 fpos()
103 : _M_off(0), _M_state() { }
105 // The standard requires that fpos objects can be constructed
106 // from streamoff objects using the constructor syntax, and
107 // fails to give any meaningful semantics. In this
108 // implementation implicit conversion is also allowed, and this
109 // constructor stores the streamoff as the offset and default
110 // constructs the state.
111 /// Construct position from offset.
112 fpos(streamoff __off)
113 : _M_off(__off), _M_state() { }
115 /// Convert to streamoff.
116 operator streamoff() const { return _M_off; }
118 /// Remember the value of @a st.
119 void
120 state(_StateT __st)
121 { _M_state = __st; }
123 /// Return the last set value of @a st.
124 _StateT
125 state() const
126 { return _M_state; }
128 // The standard requires that this operator must be defined, but
129 // gives no semantics. In this implementation it just adds its
130 // argument to the stored offset and returns *this.
131 /// Add offset to this position.
132 fpos&
133 operator+=(streamoff __off)
135 _M_off += __off;
136 return *this;
139 // The standard requires that this operator must be defined, but
140 // gives no semantics. In this implementation it just subtracts
141 // its argument from the stored offset and returns *this.
142 /// Subtract offset from this position.
143 fpos&
144 operator-=(streamoff __off)
146 _M_off -= __off;
147 return *this;
150 // The standard requires that this operator must be defined, but
151 // defines its semantics only in terms of operator-. In this
152 // implementation it constructs a copy of *this, adds the
153 // argument to that copy using operator+= and then returns the
154 // copy.
155 /// Add position and offset.
156 fpos
157 operator+(streamoff __off) const
159 fpos __pos(*this);
160 __pos += __off;
161 return __pos;
164 // The standard requires that this operator must be defined, but
165 // defines its semantics only in terms of operator+. In this
166 // implementation it constructs a copy of *this, subtracts the
167 // argument from that copy using operator-= and then returns the
168 // copy.
169 /// Subtract offset from position.
170 fpos
171 operator-(streamoff __off) const
173 fpos __pos(*this);
174 __pos -= __off;
175 return __pos;
178 // The standard requires that this operator must be defined, but
179 // defines its semantics only in terms of operator+. In this
180 // implementation it returns the difference between the offset
181 // stored in *this and in the argument.
182 /// Subtract position to return offset.
183 streamoff
184 operator-(const fpos& __other) const
185 { return _M_off - __other._M_off; }
188 // The standard only requires that operator== must be an
189 // equivalence relation. In this implementation two fpos<StateT>
190 // objects belong to the same equivalence class if the contained
191 // offsets compare equal.
192 /// Test if equivalent to another position.
193 template<typename _StateT>
194 inline bool
195 operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
196 { return streamoff(__lhs) == streamoff(__rhs); }
198 template<typename _StateT>
199 inline bool
200 operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
201 { return streamoff(__lhs) != streamoff(__rhs); }
203 // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
204 // as implementation defined types, but clause 27.2 requires that
205 // they must both be typedefs for fpos<mbstate_t>
206 /// File position for char streams.
207 typedef fpos<mbstate_t> streampos;
208 /// File position for wchar_t streams.
209 typedef fpos<mbstate_t> wstreampos;
211 _GLIBCXX_END_NAMESPACE
213 #endif