postypes.h revision 169692
153537Sbrian// Position types -*- C++ -*-
280728Sbrian
353537Sbrian// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
453537Sbrian// Free Software Foundation, Inc.
553537Sbrian//
653537Sbrian// This file is part of the GNU ISO C++ Library.  This library is free
753537Sbrian// software; you can redistribute it and/or modify it under the
853537Sbrian// terms of the GNU General Public License as published by the
953537Sbrian// Free Software Foundation; either version 2, or (at your option)
1053537Sbrian// any later version.
1153537Sbrian
1253537Sbrian// This library is distributed in the hope that it will be useful,
1353537Sbrian// but WITHOUT ANY WARRANTY; without even the implied warranty of
1453537Sbrian// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1553537Sbrian// GNU General Public License for more details.
1653537Sbrian
1753537Sbrian// You should have received a copy of the GNU General Public License along
1853537Sbrian// with this library; see the file COPYING.  If not, write to the Free
1953537Sbrian// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
2053537Sbrian// USA.
2153537Sbrian
2253537Sbrian// As a special exception, you may use this file as part of a free software
2353537Sbrian// library without restriction.  Specifically, if other files instantiate
2453537Sbrian// templates or use macros or inline functions from this file, or you compile
2553537Sbrian// this file and link it with other files to produce an executable, this
2653537Sbrian// file does not by itself cause the resulting executable to be covered by
2753537Sbrian// the GNU General Public License.  This exception does not however
2853537Sbrian// invalidate any other reasons why the executable file might be covered by
2953537Sbrian// the GNU General Public License.
3053537Sbrian
3153537Sbrian/** @file postypes.h
3253537Sbrian *  This is an internal header file, included by other library headers.
3353537Sbrian *  You should not attempt to use it directly.
3453537Sbrian */
3553537Sbrian
3653537Sbrian//
3753537Sbrian// ISO C++ 14882: 27.4.1 - Types
3853537Sbrian// ISO C++ 14882: 27.4.3 - Template class fpos
3953537Sbrian//
4053537Sbrian
4153537Sbrian#ifndef _GLIBCXX_POSTYPES_H
4253537Sbrian#define _GLIBCXX_POSTYPES_H 1
4353537Sbrian
4453537Sbrian#pragma GCC system_header
4553537Sbrian
4653537Sbrian#include <cwchar> // For mbstate_t
4753537Sbrian
4866602Sbrian#ifdef _GLIBCXX_HAVE_STDINT_H
4953537Sbrian#include <stdint.h> // For int64_t
5053537Sbrian#endif
5153537Sbrian
5253537Sbrian_GLIBCXX_BEGIN_NAMESPACE(std)
5353537Sbrian
5453537Sbrian  // The types streamoff, streampos and wstreampos and the class
5553537Sbrian  // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
5653537Sbrian  // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
5753537Sbrian  // behaviour of these types is mostly implementation defined or
5853537Sbrian  // unspecified. The behaviour in this implementation is as noted
5953537Sbrian  // below.
6053537Sbrian
6153537Sbrian  /**
6253537Sbrian   *  @brief  Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
6353537Sbrian   *
6453537Sbrian   *  @if maint
6553537Sbrian   *  In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
6669582Sbrian   *  implementation defined type.
6769582Sbrian   *  Note: In versions of GCC up to and including GCC 3.3, streamoff
6853537Sbrian   *  was typedef long.
6953537Sbrian   *  @endif
7053537Sbrian  */
7180728Sbrian#ifdef _GLIBCXX_HAVE_INT64_T
7253609Sbrian  typedef int64_t       streamoff;
7353537Sbrian#else
7453537Sbrian  typedef long long     streamoff;
7553537Sbrian#endif
7653537Sbrian
7769582Sbrian  /// Integral type for I/O operation counts and buffer sizes.
7853537Sbrian  typedef ptrdiff_t	streamsize; // Signed integral type
7969582Sbrian
8053537Sbrian  template<typename _StateT>
8153537Sbrian    class fpos;
8253537Sbrian
8353537Sbrian  /**
8453537Sbrian   *  @brief  Class representing stream positions.
8553537Sbrian   *
8653537Sbrian   *  The standard places no requirements upon the template parameter StateT.
8753537Sbrian   *  In this implementation StateT must be DefaultConstructible,
8853537Sbrian   *  CopyConstructible and Assignable.  The standard only requires that fpos
8953537Sbrian   *  should contain a member of type StateT. In this implementation it also
9053537Sbrian   *  contains an offset stored as a signed integer.
9153537Sbrian   *
9253537Sbrian   *  @param  StateT  Type passed to and returned from state().
9353537Sbrian   */
9453537Sbrian  template<typename _StateT>
9553537Sbrian    class fpos
9653537Sbrian    {
9753537Sbrian    private:
9853537Sbrian      streamoff	                _M_off;
9953537Sbrian      _StateT			_M_state;
10053537Sbrian
10153537Sbrian    public:
10253537Sbrian      // The standard doesn't require that fpos objects can be default
10353537Sbrian      // constructed. This implementation provides a default
10453537Sbrian      // constructor that initializes the offset to 0 and default
10553537Sbrian      // constructs the state.
10653537Sbrian      fpos()
10753537Sbrian      : _M_off(0), _M_state() { }
10853537Sbrian
10953537Sbrian      // The standard requires that fpos objects can be constructed
11053537Sbrian      // from streamoff objects using the constructor syntax, and
11153537Sbrian      // fails to give any meaningful semantics. In this
11253537Sbrian      // implementation implicit conversion is also allowed, and this
11353537Sbrian      // constructor stores the streamoff as the offset and default
11453537Sbrian      // constructs the state.
11553537Sbrian      /// Construct position from offset.
11653537Sbrian      fpos(streamoff __off)
11753537Sbrian      : _M_off(__off), _M_state() { }
11853537Sbrian
11953537Sbrian      /// Convert to streamoff.
12053537Sbrian      operator streamoff() const { return _M_off; }
12153537Sbrian
12253537Sbrian      /// Remember the value of @a st.
12353537Sbrian      void
12453537Sbrian      state(_StateT __st)
12553537Sbrian      { _M_state = __st; }
12653537Sbrian
12753537Sbrian      /// Return the last set value of @a st.
12853537Sbrian      _StateT
12953537Sbrian      state() const
13053537Sbrian      { return _M_state; }
13153537Sbrian
13253537Sbrian      // The standard requires that this operator must be defined, but
13353537Sbrian      // gives no semantics. In this implemenation it just adds it's
13453537Sbrian      // argument to the stored offset and returns *this.
13553537Sbrian      /// Add offset to this position.
13653537Sbrian      fpos&
13753537Sbrian      operator+=(streamoff __off)
13853537Sbrian      {
13953537Sbrian	_M_off += __off;
14053537Sbrian	return *this;
14153537Sbrian      }
14253537Sbrian
14353537Sbrian      // The standard requires that this operator must be defined, but
14453537Sbrian      // gives no semantics. In this implemenation it just subtracts
14553537Sbrian      // it's argument from the stored offset and returns *this.
14653537Sbrian      /// Subtract offset from this position.
14753537Sbrian      fpos&
14853537Sbrian      operator-=(streamoff __off)
14953537Sbrian      {
15053537Sbrian	_M_off -= __off;
15153537Sbrian	return *this;
15253537Sbrian      }
15353537Sbrian
15453537Sbrian      // The standard requires that this operator must be defined, but
15553537Sbrian      // defines it's semantics only in terms of operator-. In this
15653537Sbrian      // implementation it constructs a copy of *this, adds the
15753537Sbrian      // argument to that copy using operator+= and then returns the
15853537Sbrian      // copy.
15953537Sbrian      /// Add position and offset.
16053537Sbrian      fpos
16153537Sbrian      operator+(streamoff __off) const
16253537Sbrian      {
16353537Sbrian	fpos __pos(*this);
16453537Sbrian	__pos += __off;
16553537Sbrian	return __pos;
16653537Sbrian      }
16753537Sbrian
16853537Sbrian      // The standard requires that this operator must be defined, but
16953537Sbrian      // defines it's semantics only in terms of operator+. In this
17053537Sbrian      // implementation it constructs a copy of *this, subtracts the
17153537Sbrian      // argument from that copy using operator-= and then returns the
17253537Sbrian      // copy.
17353537Sbrian      /// Subtract offset from position.
17453537Sbrian      fpos
17553537Sbrian      operator-(streamoff __off) const
17653537Sbrian      {
17753537Sbrian	fpos __pos(*this);
17853537Sbrian	__pos -= __off;
17953537Sbrian	return __pos;
18053537Sbrian      }
18153537Sbrian
18253537Sbrian      // The standard requires that this operator must be defined, but
18353537Sbrian      // defines it's semantics only in terms of operator+. In this
18453537Sbrian      // implementation it returns the difference between the offset
18553537Sbrian      // stored in *this and in the argument.
18653537Sbrian      /// Subtract position to return offset.
18753537Sbrian      streamoff
18853537Sbrian      operator-(const fpos& __other) const
18953537Sbrian      { return _M_off - __other._M_off; }
19053537Sbrian    };
19153537Sbrian
19253537Sbrian  // The standard only requires that operator== must be an
19353537Sbrian  // equivalence relation. In this implementation two fpos<StateT>
19453537Sbrian  // objects belong to the same equivalence class if the contained
19553537Sbrian  // offsets compare equal.
19653537Sbrian  /// Test if equivalent to another position.
19753537Sbrian  template<typename _StateT>
19853537Sbrian    inline bool
19953537Sbrian    operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
20053537Sbrian    { return streamoff(__lhs) == streamoff(__rhs); }
20153537Sbrian
20253537Sbrian  template<typename _StateT>
20353537Sbrian    inline bool
20453537Sbrian    operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
20553537Sbrian    { return streamoff(__lhs) != streamoff(__rhs); }
20653537Sbrian
20753537Sbrian  // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
20853537Sbrian  // as implementation defined types, but clause 27.2 requires that
20953537Sbrian  // they must both be typedefs for fpos<mbstate_t>
21053537Sbrian  /// File position for char streams.
21153537Sbrian  typedef fpos<mbstate_t> streampos;
21253537Sbrian  /// File position for wchar_t streams.
21353537Sbrian  typedef fpos<mbstate_t> wstreampos;
21453537Sbrian
21553537Sbrian_GLIBCXX_END_NAMESPACE
21653537Sbrian
21753537Sbrian#endif
21853537Sbrian