OSDN Git Service

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