iomanip 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Standard stream manipulators -*- C++ -*-
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
  3. // 2006, 2007, 2009
  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 3, or (at your option)
  10. // any later version.
  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. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file iomanip
  23. * This is a Standard C++ Library header.
  24. */
  25. //
  26. // ISO C++ 14882: 27.6.3 Standard manipulators
  27. //
  28. #ifndef _GLIBCXX_IOMANIP
  29. #define _GLIBCXX_IOMANIP 1
  30. #pragma GCC system_header
  31. #include <bits/c++config.h>
  32. #include <iosfwd>
  33. #include <bits/ios_base.h>
  34. _GLIBCXX_BEGIN_NAMESPACE(std)
  35. // [27.6.3] standard manipulators
  36. // Also see DR 183.
  37. struct _Resetiosflags { ios_base::fmtflags _M_mask; };
  38. /**
  39. * @brief Manipulator for @c setf.
  40. * @param mask A format flags mask.
  41. *
  42. * Sent to a stream object, this manipulator resets the specified flags,
  43. * via @e stream.setf(0,mask).
  44. */
  45. inline _Resetiosflags
  46. resetiosflags(ios_base::fmtflags __mask)
  47. {
  48. _Resetiosflags __x;
  49. __x._M_mask = __mask;
  50. return __x;
  51. }
  52. template<typename _CharT, typename _Traits>
  53. inline basic_istream<_CharT, _Traits>&
  54. operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
  55. {
  56. __is.setf(ios_base::fmtflags(0), __f._M_mask);
  57. return __is;
  58. }
  59. template<typename _CharT, typename _Traits>
  60. inline basic_ostream<_CharT, _Traits>&
  61. operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
  62. {
  63. __os.setf(ios_base::fmtflags(0), __f._M_mask);
  64. return __os;
  65. }
  66. struct _Setiosflags { ios_base::fmtflags _M_mask; };
  67. /**
  68. * @brief Manipulator for @c setf.
  69. * @param mask A format flags mask.
  70. *
  71. * Sent to a stream object, this manipulator sets the format flags
  72. * to @a mask.
  73. */
  74. inline _Setiosflags
  75. setiosflags(ios_base::fmtflags __mask)
  76. {
  77. _Setiosflags __x;
  78. __x._M_mask = __mask;
  79. return __x;
  80. }
  81. template<typename _CharT, typename _Traits>
  82. inline basic_istream<_CharT, _Traits>&
  83. operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
  84. {
  85. __is.setf(__f._M_mask);
  86. return __is;
  87. }
  88. template<typename _CharT, typename _Traits>
  89. inline basic_ostream<_CharT, _Traits>&
  90. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
  91. {
  92. __os.setf(__f._M_mask);
  93. return __os;
  94. }
  95. struct _Setbase { int _M_base; };
  96. /**
  97. * @brief Manipulator for @c setf.
  98. * @param base A numeric base.
  99. *
  100. * Sent to a stream object, this manipulator changes the
  101. * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
  102. * is 8, 10, or 16, accordingly, and to 0 if @a base is any other value.
  103. */
  104. inline _Setbase
  105. setbase(int __base)
  106. {
  107. _Setbase __x;
  108. __x._M_base = __base;
  109. return __x;
  110. }
  111. template<typename _CharT, typename _Traits>
  112. inline basic_istream<_CharT, _Traits>&
  113. operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
  114. {
  115. __is.setf(__f._M_base == 8 ? ios_base::oct :
  116. __f._M_base == 10 ? ios_base::dec :
  117. __f._M_base == 16 ? ios_base::hex :
  118. ios_base::fmtflags(0), ios_base::basefield);
  119. return __is;
  120. }
  121. template<typename _CharT, typename _Traits>
  122. inline basic_ostream<_CharT, _Traits>&
  123. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
  124. {
  125. __os.setf(__f._M_base == 8 ? ios_base::oct :
  126. __f._M_base == 10 ? ios_base::dec :
  127. __f._M_base == 16 ? ios_base::hex :
  128. ios_base::fmtflags(0), ios_base::basefield);
  129. return __os;
  130. }
  131. template<typename _CharT>
  132. struct _Setfill { _CharT _M_c; };
  133. /**
  134. * @brief Manipulator for @c fill.
  135. * @param c The new fill character.
  136. *
  137. * Sent to a stream object, this manipulator calls @c fill(c) for that
  138. * object.
  139. */
  140. template<typename _CharT>
  141. inline _Setfill<_CharT>
  142. setfill(_CharT __c)
  143. {
  144. _Setfill<_CharT> __x;
  145. __x._M_c = __c;
  146. return __x;
  147. }
  148. template<typename _CharT, typename _Traits>
  149. inline basic_istream<_CharT, _Traits>&
  150. operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
  151. {
  152. __is.fill(__f._M_c);
  153. return __is;
  154. }
  155. template<typename _CharT, typename _Traits>
  156. inline basic_ostream<_CharT, _Traits>&
  157. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
  158. {
  159. __os.fill(__f._M_c);
  160. return __os;
  161. }
  162. struct _Setprecision { int _M_n; };
  163. /**
  164. * @brief Manipulator for @c precision.
  165. * @param n The new precision.
  166. *
  167. * Sent to a stream object, this manipulator calls @c precision(n) for
  168. * that object.
  169. */
  170. inline _Setprecision
  171. setprecision(int __n)
  172. {
  173. _Setprecision __x;
  174. __x._M_n = __n;
  175. return __x;
  176. }
  177. template<typename _CharT, typename _Traits>
  178. inline basic_istream<_CharT, _Traits>&
  179. operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
  180. {
  181. __is.precision(__f._M_n);
  182. return __is;
  183. }
  184. template<typename _CharT, typename _Traits>
  185. inline basic_ostream<_CharT, _Traits>&
  186. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
  187. {
  188. __os.precision(__f._M_n);
  189. return __os;
  190. }
  191. struct _Setw { int _M_n; };
  192. /**
  193. * @brief Manipulator for @c width.
  194. * @param n The new width.
  195. *
  196. * Sent to a stream object, this manipulator calls @c width(n) for
  197. * that object.
  198. */
  199. inline _Setw
  200. setw(int __n)
  201. {
  202. _Setw __x;
  203. __x._M_n = __n;
  204. return __x;
  205. }
  206. template<typename _CharT, typename _Traits>
  207. inline basic_istream<_CharT, _Traits>&
  208. operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
  209. {
  210. __is.width(__f._M_n);
  211. return __is;
  212. }
  213. template<typename _CharT, typename _Traits>
  214. inline basic_ostream<_CharT, _Traits>&
  215. operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
  216. {
  217. __os.width(__f._M_n);
  218. return __os;
  219. }
  220. // Inhibit implicit instantiations for required instantiations,
  221. // which are defined via explicit instantiations elsewhere.
  222. // NB: This syntax is a GNU extension.
  223. #if _GLIBCXX_EXTERN_TEMPLATE
  224. extern template ostream& operator<<(ostream&, _Setfill<char>);
  225. extern template ostream& operator<<(ostream&, _Setiosflags);
  226. extern template ostream& operator<<(ostream&, _Resetiosflags);
  227. extern template ostream& operator<<(ostream&, _Setbase);
  228. extern template ostream& operator<<(ostream&, _Setprecision);
  229. extern template ostream& operator<<(ostream&, _Setw);
  230. extern template istream& operator>>(istream&, _Setfill<char>);
  231. extern template istream& operator>>(istream&, _Setiosflags);
  232. extern template istream& operator>>(istream&, _Resetiosflags);
  233. extern template istream& operator>>(istream&, _Setbase);
  234. extern template istream& operator>>(istream&, _Setprecision);
  235. extern template istream& operator>>(istream&, _Setw);
  236. #ifdef _GLIBCXX_USE_WCHAR_T
  237. extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
  238. extern template wostream& operator<<(wostream&, _Setiosflags);
  239. extern template wostream& operator<<(wostream&, _Resetiosflags);
  240. extern template wostream& operator<<(wostream&, _Setbase);
  241. extern template wostream& operator<<(wostream&, _Setprecision);
  242. extern template wostream& operator<<(wostream&, _Setw);
  243. extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
  244. extern template wistream& operator>>(wistream&, _Setiosflags);
  245. extern template wistream& operator>>(wistream&, _Resetiosflags);
  246. extern template wistream& operator>>(wistream&, _Setbase);
  247. extern template wistream& operator>>(wistream&, _Setprecision);
  248. extern template wistream& operator>>(wistream&, _Setw);
  249. #endif
  250. #endif
  251. _GLIBCXX_END_NAMESPACE
  252. #endif /* _GLIBCXX_IOMANIP */