# 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, 2006
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 2, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 /** @file postypes.h
    00032  *  This is an internal header file, included by other library headers.
    00033  *  You should not attempt to use it directly.
    00034  */
    00035
    00036 //
    00037 // ISO C++ 14882: 27.4.1 - Types
    00038 // ISO C++ 14882: 27.4.3 - Template class fpos
    00039 //
    00040
    00041 #ifndef _GLIBCXX_POSTYPES_H
    00042 #define _GLIBCXX_POSTYPES_H 1
    00043
    00044 #pragma GCC system_header
    00045
    00046 #include <cwchar> // For mbstate_t
    00047
    00048 #ifdef _GLIBCXX_HAVE_STDINT_H
    00049 #include <stdint.h> // For int64_t
    00050 #endif
    00051
    00052 _GLIBCXX_BEGIN_NAMESPACE(std)
    00053
    00054   // The types streamoff, streampos and wstreampos and the class
    00055   // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
    00056   // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
    00057   // behaviour of these types is mostly implementation defined or
    00058   // unspecified. The behaviour in this implementation is as noted
    00059   // below.
    00060
    00061   /**
    00062    *  @brief  Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
    00063    *
    00064    *  @if maint
    00065    *  In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
    00066    *  implementation defined type.
    00067    *  Note: In versions of GCC up to and including GCC 3.3, streamoff
    00068    *  was typedef long.
    00069    *  @endif
    00070   */
    00071 #ifdef _GLIBCXX_HAVE_INT64_T
    00072   typedef int64_t       streamoff;
    00073 #else
    00074   typedef long long     streamoff;
    00075 #endif
    00076
    00077   /// Integral type for I/O operation counts and buffer sizes.
    00078   typedef ptrdiff_t streamsize; // Signed integral type
    00079
    00080   template<typename _StateT>
    00081     class fpos;
    00082
    00083   /**
    00084    *  @brief  Class representing stream positions.
    00085    *
    00086    *  The standard places no requirements upon the template parameter StateT.
    00087    *  In this implementation StateT must be DefaultConstructible,
    00088    *  CopyConstructible and Assignable.  The standard only requires that fpos
    00089    *  should contain a member of type StateT. In this implementation it also
    00090    *  contains an offset stored as a signed integer.
    00091    *
    00092    *  @param  StateT  Type passed to and returned from state().
    00093    */
    00094   template<typename _StateT>
    00095     class fpos
    00096     {
    00097     private:
    00098       streamoff                 _M_off;
    00099       _StateT           _M_state;
    00100
    00101     public:
    00102       // The standard doesn't require that fpos objects can be default
    00103       // constructed. This implementation provides a default
    00104       // constructor that initializes the offset to 0 and default
    00105       // constructs the state.
    00106       fpos()
    00107       : _M_off(0), _M_state() { }
    00108
    00109       // The standard requires that fpos objects can be constructed
    00110       // from streamoff objects using the constructor syntax, and
    00111       // fails to give any meaningful semantics. In this
    00112       // implementation implicit conversion is also allowed, and this
    00113       // constructor stores the streamoff as the offset and default
    00114       // constructs the state.
    00115       /// Construct position from offset.
    00116       fpos(streamoff __off)
    00117       : _M_off(__off), _M_state() { }
    00118
    00119       /// Convert to streamoff.
    00120       operator streamoff() const { return _M_off; }
    00121
    00122       /// Remember the value of @a st.
    00123       void
    00124       state(_StateT __st)
    00125       { _M_state = __st; }
    00126
    00127       /// Return the last set value of @a st.
    00128       _StateT
    00129       state() const
    00130       { return _M_state; }
    00131
    00132       // The standard requires that this operator must be defined, but
    00133       // gives no semantics. In this implemenation it just adds it's
    00134       // argument to the stored offset and returns *this.
    00135       /// Add offset to this position.
    00136       fpos&
    00137       operator+=(streamoff __off)
    00138       {
    00139     _M_off += __off;
    00140     return *this;
    00141       }
    00142
    00143       // The standard requires that this operator must be defined, but
    00144       // gives no semantics. In this implemenation it just subtracts
    00145       // it's argument from the stored offset and returns *this.
    00146       /// Subtract offset from this position.
    00147       fpos&
    00148       operator-=(streamoff __off)
    00149       {
    00150     _M_off -= __off;
    00151     return *this;
    00152       }
    00153
    00154       // The standard requires that this operator must be defined, but
    00155       // defines it's semantics only in terms of operator-. In this
    00156       // implementation it constructs a copy of *this, adds the
    00157       // argument to that copy using operator+= and then returns the
    00158       // copy.
    00159       /// Add position and offset.
    00160       fpos
    00161       operator+(streamoff __off) const
    00162       {
    00163     fpos __pos(*this);
    00164     __pos += __off;
    00165     return __pos;
    00166       }
    00167
    00168       // The standard requires that this operator must be defined, but
    00169       // defines it's semantics only in terms of operator+. In this
    00170       // implementation it constructs a copy of *this, subtracts the
    00171       // argument from that copy using operator-= and then returns the
    00172       // copy.
    00173       /// Subtract offset from position.
    00174       fpos
    00175       operator-(streamoff __off) const
    00176       {
    00177     fpos __pos(*this);
    00178     __pos -= __off;
    00179     return __pos;
    00180       }
    00181
    00182       // The standard requires that this operator must be defined, but
    00183       // defines it's semantics only in terms of operator+. In this
    00184       // implementation it returns the difference between the offset
    00185       // stored in *this and in the argument.
    00186       /// Subtract position to return offset.
    00187       streamoff
    00188       operator-(const fpos& __other) const
    00189       { return _M_off - __other._M_off; }
    00190     };
    00191
    00192   // The standard only requires that operator== must be an
    00193   // equivalence relation. In this implementation two fpos<StateT>
    00194   // objects belong to the same equivalence class if the contained
    00195   // offsets compare equal.
    00196   /// Test if equivalent to another position.
    00197   template<typename _StateT>
    00198     inline bool
    00199     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
    00200     { return streamoff(__lhs) == streamoff(__rhs); }
    00201
    00202   template<typename _StateT>
    00203     inline bool
    00204     operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
    00205     { return streamoff(__lhs) != streamoff(__rhs); }
    00206
    00207   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
    00208   // as implementation defined types, but clause 27.2 requires that
    00209   // they must both be typedefs for fpos<mbstate_t>
    00210   /// File position for char streams.
    00211   typedef fpos<mbstate_t> streampos;
    00212   /// File position for wchar_t streams.
    00213   typedef fpos<mbstate_t> wstreampos;
    00214
    00215 _GLIBCXX_END_NAMESPACE
    00216
    00217 #endif