OSDN Git Service

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