Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

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