libstdc++
postypes.h
Go to the documentation of this file.
00001 // Position types -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2019 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/postypes.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{iosfwd}
00028  */
00029 
00030 //
00031 // ISO C++ 14882: 27.4.1 - Types
00032 // ISO C++ 14882: 27.4.3 - Template class fpos
00033 //
00034 
00035 #ifndef _GLIBCXX_POSTYPES_H
00036 #define _GLIBCXX_POSTYPES_H 1
00037 
00038 #pragma GCC system_header
00039 
00040 #include <cwchar> // For mbstate_t
00041 
00042 // XXX If <stdint.h> is really needed, make sure to define the macros
00043 // before including it, in order not to break <tr1/cstdint> (and <cstdint>
00044 // in C++11).  Reconsider all this as soon as possible...
00045 #if (defined(_GLIBCXX_HAVE_INT64_T) && !defined(_GLIBCXX_HAVE_INT64_T_LONG) \
00046      && !defined(_GLIBCXX_HAVE_INT64_T_LONG_LONG))
00047 
00048 #ifndef __STDC_LIMIT_MACROS
00049 # define _UNDEF__STDC_LIMIT_MACROS
00050 # define __STDC_LIMIT_MACROS
00051 #endif
00052 #ifndef __STDC_CONSTANT_MACROS
00053 # define _UNDEF__STDC_CONSTANT_MACROS
00054 # define __STDC_CONSTANT_MACROS
00055 #endif
00056 #include <stdint.h> // For int64_t
00057 #ifdef _UNDEF__STDC_LIMIT_MACROS
00058 # undef __STDC_LIMIT_MACROS
00059 # undef _UNDEF__STDC_LIMIT_MACROS
00060 #endif
00061 #ifdef _UNDEF__STDC_CONSTANT_MACROS
00062 # undef __STDC_CONSTANT_MACROS
00063 # undef _UNDEF__STDC_CONSTANT_MACROS
00064 #endif
00065 
00066 #endif
00067 
00068 namespace std _GLIBCXX_VISIBILITY(default)
00069 {
00070 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00071 
00072   // The types streamoff, streampos and wstreampos and the class
00073   // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
00074   // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbiage, the
00075   // behaviour of these types is mostly implementation defined or
00076   // unspecified. The behaviour in this implementation is as noted
00077   // below.
00078 
00079   /**
00080    *  @brief  Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
00081    *
00082    *  In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
00083    *  implementation defined type.
00084    *  Note: In versions of GCC up to and including GCC 3.3, streamoff
00085    *  was typedef long.
00086   */  
00087 #ifdef _GLIBCXX_HAVE_INT64_T_LONG
00088   typedef long          streamoff;
00089 #elif defined(_GLIBCXX_HAVE_INT64_T_LONG_LONG)
00090   typedef long long     streamoff;
00091 #elif defined(_GLIBCXX_HAVE_INT64_T) 
00092   typedef int64_t       streamoff;
00093 #else
00094   typedef long long     streamoff;
00095 #endif
00096 
00097   /// Integral type for I/O operation counts and buffer sizes.
00098   typedef ptrdiff_t     streamsize; // Signed integral type
00099 
00100   /**
00101    *  @brief  Class representing stream positions.
00102    *
00103    *  The standard places no requirements upon the template parameter StateT.
00104    *  In this implementation StateT must be DefaultConstructible,
00105    *  CopyConstructible and Assignable.  The standard only requires that fpos
00106    *  should contain a member of type StateT. In this implementation it also
00107    *  contains an offset stored as a signed integer.
00108    *
00109    *  @param  StateT  Type passed to and returned from state().
00110    */
00111   template<typename _StateT>
00112     class fpos
00113     {
00114     private:
00115       streamoff                 _M_off;
00116       _StateT                   _M_state;
00117 
00118     public:
00119       // The standard doesn't require that fpos objects can be default
00120       // constructed. This implementation provides a default
00121       // constructor that initializes the offset to 0 and default
00122       // constructs the state.
00123       fpos()
00124       : _M_off(0), _M_state() { }
00125 
00126       // The standard requires that fpos objects can be constructed
00127       // from streamoff objects using the constructor syntax, and
00128       // fails to give any meaningful semantics. In this
00129       // implementation implicit conversion is also allowed, and this
00130       // constructor stores the streamoff as the offset and default
00131       // constructs the state.
00132       /// Construct position from offset.
00133       fpos(streamoff __off)
00134       : _M_off(__off), _M_state() { }
00135 
00136 #if __cplusplus >= 201103L
00137       fpos(const fpos&) = default;
00138       fpos& operator=(const fpos&) = default;
00139       ~fpos() = default;
00140 #endif
00141 
00142       /// Convert to streamoff.
00143       operator streamoff() const { return _M_off; }
00144 
00145       /// Remember the value of @a st.
00146       void
00147       state(_StateT __st)
00148       { _M_state = __st; }
00149 
00150       /// Return the last set value of @a st.
00151       _StateT
00152       state() const
00153       { return _M_state; }
00154 
00155       // The standard requires that this operator must be defined, but
00156       // gives no semantics. In this implementation it just adds its
00157       // argument to the stored offset and returns *this.
00158       /// Add offset to this position.
00159       fpos&
00160       operator+=(streamoff __off)
00161       {
00162         _M_off += __off;
00163         return *this;
00164       }
00165 
00166       // The standard requires that this operator must be defined, but
00167       // gives no semantics. In this implementation it just subtracts
00168       // its argument from the stored offset and returns *this.
00169       /// Subtract offset from this position.
00170       fpos&
00171       operator-=(streamoff __off)
00172       {
00173         _M_off -= __off;
00174         return *this;
00175       }
00176 
00177       // The standard requires that this operator must be defined, but
00178       // defines its semantics only in terms of operator-. In this
00179       // implementation it constructs a copy of *this, adds the
00180       // argument to that copy using operator+= and then returns the
00181       // copy.
00182       /// Add position and offset.
00183       fpos
00184       operator+(streamoff __off) const
00185       {
00186         fpos __pos(*this);
00187         __pos += __off;
00188         return __pos;
00189       }
00190 
00191       // The standard requires that this operator must be defined, but
00192       // defines its semantics only in terms of operator+. In this
00193       // implementation it constructs a copy of *this, subtracts the
00194       // argument from that copy using operator-= and then returns the
00195       // copy.
00196       /// Subtract offset from position.
00197       fpos
00198       operator-(streamoff __off) const
00199       {
00200         fpos __pos(*this);
00201         __pos -= __off;
00202         return __pos;
00203       }
00204 
00205       // The standard requires that this operator must be defined, but
00206       // defines its semantics only in terms of operator+. In this
00207       // implementation it returns the difference between the offset
00208       // stored in *this and in the argument.
00209       /// Subtract position to return offset.
00210       streamoff
00211       operator-(const fpos& __other) const
00212       { return _M_off - __other._M_off; }
00213     };
00214 
00215   // The standard only requires that operator== must be an
00216   // equivalence relation. In this implementation two fpos<StateT>
00217   // objects belong to the same equivalence class if the contained
00218   // offsets compare equal.
00219   /// Test if equivalent to another position.
00220   template<typename _StateT>
00221     inline bool
00222     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
00223     { return streamoff(__lhs) == streamoff(__rhs); }
00224 
00225   template<typename _StateT>
00226     inline bool
00227     operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
00228     { return streamoff(__lhs) != streamoff(__rhs); }
00229 
00230   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
00231   // as implementation defined types, but clause 27.2 requires that
00232   // they must both be typedefs for fpos<mbstate_t>
00233   /// File position for char streams.
00234   typedef fpos<mbstate_t> streampos;
00235   /// File position for wchar_t streams.
00236   typedef fpos<mbstate_t> wstreampos;
00237 
00238 #ifdef _GLIBCXX_USE_CHAR8_T
00239   /// File position for char8_t streams.
00240   typedef fpos<mbstate_t> u8streampos;
00241 #endif
00242 
00243 #if __cplusplus >= 201103L
00244   /// File position for char16_t streams.
00245   typedef fpos<mbstate_t> u16streampos;
00246   /// File position for char32_t streams.
00247   typedef fpos<mbstate_t> u32streampos;
00248 #endif
00249 
00250 _GLIBCXX_END_NAMESPACE_VERSION
00251 } // namespace
00252 
00253 #endif