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
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 postypes.h
00028  *  This is an internal header file, included by other library headers.
00029  *  You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
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       /// Convert to streamoff.
00137       operator streamoff() const { return _M_off; }
00138 
00139       /// Remember the value of @a st.
00140       void
00141       state(_StateT __st)
00142       { _M_state = __st; }
00143 
00144       /// Return the last set value of @a st.
00145       _StateT
00146       state() const
00147       { return _M_state; }
00148 
00149       // The standard requires that this operator must be defined, but
00150       // gives no semantics. In this implementation it just adds its
00151       // argument to the stored offset and returns *this.
00152       /// Add offset to this position.
00153       fpos&
00154       operator+=(streamoff __off)
00155       {
00156     _M_off += __off;
00157     return *this;
00158       }
00159 
00160       // The standard requires that this operator must be defined, but
00161       // gives no semantics. In this implementation it just subtracts
00162       // its argument from the stored offset and returns *this.
00163       /// Subtract offset from this position.
00164       fpos&
00165       operator-=(streamoff __off)
00166       {
00167     _M_off -= __off;
00168     return *this;
00169       }
00170 
00171       // The standard requires that this operator must be defined, but
00172       // defines its semantics only in terms of operator-. In this
00173       // implementation it constructs a copy of *this, adds the
00174       // argument to that copy using operator+= and then returns the
00175       // copy.
00176       /// Add position and offset.
00177       fpos
00178       operator+(streamoff __off) const
00179       {
00180     fpos __pos(*this);
00181     __pos += __off;
00182     return __pos;
00183       }
00184 
00185       // The standard requires that this operator must be defined, but
00186       // defines its semantics only in terms of operator+. In this
00187       // implementation it constructs a copy of *this, subtracts the
00188       // argument from that copy using operator-= and then returns the
00189       // copy.
00190       /// Subtract offset from position.
00191       fpos
00192       operator-(streamoff __off) const
00193       {
00194     fpos __pos(*this);
00195     __pos -= __off;
00196     return __pos;
00197       }
00198 
00199       // The standard requires that this operator must be defined, but
00200       // defines its semantics only in terms of operator+. In this
00201       // implementation it returns the difference between the offset
00202       // stored in *this and in the argument.
00203       /// Subtract position to return offset.
00204       streamoff
00205       operator-(const fpos& __other) const
00206       { return _M_off - __other._M_off; }
00207     };
00208 
00209   // The standard only requires that operator== must be an
00210   // equivalence relation. In this implementation two fpos<StateT>
00211   // objects belong to the same equivalence class if the contained
00212   // offsets compare equal.
00213   /// Test if equivalent to another position.
00214   template<typename _StateT>
00215     inline bool
00216     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
00217     { return streamoff(__lhs) == streamoff(__rhs); }
00218 
00219   template<typename _StateT>
00220     inline bool
00221     operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
00222     { return streamoff(__lhs) != streamoff(__rhs); }
00223 
00224   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
00225   // as implementation defined types, but clause 27.2 requires that
00226   // they must both be typedefs for fpos<mbstate_t>
00227   /// File position for char streams.
00228   typedef fpos<mbstate_t> streampos;
00229   /// File position for wchar_t streams.
00230   typedef fpos<mbstate_t> wstreampos;
00231 
00232 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00233   /// File position for char16_t streams.
00234   typedef fpos<mbstate_t> u16streampos;
00235   /// File position for char32_t streams.
00236   typedef fpos<mbstate_t> u32streampos;
00237 #endif
00238 
00239 _GLIBCXX_END_NAMESPACE
00240 
00241 #endif

Generated on Thu Jul 23 21:16:12 2009 for libstdc++ by  doxygen 1.5.8