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