OSDN Git Service

2001-11-02 Phil Edwards <pme@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / ios_base.h
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 27.8  File-based streams
32 //
33
34 /** @file ios_base.h
35  *  This is an internal header file, included by other library headers.
36  *  You should not attempt to use it directly.
37  */
38
39 #ifndef _CPP_BITS_IOSBASE_H
40 #define _CPP_BITS_IOSBASE_H 1
41
42 #pragma GCC system_header
43
44 namespace std
45 {
46   // The following definitions of bitmask types are enums, not ints,
47   // as permitted (but not required) in the standard, in order to provide
48   // better type safety in iostream calls.  A side effect is that
49   // expressions involving them are no longer compile-time constants.
50   enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
51
52   inline _Ios_Fmtflags 
53   operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
54   { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
55
56   inline _Ios_Fmtflags 
57   operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
58   { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
59
60   inline _Ios_Fmtflags 
61   operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
62   { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
63
64   inline _Ios_Fmtflags 
65   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
66   { return __a = __a | __b; }
67
68   inline _Ios_Fmtflags 
69   operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
70   { return __a = __a & __b; }
71
72   inline _Ios_Fmtflags 
73   operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
74   { return __a = __a ^ __b; }
75
76   inline _Ios_Fmtflags 
77   operator~(_Ios_Fmtflags __a)
78   { return _Ios_Fmtflags(~static_cast<int>(__a)); }
79
80
81   enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
82
83   inline _Ios_Openmode 
84   operator&(_Ios_Openmode __a, _Ios_Openmode __b)
85   { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
86
87   inline _Ios_Openmode 
88   operator|(_Ios_Openmode __a, _Ios_Openmode __b)
89   { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
90
91   inline _Ios_Openmode 
92   operator^(_Ios_Openmode __a, _Ios_Openmode __b)
93   { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
94
95   inline _Ios_Openmode 
96   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
97   { return __a = __a | __b; }
98
99   inline _Ios_Openmode 
100   operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
101   { return __a = __a & __b; }
102
103   inline _Ios_Openmode 
104   operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
105   { return __a = __a ^ __b; }
106
107   inline _Ios_Openmode 
108   operator~(_Ios_Openmode __a)
109   { return _Ios_Openmode(~static_cast<int>(__a)); }
110
111
112   enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
113
114   inline _Ios_Iostate 
115   operator&(_Ios_Iostate __a, _Ios_Iostate __b)
116   { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
117
118   inline _Ios_Iostate 
119   operator|(_Ios_Iostate __a, _Ios_Iostate __b)
120   { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
121
122   inline _Ios_Iostate 
123   operator^(_Ios_Iostate __a, _Ios_Iostate __b)
124   { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
125
126   inline _Ios_Iostate 
127   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
128   { return __a = __a | __b; }
129
130   inline _Ios_Iostate 
131   operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
132   { return __a = __a & __b; }
133
134   inline _Ios_Iostate 
135   operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
136   { return __a = __a ^ __b; }
137
138   inline _Ios_Iostate 
139   operator~(_Ios_Iostate __a)
140   { return _Ios_Iostate(~static_cast<int>(__a)); }
141
142   enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
143
144   // 27.4.2  Class ios_base
145   class ios_base
146   {
147   public:
148     
149     // 27.4.2.1.1  Class ios_base::failure
150     class failure : public exception
151     {
152     public:
153 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
154       //48.  Use of non-existent exception constructor
155       explicit 
156       failure(const string& __str) throw();
157
158       virtual 
159       ~failure() throw();
160
161       virtual const char*
162       what() const throw();
163       
164     private:
165       enum { _M_bufsize = 256 };
166       char _M_name[_M_bufsize];
167 #endif
168     };
169
170     // 27.4.2.1.2  Type ios_base::fmtflags
171     typedef _Ios_Fmtflags fmtflags;
172     // 27.4.2.1.2  Type fmtflags
173     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
174     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
175     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
176     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
177     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
178     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
179     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
180     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
181     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
182     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
183     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
184     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
185     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
186     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
187     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
188     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
189     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
190     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
191
192     // 27.4.2.1.3  Type ios_base::iostate
193     typedef _Ios_Iostate iostate;
194     static const iostate badbit =       iostate(__ios_flags::_S_badbit);
195     static const iostate eofbit =       iostate(__ios_flags::_S_eofbit);
196     static const iostate failbit =      iostate(__ios_flags::_S_failbit);
197     static const iostate goodbit =      iostate(0);
198
199     // 27.4.2.1.4  Type openmode
200     typedef _Ios_Openmode openmode;
201     static const openmode app =         openmode(__ios_flags::_S_app);
202     static const openmode ate =         openmode(__ios_flags::_S_ate);
203     static const openmode binary =      openmode(__ios_flags::_S_bin);
204     static const openmode in =          openmode(__ios_flags::_S_in);
205     static const openmode out =         openmode(__ios_flags::_S_out);
206     static const openmode trunc =       openmode(__ios_flags::_S_trunc);
207
208     // 27.4.2.1.5  Type seekdir
209     typedef _Ios_Seekdir seekdir;
210     static const seekdir beg =          seekdir(0);
211     static const seekdir cur =          seekdir(SEEK_CUR);
212     static const seekdir end =          seekdir(SEEK_END);
213
214 #ifdef _GLIBCPP_DEPRECATED
215     typedef int io_state;
216     typedef int open_mode;
217     typedef int seek_dir;
218 #endif
219
220     // Callbacks;
221     enum event
222     {
223       erase_event,
224       imbue_event,
225       copyfmt_event
226     };
227
228     typedef void (*event_callback) (event, ios_base&, int);
229
230     void 
231     register_callback(event_callback __fn, int __index);
232
233   protected:
234     // Data Members
235     streamsize          _M_precision;
236     streamsize          _M_width;
237     fmtflags            _M_flags;
238
239     // 27.4.2.6  Members for callbacks
240     // 27.4.2.6  ios_base callbacks
241     struct _Callback_list
242     {
243       // Data Members
244       _Callback_list*           _M_next;
245       ios_base::event_callback  _M_fn;
246       int                       _M_index;
247       int                       _M_refcount;  // 0 means one reference.
248     
249       _Callback_list(ios_base::event_callback __fn, int __index, 
250                      _Callback_list* __cb)
251       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
252       
253       void 
254       _M_add_reference() { ++_M_refcount; } // XXX MT
255       
256       int 
257       _M_remove_reference() { return _M_refcount--; }  // 0 => OK to delete
258     };
259
260      _Callback_list*    _M_callbacks;
261
262     void 
263     _M_call_callbacks(event __ev) throw();
264
265     void 
266     _M_dispose_callbacks(void);
267
268     // 27.4.2.5  Members for iword/pword storage
269     struct _Words 
270     { 
271       void*     _M_pword; 
272       long      _M_iword; 
273     };
274
275     static const int    _S_local_words = 8;
276     _Words              _M_word_array[_S_local_words];  // Guaranteed storage
277     _Words              _M_dummy;    // Only for failed iword/pword calls.
278     _Words*             _M_words;
279     int                 _M_word_limit;
280  
281     _Words& 
282     _M_grow_words(int __index);
283
284     // Members for locale and locale caching.
285     locale              _M_ios_locale;
286
287     void 
288     _M_init();
289
290   public:
291
292     // 27.4.2.1.6  Class ios_base::Init
293     // Used to initialize standard streams. In theory, g++ could use
294     // -finit-priority to order this stuff correctly without going
295     // through these machinations. 
296     class Init 
297     {
298       friend class ios_base;
299     public:
300       Init();
301       ~Init();
302       
303       static void
304       _S_ios_create(bool __sync);
305       
306       static void
307       _S_ios_destroy();
308
309     private:
310       static int        _S_ios_base_init;
311       static bool       _S_synced_with_stdio;
312     };
313
314     // Fmtflags state:
315     inline fmtflags 
316     flags() const { return _M_flags; }
317
318     inline fmtflags 
319     flags(fmtflags __fmtfl)
320     { 
321       fmtflags __old = _M_flags; 
322       _M_flags = __fmtfl; 
323       return __old; 
324     }
325
326     inline fmtflags 
327     setf(fmtflags __fmtfl)
328     { 
329       fmtflags __old = _M_flags; 
330       _M_flags |= __fmtfl; 
331       return __old; 
332     }
333
334     inline fmtflags 
335     setf(fmtflags __fmtfl, fmtflags __mask)
336     {
337       fmtflags __old = _M_flags;
338       _M_flags &= ~__mask;
339       _M_flags |= (__fmtfl & __mask);
340       return __old;
341     }
342
343     inline void 
344     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
345
346     inline streamsize 
347     precision() const { return _M_precision; }
348
349     inline streamsize 
350     precision(streamsize __prec)
351     { 
352       streamsize __old = _M_precision; 
353       _M_precision = __prec; 
354       return __old; 
355     }
356
357     inline streamsize 
358     width() const { return _M_width; }
359
360     inline streamsize 
361     width(streamsize __wide)
362     { 
363       streamsize __old = _M_width; 
364       _M_width = __wide; 
365       return __old; 
366     }
367
368     static bool 
369     sync_with_stdio(bool __sync = true);
370
371     // Locales:
372     locale 
373     imbue(const locale& __loc);
374
375     inline locale 
376     getloc() const { return _M_ios_locale; }
377
378     // Storage:
379     static int 
380     xalloc() throw();
381
382     inline long& 
383     iword(int __ix)
384     {
385       _Words& __word = (__ix < _M_word_limit) 
386                         ? _M_words[__ix] : _M_grow_words(__ix);
387       return __word._M_iword;
388     }
389
390     inline void*& 
391     pword(int __ix)
392     {
393       _Words& __word = (__ix < _M_word_limit) 
394                         ? _M_words[__ix] : _M_grow_words(__ix);
395       return __word._M_pword;
396     }
397
398     // Destructor
399     ~ios_base();
400
401   protected:
402     ios_base();
403
404 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
405   //50.  Copy constructor and assignment operator of ios_base
406   private:
407     ios_base(const ios_base&);
408
409     ios_base& 
410     operator=(const ios_base&);
411 #endif
412   };
413  
414   // 27.4.5.1 fmtflags manipulators:
415   inline ios_base& 
416   boolalpha(ios_base& __base)
417   {
418     __base.setf(ios_base::boolalpha);
419     return __base;
420   }
421
422   inline ios_base& 
423   noboolalpha(ios_base& __base)
424   {
425     __base.unsetf(ios_base::boolalpha);
426     return __base;
427   }
428
429   inline ios_base& 
430   showbase(ios_base& __base)
431   {
432     __base.setf(ios_base::showbase);
433     return __base;
434   }
435
436   inline ios_base& 
437   noshowbase(ios_base& __base)
438   {
439     __base.unsetf(ios_base::showbase);
440     return __base;
441   }
442
443   inline ios_base& 
444   showpoint(ios_base& __base)
445   {
446     __base.setf(ios_base::showpoint);
447     return __base;
448   }
449
450   inline ios_base& 
451   noshowpoint(ios_base& __base)
452   {
453     __base.unsetf(ios_base::showpoint);
454     return __base;
455   }
456
457   inline ios_base& 
458   showpos(ios_base& __base)
459   {
460     __base.setf(ios_base::showpos);
461     return __base;
462   }
463
464   inline ios_base& 
465   noshowpos(ios_base& __base)
466   {
467     __base.unsetf(ios_base::showpos);
468     return __base;
469   }
470
471   inline ios_base& 
472   skipws(ios_base& __base)
473   {
474     __base.setf(ios_base::skipws);
475     return __base;
476   }
477   
478   inline ios_base& 
479   noskipws(ios_base& __base)
480   {
481     __base.unsetf(ios_base::skipws);
482     return __base;
483   }
484
485   inline ios_base& 
486   uppercase(ios_base& __base)
487   {
488     __base.setf(ios_base::uppercase);
489     return __base;
490   }
491
492   inline ios_base& 
493   nouppercase(ios_base& __base)
494   {
495     __base.unsetf(ios_base::uppercase);
496     return __base;
497   }
498
499   inline ios_base& 
500   unitbuf(ios_base& __base)
501   {
502      __base.setf(ios_base::unitbuf);      
503      return __base;
504   }
505
506   inline ios_base& 
507   nounitbuf(ios_base& __base)
508   {
509      __base.unsetf(ios_base::unitbuf);
510      return __base;    
511   }
512
513   // 27.4.5.2 adjustfield anipulators:
514   inline ios_base& 
515   internal(ios_base& __base)
516   {
517      __base.setf(ios_base::internal, ios_base::adjustfield);
518      return __base;    
519   }
520
521   inline ios_base& 
522   left(ios_base& __base)
523   {
524     __base.setf(ios_base::left, ios_base::adjustfield);
525     return __base;
526   }
527   
528   inline ios_base& 
529   right(ios_base& __base)
530   {
531     __base.setf(ios_base::right, ios_base::adjustfield);
532     return __base;
533   }
534   
535   // 27.4.5.3 basefield anipulators:
536   inline ios_base& 
537   dec(ios_base& __base)
538   {
539     __base.setf(ios_base::dec, ios_base::basefield);
540     return __base;
541   }
542   
543   inline ios_base& 
544   hex(ios_base& __base)
545   {
546     __base.setf(ios_base::hex, ios_base::basefield);
547     return __base;
548   }
549
550   inline ios_base& 
551   oct(ios_base& __base)
552   {
553     __base.setf(ios_base::oct, ios_base::basefield);
554     return __base;
555   }
556   
557   // 27.4.5.4 floatfield anipulators:
558   inline ios_base& 
559   fixed(ios_base& __base)
560   {
561     __base.setf(ios_base::fixed, ios_base::floatfield);
562     return __base;
563   }
564
565   inline ios_base& 
566   scientific(ios_base& __base)
567   {
568     __base.setf(ios_base::scientific, ios_base::floatfield);
569     return __base;
570   }
571
572 } // namespace std
573
574 #endif /* _CPP_BITS_IOSBASE_H */
575