postypes.h revision 132720
1// Position types -*- C++ -*-
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003, 2004
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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//
32// ISO C++ 14882: 27.4.1 - Types
33// ISO C++ 14882: 27.4.3 - Template class fpos
34//
35
36/** @file postypes.h
37 *  This is an internal header file, included by other library headers.
38 *  You should not attempt to use it directly.
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
52namespace 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      void
124      state(_StateT __st)
125      { _M_state = __st; }
126
127      /// Return the last set value of @a st.
128      _StateT
129      state() const
130      { return _M_state; }
131
132      // The standard only requires that operator== must be an
133      // equivalence relation. In this implementation two fpos<StateT>
134      // objects belong to the same equivalence class if the contained
135      // offsets compare equal.
136      /// Test if equivalent to another position.
137      bool
138      operator==(const fpos& __other) const
139      { return _M_off == __other._M_off; }
140
141      /// Test if not equivalent to another position.
142      bool
143      operator!=(const fpos& __other) const
144      { return _M_off != __other._M_off; }
145
146      // The standard requires that this operator must be defined, but
147      // gives no semantics. In this implemenation it just adds it's
148      // argument to the stored offset and returns *this.
149      /// Add offset to 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      // gives no semantics. In this implemenation it just subtracts
159      // it's argument from the stored offset and returns *this.
160      /// Subtract offset from this position.
161      fpos&
162      operator-=(streamoff __off)
163      {
164	_M_off -= __off;
165	return *this;
166      }
167
168      // The standard requires that this operator must be defined, but
169      // defines it's semantics only in terms of operator-. In this
170      // implementation it constructs a copy of *this, adds the
171      // argument to that copy using operator+= and then returns the
172      // copy.
173      /// Add position and offset.
174      fpos
175      operator+(streamoff __off) const
176      {
177	fpos __pos(*this);
178	__pos += __off;
179	return __pos;
180      }
181
182      // The standard requires that this operator must be defined, but
183      // defines it's semantics only in terms of operator+. In this
184      // implementation it constructs a copy of *this, subtracts the
185      // argument from that copy using operator-= and then returns the
186      // copy.
187      /// Subtract offset from position.
188      fpos
189      operator-(streamoff __off) const
190      {
191	fpos __pos(*this);
192	__pos -= __off;
193	return __pos;
194      }
195
196      // The standard requires that this operator must be defined, but
197      // defines it's semantics only in terms of operator+. In this
198      // implementation it returns the difference between the offset
199      // stored in *this and in the argument.
200      /// Subtract position to return offset.
201      streamoff
202      operator-(const fpos& __other) const
203      { return _M_off - __other._M_off; }
204    };
205
206  // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
207  // as implementation defined types, but clause 27.2 requires that
208  // they must both be typedefs for fpos<mbstate_t>
209  /// File position for char streams.
210  typedef fpos<mbstate_t> streampos;
211  /// File position for wchar_t streams.
212  typedef fpos<mbstate_t> wstreampos;
213} // namespace std
214
215#endif
216