# postypes.h

Go to the documentation of this file.

    00001 // Position types -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
    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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    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 //
    00032 // ISO C++ 14882: 27.4.1 - Types
    00033 // ISO C++ 14882: 27.4.3 - Template class fpos
    00034 //
    00035
    00036 /** @file postypes.h
    00037  *  This is an internal header file, included by other library headers.
    00038  *  You should not attempt to use it directly.
    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 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 only requires that operator== must be an
    00133       // equivalence relation. In this implementation two fpos<StateT>
    00134       // objects belong to the same equivalence class if the contained
    00135       // offsets compare equal.
    00136       /// Test if equivalent to another position.
    00137       bool
    00138       operator==(const fpos& __other) const
    00139       { return _M_off == __other._M_off; }
    00140
    00141       /// Test if not equivalent to another position.
    00142       bool
    00143       operator!=(const fpos& __other) const
    00144       { return _M_off != __other._M_off; }
    00145
    00146       // The standard requires that this operator must be defined, but
    00147       // gives no semantics. In this implemenation it just adds it's
    00148       // argument to the stored offset and returns *this.
    00149       /// Add offset to this position.
    00150       fpos&
    00151       operator+=(streamoff __off)
    00152       {
    00153     _M_off += __off;
    00154     return *this;
    00155       }
    00156
    00157       // The standard requires that this operator must be defined, but
    00158       // gives no semantics. In this implemenation it just subtracts
    00159       // it's argument from the stored offset and returns *this.
    00160       /// Subtract offset from this position.
    00161       fpos&
    00162       operator-=(streamoff __off)
    00163       {
    00164     _M_off -= __off;
    00165     return *this;
    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, adds the
    00171       // argument to that copy using operator+= and then returns the
    00172       // copy.
    00173       /// Add position and offset.
    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 constructs a copy of *this, subtracts the
    00185       // argument from that copy using operator-= and then returns the
    00186       // copy.
    00187       /// Subtract offset from position.
    00188       fpos
    00189       operator-(streamoff __off) const
    00190       {
    00191     fpos __pos(*this);
    00192     __pos -= __off;
    00193     return __pos;
    00194       }
    00195
    00196       // The standard requires that this operator must be defined, but
    00197       // defines it's semantics only in terms of operator+. In this
    00198       // implementation it returns the difference between the offset
    00199       // stored in *this and in the argument.
    00200       /// Subtract position to return offset.
    00201       streamoff
    00202       operator-(const fpos& __other) const
    00203       { return _M_off - __other._M_off; }
    00204     };
    00205
    00206   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
    00207   // as implementation defined types, but clause 27.2 requires that
    00208   // they must both be typedefs for fpos<mbstate_t>
    00209   /// File position for char streams.
    00210   typedef fpos<mbstate_t> streampos;
    00211   /// File position for wchar_t streams.
    00212   typedef fpos<mbstate_t> wstreampos;
    00213 } // namespace std
    00214
    00215 #endif