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