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