OSDN Git Service

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