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