basic_ios.tcc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // basic_ios member functions -*- C++ -*-
  2. // Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/basic_ios.tcc
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ios}
  23. */
  24. #ifndef _BASIC_IOS_TCC
  25. #define _BASIC_IOS_TCC 1
  26. #pragma GCC system_header
  27. namespace std _GLIBCXX_VISIBILITY(default)
  28. {
  29. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  30. template<typename _CharT, typename _Traits>
  31. void
  32. basic_ios<_CharT, _Traits>::clear(iostate __state)
  33. {
  34. if (this->rdbuf())
  35. _M_streambuf_state = __state;
  36. else
  37. _M_streambuf_state = __state | badbit;
  38. if (this->exceptions() & this->rdstate())
  39. __throw_ios_failure(__N("basic_ios::clear"));
  40. }
  41. template<typename _CharT, typename _Traits>
  42. basic_streambuf<_CharT, _Traits>*
  43. basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
  44. {
  45. basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
  46. _M_streambuf = __sb;
  47. this->clear();
  48. return __old;
  49. }
  50. template<typename _CharT, typename _Traits>
  51. basic_ios<_CharT, _Traits>&
  52. basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
  53. {
  54. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  55. // 292. effects of a.copyfmt (a)
  56. if (this != std::__addressof(__rhs))
  57. {
  58. // Per 27.1.1, do not call imbue, yet must trash all caches
  59. // associated with imbue()
  60. // Alloc any new word array first, so if it fails we have "rollback".
  61. _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
  62. _M_local_word : new _Words[__rhs._M_word_size];
  63. // Bump refs before doing callbacks, for safety.
  64. _Callback_list* __cb = __rhs._M_callbacks;
  65. if (__cb)
  66. __cb->_M_add_reference();
  67. _M_call_callbacks(erase_event);
  68. if (_M_word != _M_local_word)
  69. {
  70. delete [] _M_word;
  71. _M_word = 0;
  72. }
  73. _M_dispose_callbacks();
  74. // NB: Don't want any added during above.
  75. _M_callbacks = __cb;
  76. for (int __i = 0; __i < __rhs._M_word_size; ++__i)
  77. __words[__i] = __rhs._M_word[__i];
  78. _M_word = __words;
  79. _M_word_size = __rhs._M_word_size;
  80. this->flags(__rhs.flags());
  81. this->width(__rhs.width());
  82. this->precision(__rhs.precision());
  83. this->tie(__rhs.tie());
  84. this->fill(__rhs.fill());
  85. _M_ios_locale = __rhs.getloc();
  86. _M_cache_locale(_M_ios_locale);
  87. _M_call_callbacks(copyfmt_event);
  88. // The next is required to be the last assignment.
  89. this->exceptions(__rhs.exceptions());
  90. }
  91. return *this;
  92. }
  93. // Locales:
  94. template<typename _CharT, typename _Traits>
  95. locale
  96. basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
  97. {
  98. locale __old(this->getloc());
  99. ios_base::imbue(__loc);
  100. _M_cache_locale(__loc);
  101. if (this->rdbuf() != 0)
  102. this->rdbuf()->pubimbue(__loc);
  103. return __old;
  104. }
  105. template<typename _CharT, typename _Traits>
  106. void
  107. basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
  108. {
  109. // NB: This may be called more than once on the same object.
  110. ios_base::_M_init();
  111. // Cache locale data and specific facets used by iostreams.
  112. _M_cache_locale(_M_ios_locale);
  113. // NB: The 27.4.4.1 Postconditions Table specifies requirements
  114. // after basic_ios::init() has been called. As part of this,
  115. // fill() must return widen(' ') any time after init() has been
  116. // called, which needs an imbued ctype facet of char_type to
  117. // return without throwing an exception. Unfortunately,
  118. // ctype<char_type> is not necessarily a required facet, so
  119. // streams with char_type != [char, wchar_t] will not have it by
  120. // default. Because of this, the correct value for _M_fill is
  121. // constructed on the first call of fill(). That way,
  122. // unformatted input and output with non-required basic_ios
  123. // instantiations is possible even without imbuing the expected
  124. // ctype<char_type> facet.
  125. _M_fill = _CharT();
  126. _M_fill_init = false;
  127. _M_tie = 0;
  128. _M_exception = goodbit;
  129. _M_streambuf = __sb;
  130. _M_streambuf_state = __sb ? goodbit : badbit;
  131. }
  132. template<typename _CharT, typename _Traits>
  133. void
  134. basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
  135. {
  136. if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
  137. _M_ctype = std::__addressof(use_facet<__ctype_type>(__loc));
  138. else
  139. _M_ctype = 0;
  140. if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
  141. _M_num_put = std::__addressof(use_facet<__num_put_type>(__loc));
  142. else
  143. _M_num_put = 0;
  144. if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
  145. _M_num_get = std::__addressof(use_facet<__num_get_type>(__loc));
  146. else
  147. _M_num_get = 0;
  148. }
  149. // Inhibit implicit instantiations for required instantiations,
  150. // which are defined via explicit instantiations elsewhere.
  151. #if _GLIBCXX_EXTERN_TEMPLATE
  152. extern template class basic_ios<char>;
  153. #ifdef _GLIBCXX_USE_WCHAR_T
  154. extern template class basic_ios<wchar_t>;
  155. #endif
  156. #endif
  157. _GLIBCXX_END_NAMESPACE_VERSION
  158. } // namespace std
  159. #endif