OSDN Git Service

2003-10-16 Petur Runolfsson <peturr02@ru.is>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / postypes.h
1 // Position types -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2003 
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 namespace std
49 {
50   // The types streamoff, streampos and wstreampos and the class
51   // template fpos<> are described in clauses 21.1.2, 21.1.3, 27.1.2,
52   // 27.2, 27.4.1, 27.4.3 and D.6. Despite all this verbage, the
53   // behaviour of these types is mostly implementation defined or
54   // unspecified. The behaviour in this implementation is as noted
55   // below.  
56   typedef long          __streamoff_base_type;
57   typedef ptrdiff_t     streamsize; // Signed integral type
58
59   template<typename _StateT>
60     class fpos;
61
62   // Class streamoff is an implementation defined type that meets the
63   // requirements for streamoff. It stores an offset as a signed
64   // integer.  Note: this class is an implementation detail.
65   class streamoff
66   {
67   private:
68     __streamoff_base_type _M_off;
69
70   public:
71     // Nothing in the standard requires that streamoff can be default
72     // constructed. In this implementation a default constructor that
73     // stores the value 0 is provided.
74     streamoff()
75     : _M_off(0) { }
76
77     // The standard only requires that streamoff can be constructed
78     // from streamsize using the constructor syntax. This
79     // implementation also allows implicit conversion from integer
80     // types to streamoff.
81     streamoff(__streamoff_base_type __off)
82     : _M_off(__off) { }
83
84     // The standard requires that streamoff can be constructed from
85     // instances of fpos using the constructor syntax, but gives no
86     // semantics for this construction. In this implementation it
87     // extracts the offset stored by the fpos object.
88     // Note: In versions of GCC up to and including GCC 3.3, implicit
89     // conversion from fpos to streamoff was allowed. This constructor
90     // has now been made explicit to improve type safety.
91     template<typename _StateT>
92       explicit
93       streamoff(const fpos<_StateT>&);
94
95     // The standard requires that streamsize can be constructed from
96     // streamoff using the constructor syntax. This implementation
97     // also allows implicit conversion. This allows streamoff objects
98     // to be used in arithmetic expressions and to be compared against
99     // each other and integer types.
100     operator __streamoff_base_type() const
101     { return _M_off; }
102
103     // This implementation allows the use of operators +=, -=, ++ and
104     // -- on streamoff objects.
105     streamoff&
106     operator+=(__streamoff_base_type __off)
107     {
108       _M_off += __off;
109       return *this;
110     }
111
112     streamoff&
113     operator-=(__streamoff_base_type __off)
114     {
115       _M_off -= __off;
116       return *this;
117     }
118   };
119
120   // In clauses 21.1.3.1 and 27.4.1 streamoff is described as an
121   // implementation defined type. In this implementation it is a
122   // distinct class type.
123   // Note: In versions of GCC up to and including GCC 3.3, streamoff
124   // was typedef long.
125   typedef class streamoff streamoff;
126
127   // The standard fails to place any requiremens on the template
128   // argument StateT. In this implementation StateT must be
129   // DefaultConstructible, CopyConstructible and Assignable.  The
130   // standard only requires that fpos should contain a member of type
131   // StateT. In this implementation it also contains an offset stored
132   // as a signed integer.
133   template<typename _StateT>
134     class fpos
135     {
136     private:
137       friend class streamoff;
138
139       __streamoff_base_type _M_off;
140       _StateT _M_state;
141
142     public:
143       // The standard doesn't require that fpos objects can be default
144       // constructed. This implementation provides a default
145       // constructor that initializes the offset to 0 and default
146       // constructs the state.
147       fpos()
148       : _M_off(0), _M_state() { }
149
150       // The standard requires implicit conversion from integers to
151       // fpos, but gives no meaningful semantics for this
152       // conversion. In this implementation this constructor stores
153       // the integer as the offset and default constructs the state.
154       fpos(__streamoff_base_type __off)
155       : _M_off(__off), _M_state() { }
156
157       // The standard requires that fpos objects can be constructed
158       // from streamoff objects using the constructor syntax, and
159       // fails to give any meaningful semantics. In this
160       // implementation implicit conversion is also allowed, and this
161       // constructor stores the streamoff as the offset and default
162       // constructs the state.
163       fpos(const streamoff& __off)
164       : _M_off(__off), _M_state() { }
165
166       void
167       state(_StateT __st)
168       { _M_state = __st; }
169
170       _StateT
171       state() const
172       { return _M_state; }
173
174       // The standard only requires that operator== must be an
175       // equivalence relation. In this implementation two fpos<StateT>
176       // objects belong to the same equivalence class if the contained
177       // offsets compare equal.
178       bool
179       operator==(const fpos& __other) const
180       { return _M_off == __other._M_off; }
181
182       bool
183       operator!=(const fpos& __other) const
184       { return _M_off != __other._M_off; }
185
186       // The standard requires that this operator must be defined, but
187       // gives no semantics. In this implemenation it just adds it's
188       // argument to the stored offset and returns *this.
189       fpos&
190       operator+=(const streamoff& __off)
191       {
192         _M_off += __off;
193         return *this;
194       }
195
196       // The standard requires that this operator must be defined, but
197       // gives no semantics. In this implemenation it just subtracts
198       // it's argument from the stored offset and returns *this.
199       fpos&
200       operator-=(const streamoff& __off)
201       {
202         _M_off -= __off;
203         return *this;
204       }
205
206       // The standard requires that this operator must be defined, but
207       // defines it's semantics only in terms of operator-. In this
208       // implementation it constructs a copy of *this, adds the
209       // argument to that copy using operator+= and then returns the
210       // copy.
211       fpos
212       operator+(const streamoff& __off) const
213       {
214         fpos __pos(*this);
215         __pos += __off;
216         return __pos;
217       }
218
219       // The standard requires that this operator must be defined, but
220       // defines it's semantics only in terms of operator+. In this
221       // implementation it constructs a copy of *this, subtracts the
222       // argument from that copy using operator-= and then returns the
223       // copy.
224       fpos
225       operator-(const streamoff& __off) const
226       {
227         fpos __pos(*this);
228         __pos -= __off;
229         return __pos;
230       }
231
232       // The standard requires that this operator must be defined, but
233       // defines it's semantics only in terms of operator+. In this
234       // implementation it returns the difference between the offset
235       // stored in *this and in the argument.
236       streamoff
237       operator-(const fpos& __other) const
238       { return _M_off - __other._M_off; }
239     };
240
241   template<typename _StateT>
242     inline
243     streamoff::streamoff(const fpos<_StateT>& __pos)
244     : _M_off(__pos._M_off) { }
245
246   // Clauses 21.1.3.1 and 21.1.3.2 describe streampos and wstreampos
247   // as implementation defined types, but clause 27.2 requires that
248   // they must both be typedefs for fpos<mbstate_t>
249   typedef fpos<mbstate_t> streampos;
250   typedef fpos<mbstate_t> wstreampos;
251 } // namespace std
252
253 #endif