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.
34template <class _Arg>
35struct _Ios_Manip_1 {
36   typedef _Arg (ios_base::*__f_ptr_type)(_Arg);
37
38  _Ios_Manip_1(__f_ptr_type __f, const _Arg& __arg)
39    : _M_f(__f), _M_arg(__arg) {}
40
41  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.
49struct _Ios_Setf_Manip {
50  ios_base::fmtflags _M_flag;
51  ios_base::fmtflags _M_mask;
52  bool _M_two_args;
53
54  _Ios_Setf_Manip(ios_base::fmtflags __f)
55    : _M_flag(__f), _M_mask(0), _M_two_args(false) {}
56
57  _Ios_Setf_Manip(ios_base::fmtflags __f, ios_base::fmtflags __m)
58    : _M_flag(__f), _M_mask(__m), _M_two_args(true) {}
59
60  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
70template <class _CharT, class _Traits, class _Arg>
71inline basic_istream<_CharT, _Traits>& _STLP_CALL
72operator>>(basic_istream<_CharT, _Traits>& __istr,
73           const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
74  __f(__istr);
75  return __istr;
76}
77
78template <class _CharT, class _Traits, class _Arg>
79inline basic_ostream<_CharT, _Traits>& _STLP_CALL
80operator<<(basic_ostream<_CharT, _Traits>& __os,
81           const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
82  __f(__os);
83  return __os;
84}
85
86template <class _CharT, class _Traits>
87inline basic_istream<_CharT, _Traits>& _STLP_CALL
88operator>>(basic_istream<_CharT, _Traits>& __istr,
89           const _STLP_PRIV _Ios_Setf_Manip& __f) {
90  __f(__istr);
91  return __istr;
92}
93
94template <class _CharT, class _Traits>
95inline basic_ostream<_CharT, _Traits>& _STLP_CALL
96operator<<(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.
104inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL resetiosflags(ios_base::fmtflags __mask)
105{ return _STLP_PRIV _Ios_Setf_Manip(0, __mask); }
106
107inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL setiosflags(ios_base::fmtflags __flag)
108{ return _STLP_PRIV _Ios_Setf_Manip(__flag); }
109
110inline _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
118inline _STLP_PRIV _Ios_Manip_1<streamsize> _STLP_CALL
119setprecision(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
124inline _STLP_PRIV _Ios_Manip_1<streamsize>  _STLP_CALL
125setw(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
135template <class _CharT>
136struct _Setfill_Manip {
137  _Setfill_Manip(_CharT __c) : _M_c(__c) {}
138  _CharT _M_c;
139};
140
141_STLP_MOVE_TO_STD_NAMESPACE
142
143template <class _CharT, class _CharT2, class _Traits>
144inline basic_ostream<_CharT, _Traits>& _STLP_CALL
145operator<<(basic_ostream<_CharT, _Traits>& __os,
146           const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
147  __os.fill(__m._M_c);
148  return __os;
149}
150
151template <class _CharT, class _CharT2, class _Traits>
152inline basic_istream<_CharT, _Traits>& _STLP_CALL
153operator>>(basic_istream<_CharT, _Traits>& __is,
154           const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
155  __is.fill(__m._M_c);
156  return __is;
157}
158
159template <class _CharT>
160inline _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