_iomanip.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _STLP_INTERNAL_IOMANIP
  19. #define _STLP_INTERNAL_IOMANIP
  20. #ifndef _STLP_INTERNAL_ISTREAM
  21. # include <stl/_istream.h> // Includes <ostream> and <ios>
  22. #endif
  23. _STLP_BEGIN_NAMESPACE
  24. _STLP_MOVE_TO_PRIV_NAMESPACE
  25. //----------------------------------------------------------------------
  26. // Machinery for defining manipulators.
  27. // Class that calls one of ios_base's single-argument member functions.
  28. template <class _Arg>
  29. struct _Ios_Manip_1 {
  30. typedef _Arg (ios_base::*__f_ptr_type)(_Arg);
  31. _Ios_Manip_1(__f_ptr_type __f, const _Arg& __arg)
  32. : _M_f(__f), _M_arg(__arg) {}
  33. void operator()(ios_base& __ios) const
  34. { (__ios.*_M_f)(_M_arg); }
  35. __f_ptr_type _M_f;
  36. _Arg _M_arg;
  37. };
  38. // Class that calls one of ios_base's two-argument member functions.
  39. struct _Ios_Setf_Manip {
  40. ios_base::fmtflags _M_flag;
  41. ios_base::fmtflags _M_mask;
  42. bool _M_two_args;
  43. _Ios_Setf_Manip(ios_base::fmtflags __f)
  44. : _M_flag(__f), _M_mask(0), _M_two_args(false) {}
  45. _Ios_Setf_Manip(ios_base::fmtflags __f, ios_base::fmtflags __m)
  46. : _M_flag(__f), _M_mask(__m), _M_two_args(true) {}
  47. void operator()(ios_base& __ios) const {
  48. if (_M_two_args)
  49. __ios.setf(_M_flag, _M_mask);
  50. else
  51. __ios.setf(_M_flag);
  52. }
  53. };
  54. _STLP_MOVE_TO_STD_NAMESPACE
  55. template <class _CharT, class _Traits, class _Arg>
  56. inline basic_istream<_CharT, _Traits>& _STLP_CALL
  57. operator>>(basic_istream<_CharT, _Traits>& __istr,
  58. const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
  59. __f(__istr);
  60. return __istr;
  61. }
  62. template <class _CharT, class _Traits, class _Arg>
  63. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  64. operator<<(basic_ostream<_CharT, _Traits>& __os,
  65. const _STLP_PRIV _Ios_Manip_1<_Arg>& __f) {
  66. __f(__os);
  67. return __os;
  68. }
  69. template <class _CharT, class _Traits>
  70. inline basic_istream<_CharT, _Traits>& _STLP_CALL
  71. operator>>(basic_istream<_CharT, _Traits>& __istr,
  72. const _STLP_PRIV _Ios_Setf_Manip& __f) {
  73. __f(__istr);
  74. return __istr;
  75. }
  76. template <class _CharT, class _Traits>
  77. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  78. operator<<(basic_ostream<_CharT, _Traits>& __os,
  79. const _STLP_PRIV _Ios_Setf_Manip& __f) {
  80. __f(__os);
  81. return __os;
  82. }
  83. //----------------------------------------------------------------------
  84. // The ios_base manipulators.
  85. inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL resetiosflags(ios_base::fmtflags __mask)
  86. { return _STLP_PRIV _Ios_Setf_Manip(0, __mask); }
  87. inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL setiosflags(ios_base::fmtflags __flag)
  88. { return _STLP_PRIV _Ios_Setf_Manip(__flag); }
  89. inline _STLP_PRIV _Ios_Setf_Manip _STLP_CALL setbase(int __n) {
  90. ios_base::fmtflags __base = __n == 8 ? ios_base::oct :
  91. __n == 10 ? ios_base::dec :
  92. __n == 16 ? ios_base::hex :
  93. ios_base::fmtflags(0);
  94. return _STLP_PRIV _Ios_Setf_Manip(__base, ios_base::basefield);
  95. }
  96. inline _STLP_PRIV _Ios_Manip_1<streamsize> _STLP_CALL
  97. setprecision(int __n) {
  98. _STLP_PRIV _Ios_Manip_1<streamsize>::__f_ptr_type __f = &ios_base::precision;
  99. return _STLP_PRIV _Ios_Manip_1<streamsize>(__f, __n);
  100. }
  101. inline _STLP_PRIV _Ios_Manip_1<streamsize> _STLP_CALL
  102. setw(int __n) {
  103. _STLP_PRIV _Ios_Manip_1<streamsize>::__f_ptr_type __f = &ios_base::width;
  104. return _STLP_PRIV _Ios_Manip_1<streamsize>(__f, __n);
  105. }
  106. //----------------------------------------------------------------------
  107. // setfill, a manipulator that operates on basic_ios<> instead of ios_base.
  108. _STLP_MOVE_TO_PRIV_NAMESPACE
  109. template <class _CharT>
  110. struct _Setfill_Manip {
  111. _Setfill_Manip(_CharT __c) : _M_c(__c) {}
  112. _CharT _M_c;
  113. };
  114. _STLP_MOVE_TO_STD_NAMESPACE
  115. template <class _CharT, class _CharT2, class _Traits>
  116. inline basic_ostream<_CharT, _Traits>& _STLP_CALL
  117. operator<<(basic_ostream<_CharT, _Traits>& __os,
  118. const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
  119. __os.fill(__m._M_c);
  120. return __os;
  121. }
  122. template <class _CharT, class _CharT2, class _Traits>
  123. inline basic_istream<_CharT, _Traits>& _STLP_CALL
  124. operator>>(basic_istream<_CharT, _Traits>& __is,
  125. const _STLP_PRIV _Setfill_Manip<_CharT2>& __m) {
  126. __is.fill(__m._M_c);
  127. return __is;
  128. }
  129. template <class _CharT>
  130. inline _STLP_PRIV _Setfill_Manip<_CharT> _STLP_CALL setfill(_CharT __c)
  131. { return _STLP_PRIV _Setfill_Manip<_CharT>(__c); }
  132. _STLP_END_NAMESPACE
  133. #endif /* _STLP_INTERNAL_IOMANIP */