1 /*
2 * Copyright (c) 1999
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Copyright (c) 1999
6 * Boris Fomitchev
7 *
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
10 *
11 * Permission to use or copy this software for any purpose is hereby granted
12 * without fee, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
16 *
17 */
18
19 #ifndef _STLP_INTERNAL_IOMANIP
20 #define _STLP_INTERNAL_IOMANIP
21
22 #ifndef _STLP_INTERNAL_ISTREAM
23 # include <stl/_istream.h> // Includes <ostream> and <ios>
24 #endif
25
26 _STLP_BEGIN_NAMESPACE
27
28 _STLP_MOVE_TO_PRIV_NAMESPACE
29
30 //----------------------------------------------------------------------
31 // Machinery for defining manipulators.
32
33 // Class that calls one of ios_base's single-argument member functions.
34 template <class _Arg>
35 struct _Ios_Manip_1 {
36 typedef _Arg (ios_base::*__f_ptr_type)(_Arg);
37
_Ios_Manip_1_Ios_Manip_138 _Ios_Manip_1(__f_ptr_type __f, const _Arg& __arg)
39 : _M_f(__f), _M_arg(__arg) {}
40
operator_Ios_Manip_141 void operator()(ios_base& __ios) const
42 { (__ios.*_M_f)(_M_arg); }
43
44 __f_ptr_type _M_f;
45 _Arg _M_arg;
46 };
47
48 // Class that calls one of ios_base's two-argument member functions.
49 struct _Ios_Setf_Manip {
50 ios_base::fmtflags _M_flag;
51 ios_base::fmtflags _M_mask;
52 bool _M_two_args;
53
_Ios_Setf_Manip_Ios_Setf_Manip54 _Ios_Setf_Manip(ios_base::fmtflags __f)
55 : _M_flag(__f), _M_mask(0), _M_two_args(false) {}
56
_Ios_Setf_Manip_Ios_Setf_Manip57 _Ios_Setf_Manip(ios_base::fmtflags __f, ios_base::fmtflags __m)
58 : _M_flag(__f), _M_mask(__m), _M_two_args(true) {}
59
operator_Ios_Setf_Manip60 void operator()(ios_base& __ios) const {
61 if (_M_two_args)
62 __ios.setf(_M_flag, _M_mask);
63 else
64 __ios.setf(_M_flag);
65 }
66 };
67
68 _STLP_MOVE_TO_STD_NAMESPACE
69
70 template <class _CharT, class _Traits, class _Arg>
71 inline basic_istream<_CharT, _Traits>& _STLP_CALL
72 operator>>(basic_istream<_CharT, _Traits>& __istr,
73 const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
74 __f(__istr);
75 return __istr;
76 }
77
78 template <class _CharT, class _Traits, class _Arg>
79 inline basic_ostream<_CharT, _Traits>& _STLP_CALL
80 operator<<(basic_ostream<_CharT, _Traits>& __os,
81 const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
82 __f(__os);
83 return __os;
84 }
85
86 template <class _CharT, class _Traits>
87 inline basic_istream<_CharT, _Traits>& _STLP_CALL
88 operator>>(basic_istream<_CharT, _Traits>& __istr,
89 const _STLP_PRIV _Ios_Setf_Manip& __f) {
90 __f(__istr);
91 return __istr;
92 }
93
94 template <class _CharT, class _Traits>
95 inline basic_ostream<_CharT, _Traits>& _STLP_CALL
96 operator<<(basic_ostream<_CharT, _Traits>& __os,
97 const _STLP_PRIV _Ios_Setf_Manip& __f) {
98 __f(__os);
99 return __os;
100 }
101
102 //----------------------------------------------------------------------
103 // The ios_base manipulators.
resetiosflags(ios_base::fmtflags __mask)104 inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL resetiosflags(ios_base::fmtflags __mask)
105 { return _STLP_PRIV _Ios_Setf_Manip(0, __mask); }
106
setiosflags(ios_base::fmtflags __flag)107 inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL setiosflags(ios_base::fmtflags __flag)
108 { return _STLP_PRIV _Ios_Setf_Manip(__flag); }
109
setbase(int __n)110 inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL setbase(int __n) {
111 ios_base::fmtflags __base = __n == 8 ? ios_base::oct :
112 __n == 10 ? ios_base::dec :
113 __n == 16 ? ios_base::hex :
114 ios_base::fmtflags(0);
115 return _STLP_PRIV _Ios_Setf_Manip(__base, ios_base::basefield);
116 }
117
118 inline _STLP_PRIV _Ios_Manip_1<streamsize> _STLP_CALL
setprecision(int __n)119 setprecision(int __n) {
120 _STLP_PRIV _Ios_Manip_1<streamsize>::__f_ptr_type __f = &ios_base::precision;
121 return _STLP_PRIV _Ios_Manip_1<streamsize>(__f, __n);
122 }
123
124 inline _STLP_PRIV _Ios_Manip_1<streamsize> _STLP_CALL
setw(int __n)125 setw(int __n) {
126 _STLP_PRIV _Ios_Manip_1<streamsize>::__f_ptr_type __f = &ios_base::width;
127 return _STLP_PRIV _Ios_Manip_1<streamsize>(__f, __n);
128 }
129
130 //----------------------------------------------------------------------
131 // setfill, a manipulator that operates on basic_ios<> instead of ios_base.
132
133 _STLP_MOVE_TO_PRIV_NAMESPACE
134
135 template <class _CharT>
136 struct _Setfill_Manip {
_Setfill_Manip_Setfill_Manip137 _Setfill_Manip(_CharT __c) : _M_c(__c) {}
138 _CharT _M_c;
139 };
140
141 _STLP_MOVE_TO_STD_NAMESPACE
142
143 template <class _CharT, class _CharT2, class _Traits>
144 inline basic_ostream<_CharT, _Traits>& _STLP_CALL
145 operator<<(basic_ostream<_CharT, _Traits>& __os,
146 const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
147 __os.fill(__m._M_c);
148 return __os;
149 }
150
151 template <class _CharT, class _CharT2, class _Traits>
152 inline basic_istream<_CharT, _Traits>& _STLP_CALL
153 operator>>(basic_istream<_CharT, _Traits>& __is,
154 const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
155 __is.fill(__m._M_c);
156 return __is;
157 }
158
159 template <class _CharT>
setfill(_CharT __c)160 inline _STLP_PRIV _Setfill_Manip<_CharT> _STLP_CALL setfill(_CharT __c)
161 { return _STLP_PRIV _Setfill_Manip<_CharT>(__c); }
162
163 _STLP_END_NAMESPACE
164
165 #endif /* _STLP_INTERNAL_IOMANIP */
166