OSDN Git Service

2008-07-05 Paolo Carlini <paolo.carlini@oracle.com>
[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, 2008
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 _GLIBCXX_BEGIN_NAMESPACE(std)
50
51   // The types streamoff, streampos and wstreampos and the class
52   // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
53   // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbiage, the
54   // behaviour of these types is mostly implementation defined or
55   // unspecified. The behaviour in this implementation is as noted
56   // below.
57
58   /**
59    *  @brief  Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
60    *
61    *  In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
62    *  implementation defined type.
63    *  Note: In versions of GCC up to and including GCC 3.3, streamoff
64    *  was typedef long.
65   */  
66 #ifdef _GLIBCXX_HAVE_INT64_T
67 # if (__CHAR_BIT__ * __SIZEOF_LONG__ == 64)
68   typedef long          streamoff;
69 # else
70   typedef long long     streamoff;
71 # endif
72 #else
73   typedef long long     streamoff;
74 #endif
75
76   /// Integral type for I/O operation counts and buffer sizes.
77   typedef ptrdiff_t     streamsize; // Signed integral type
78
79   /**
80    *  @brief  Class representing stream positions.
81    *
82    *  The standard places no requirements upon the template parameter StateT.
83    *  In this implementation StateT must be DefaultConstructible,
84    *  CopyConstructible and Assignable.  The standard only requires that fpos
85    *  should contain a member of type StateT. In this implementation it also
86    *  contains an offset stored as a signed integer.
87    *
88    *  @param  StateT  Type passed to and returned from state().
89    */
90   template<typename _StateT>
91     class fpos
92     {
93     private:
94       streamoff                 _M_off;
95       _StateT                   _M_state;
96
97     public:
98       // The standard doesn't require that fpos objects can be default
99       // constructed. This implementation provides a default
100       // constructor that initializes the offset to 0 and default
101       // constructs the state.
102       fpos()
103       : _M_off(0), _M_state() { }
104
105       // The standard requires that fpos objects can be constructed
106       // from streamoff objects using the constructor syntax, and
107       // fails to give any meaningful semantics. In this
108       // implementation implicit conversion is also allowed, and this
109       // constructor stores the streamoff as the offset and default
110       // constructs the state.
111       /// Construct position from offset.
112       fpos(streamoff __off)
113       : _M_off(__off), _M_state() { }
114
115       /// Convert to streamoff.
116       operator streamoff() const { return _M_off; }
117
118       /// Remember the value of @a st.
119       void
120       state(_StateT __st)
121       { _M_state = __st; }
122
123       /// Return the last set value of @a st.
124       _StateT
125       state() const
126       { return _M_state; }
127
128       // The standard requires that this operator must be defined, but
129       // gives no semantics. In this implementation it just adds its
130       // argument to the stored offset and returns *this.
131       /// Add offset to this position.
132       fpos&
133       operator+=(streamoff __off)
134       {
135         _M_off += __off;
136         return *this;
137       }
138
139       // The standard requires that this operator must be defined, but
140       // gives no semantics. In this implementation it just subtracts
141       // its argument from the stored offset and returns *this.
142       /// Subtract offset from this position.
143       fpos&
144       operator-=(streamoff __off)
145       {
146         _M_off -= __off;
147         return *this;
148       }
149
150       // The standard requires that this operator must be defined, but
151       // defines its semantics only in terms of operator-. In this
152       // implementation it constructs a copy of *this, adds the
153       // argument to that copy using operator+= and then returns the
154       // copy.
155       /// Add position and offset.
156       fpos
157       operator+(streamoff __off) const
158       {
159         fpos __pos(*this);
160         __pos += __off;
161         return __pos;
162       }
163
164       // The standard requires that this operator must be defined, but
165       // defines its semantics only in terms of operator+. In this
166       // implementation it constructs a copy of *this, subtracts the
167       // argument from that copy using operator-= and then returns the
168       // copy.
169       /// Subtract offset from position.
170       fpos
171       operator-(streamoff __off) const
172       {
173         fpos __pos(*this);
174         __pos -= __off;
175         return __pos;
176       }
177
178       // The standard requires that this operator must be defined, but
179       // defines its semantics only in terms of operator+. In this
180       // implementation it returns the difference between the offset
181       // stored in *this and in the argument.
182       /// Subtract position to return offset.
183       streamoff
184       operator-(const fpos& __other) const
185       { return _M_off - __other._M_off; }
186     };
187
188   // The standard only requires that operator== must be an
189   // equivalence relation. In this implementation two fpos<StateT>
190   // objects belong to the same equivalence class if the contained
191   // offsets compare equal.
192   /// Test if equivalent to another position.
193   template<typename _StateT>
194     inline bool
195     operator==(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
196     { return streamoff(__lhs) == streamoff(__rhs); }
197
198   template<typename _StateT>
199     inline bool
200     operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
201     { return streamoff(__lhs) != streamoff(__rhs); }
202
203   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
204   // as implementation defined types, but clause 27.2 requires that
205   // they must both be typedefs for fpos<mbstate_t>
206   /// File position for char streams.
207   typedef fpos<mbstate_t> streampos;
208   /// File position for wchar_t streams.
209   typedef fpos<mbstate_t> wstreampos;
210
211 _GLIBCXX_END_NAMESPACE
212
213 #endif