1// Position types -*- C++ -*-
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 2, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// You should have received a copy of the GNU General Public License along
18// with this library; see the file COPYING.  If not, write to the Free
19// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20// USA.
21
22// As a special exception, you may use this file as part of a free software
23// library without restriction.  Specifically, if other files instantiate
24// templates or use macros or inline functions from this file, or you compile
25// this file and link it with other files to produce an executable, this
26// file does not by itself cause the resulting executable to be covered by
27// the GNU General Public License.  This exception does not however
28// invalidate any other reasons why the executable file might be covered by
29// the GNU General Public License.
30
31/** @file postypes.h
32 *  This is an internal header file, included by other library headers.
33 *  You should not attempt to use it directly.
34 */
35
36//
37// ISO C++ 14882: 27.4.1 - Types
38// ISO C++ 14882: 27.4.3 - Template class fpos
39//
40
41#ifndef _GLIBCXX_POSTYPES_H
42#define _GLIBCXX_POSTYPES_H 1
43
44#pragma GCC system_header
45
46#include <cwchar> // For mbstate_t
47
48#ifdef _GLIBCXX_HAVE_STDINT_H
49#include <stdint.h> // For int64_t
50#endif
51
52_GLIBCXX_BEGIN_NAMESPACE(std)
53
54  // The types streamoff, streampos and wstreampos and the class
55  // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
56  // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
57  // behaviour of these types is mostly implementation defined or
58  // unspecified. The behaviour in this implementation is as noted
59  // below.
60
61  /**
62   *  @brief  Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
63   *
64   *  @if maint
65   *  In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
66   *  implementation defined type.
67   *  Note: In versions of GCC up to and including GCC 3.3, streamoff
68   *  was typedef long.
69   *  @endif
70  */
71#ifdef _GLIBCXX_HAVE_INT64_T
72  typedef int64_t       streamoff;
73#else
74  typedef long long     streamoff;
75#endif
76
77  /// Integral type for I/O operation counts and buffer sizes.
78  typedef ptrdiff_t	streamsize; // Signed integral type
79
80  template<typename _StateT>
81    class fpos;
82
83  /**
84   *  @brief  Class representing stream positions.
85   *
86   *  The standard places no requirements upon the template parameter StateT.
87   *  In this implementation StateT must be DefaultConstructible,
88   *  CopyConstructible and Assignable.  The standard only requires that fpos
89   *  should contain a member of type StateT. In this implementation it also
90   *  contains an offset stored as a signed integer.
91   *
92   *  @param  StateT  Type passed to and returned from state().
93   */
94  template<typename _StateT>
95    class fpos
96    {
97    private:
98      streamoff	                _M_off;
99      _StateT			_M_state;
100
101    public:
102      // The standard doesn't require that fpos objects can be default
103      // constructed. This implementation provides a default
104      // constructor that initializes the offset to 0 and default
105      // constructs the state.
106      fpos()
107      : _M_off(0), _M_state() { }
108
109      // The standard requires that fpos objects can be constructed
110      // from streamoff objects using the constructor syntax, and
111      // fails to give any meaningful semantics. In this
112      // implementation implicit conversion is also allowed, and this
113      // constructor stores the streamoff as the offset and default
114      // constructs the state.
115      /// Construct position from offset.
116      fpos(streamoff __off)
117      : _M_off(__off), _M_state() { }
118
119      /// Convert to streamoff.
120      operator streamoff() const { return _M_off; }
121
122      /// Remember the value of @a st.
123#if BUILDING_LIBSTDCXX
124      __attribute__((used))
125#endif
126      void
127      state(_StateT __st)
128      { _M_state = __st; }
129
130      /// Return the last set value of @a st.
131      _StateT
132      state() const
133      { return _M_state; }
134
135      // The standard requires that this operator must be defined, but
136      // gives no semantics. In this implemenation it just adds it's
137      // argument to the stored offset and returns *this.
138      /// Add offset to this position.
139      fpos&
140      operator+=(streamoff __off)
141      {
142	_M_off += __off;
143	return *this;
144      }
145
146      // The standard requires that this operator must be defined, but
147      // gives no semantics. In this implemenation it just subtracts
148      // it's argument from the stored offset and returns *this.
149      /// Subtract offset from this position.
150      fpos&
151      operator-=(streamoff __off)
152      {
153	_M_off -= __off;
154	return *this;
155      }
156
157      // The standard requires that this operator must be defined, but
158      // defines it's semantics only in terms of operator-. In this
159      // implementation it constructs a copy of *this, adds the
160      // argument to that copy using operator+= and then returns the
161      // copy.
162      /// Add position and offset.
163      fpos
164      operator+(streamoff __off) const
165      {
166	fpos __pos(*this);
167	__pos += __off;
168	return __pos;
169      }
170
171      // The standard requires that this operator must be defined, but
172      // defines it's semantics only in terms of operator+. In this
173      // implementation it constructs a copy of *this, subtracts the
174      // argument from that copy using operator-= and then returns the
175      // copy.
176      /// Subtract offset from position.
177      fpos
178      operator-(streamoff __off) const
179      {
180	fpos __pos(*this);
181	__pos -= __off;
182	return __pos;
183      }
184
185      // The standard requires that this operator must be defined, but
186      // defines it's semantics only in terms of operator+. In this
187      // implementation it returns the difference between the offset
188      // stored in *this and in the argument.
189      /// Subtract position to return offset.
190      streamoff
191      operator-(const fpos& __other) const
192      { return _M_off - __other._M_off; }
193    };
194
195  // The standard only requires that operator== must be an
196  // equivalence relation. In this implementation two fpos<StateT>
197  // objects belong to the same equivalence class if the contained
198  // offsets compare equal.
199  /// Test if equivalent to another position.
200  template<typename _StateT>
201    inline bool
202    operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
203    { return streamoff(__lhs) == streamoff(__rhs); }
204
205  template<typename _StateT>
206    inline bool
207    operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
208    { return streamoff(__lhs) != streamoff(__rhs); }
209
210  // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
211  // as implementation defined types, but clause 27.2 requires that
212  // they must both be typedefs for fpos<mbstate_t>
213  /// File position for char streams.
214  typedef fpos<mbstate_t> streampos;
215  /// File position for wchar_t streams.
216  typedef fpos<mbstate_t> wstreampos;
217
218_GLIBCXX_END_NAMESPACE
219
220#endif
221