postypes.h revision 169692
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      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 requires that this operator must be defined, but
133      // gives no semantics. In this implemenation it just adds it's
134      // argument to the stored offset and returns *this.
135      /// Add offset to this position.
136      fpos&
137      operator+=(streamoff __off)
138      {
139	_M_off += __off;
140	return *this;
141      }
142
143      // The standard requires that this operator must be defined, but
144      // gives no semantics. In this implemenation it just subtracts
145      // it's argument from the stored offset and returns *this.
146      /// Subtract offset from this position.
147      fpos&
148      operator-=(streamoff __off)
149      {
150	_M_off -= __off;
151	return *this;
152      }
153
154      // The standard requires that this operator must be defined, but
155      // defines it's semantics only in terms of operator-. In this
156      // implementation it constructs a copy of *this, adds the
157      // argument to that copy using operator+= and then returns the
158      // copy.
159      /// Add position and offset.
160      fpos
161      operator+(streamoff __off) const
162      {
163	fpos __pos(*this);
164	__pos += __off;
165	return __pos;
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, subtracts the
171      // argument from that copy using operator-= and then returns the
172      // copy.
173      /// Subtract offset from position.
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 returns the difference between the offset
185      // stored in *this and in the argument.
186      /// Subtract position to return offset.
187      streamoff
188      operator-(const fpos& __other) const
189      { return _M_off - __other._M_off; }
190    };
191
192  // The standard only requires that operator== must be an
193  // equivalence relation. In this implementation two fpos<StateT>
194  // objects belong to the same equivalence class if the contained
195  // offsets compare equal.
196  /// Test if equivalent to another position.
197  template<typename _StateT>
198    inline bool
199    operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
200    { return streamoff(__lhs) == streamoff(__rhs); }
201
202  template<typename _StateT>
203    inline bool
204    operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
205    { return streamoff(__lhs) != streamoff(__rhs); }
206
207  // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
208  // as implementation defined types, but clause 27.2 requires that
209  // they must both be typedefs for fpos<mbstate_t>
210  /// File position for char streams.
211  typedef fpos<mbstate_t> streampos;
212  /// File position for wchar_t streams.
213  typedef fpos<mbstate_t> wstreampos;
214
215_GLIBCXX_END_NAMESPACE
216
217#endif
218