Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

519 lines
16KB

  1. // Iostreams base classes -*- C++ -*-
  2. // Copyright (C) 1997-2020 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.h
  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_H
  25. #define _BASIC_IOS_H 1
  26. #pragma GCC system_header
  27. #include <bits/localefwd.h>
  28. #include <bits/locale_classes.h>
  29. #include <bits/locale_facets.h>
  30. #include <bits/streambuf_iterator.h>
  31. #include <bits/move.h>
  32. namespace std _GLIBCXX_VISIBILITY(default)
  33. {
  34. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  35. template<typename _Facet>
  36. inline const _Facet&
  37. __check_facet(const _Facet* __f)
  38. {
  39. if (!__f)
  40. __throw_bad_cast();
  41. return *__f;
  42. }
  43. /**
  44. * @brief Template class basic_ios, virtual base class for all
  45. * stream classes.
  46. * @ingroup io
  47. *
  48. * @tparam _CharT Type of character stream.
  49. * @tparam _Traits Traits for character type, defaults to
  50. * char_traits<_CharT>.
  51. *
  52. * Most of the member functions called dispatched on stream objects
  53. * (e.g., @c std::cout.foo(bar);) are consolidated in this class.
  54. */
  55. template<typename _CharT, typename _Traits>
  56. class basic_ios : public ios_base
  57. {
  58. public:
  59. //@{
  60. /**
  61. * These are standard types. They permit a standardized way of
  62. * referring to names of (or names dependent on) the template
  63. * parameters, which are specific to the implementation.
  64. */
  65. typedef _CharT char_type;
  66. typedef typename _Traits::int_type int_type;
  67. typedef typename _Traits::pos_type pos_type;
  68. typedef typename _Traits::off_type off_type;
  69. typedef _Traits traits_type;
  70. //@}
  71. //@{
  72. /**
  73. * These are non-standard types.
  74. */
  75. typedef ctype<_CharT> __ctype_type;
  76. typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
  77. __num_put_type;
  78. typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
  79. __num_get_type;
  80. //@}
  81. // Data members:
  82. protected:
  83. basic_ostream<_CharT, _Traits>* _M_tie;
  84. mutable char_type _M_fill;
  85. mutable bool _M_fill_init;
  86. basic_streambuf<_CharT, _Traits>* _M_streambuf;
  87. // Cached use_facet<ctype>, which is based on the current locale info.
  88. const __ctype_type* _M_ctype;
  89. // For ostream.
  90. const __num_put_type* _M_num_put;
  91. // For istream.
  92. const __num_get_type* _M_num_get;
  93. public:
  94. //@{
  95. /**
  96. * @brief The quick-and-easy status check.
  97. *
  98. * This allows you to write constructs such as
  99. * <code>if (!a_stream) ...</code> and <code>while (a_stream) ...</code>
  100. */
  101. #if __cplusplus >= 201103L
  102. explicit operator bool() const
  103. { return !this->fail(); }
  104. #else
  105. operator void*() const
  106. { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
  107. #endif
  108. bool
  109. operator!() const
  110. { return this->fail(); }
  111. //@}
  112. /**
  113. * @brief Returns the error state of the stream buffer.
  114. * @return A bit pattern (well, isn't everything?)
  115. *
  116. * See std::ios_base::iostate for the possible bit values. Most
  117. * users will call one of the interpreting wrappers, e.g., good().
  118. */
  119. iostate
  120. rdstate() const
  121. { return _M_streambuf_state; }
  122. /**
  123. * @brief [Re]sets the error state.
  124. * @param __state The new state flag(s) to set.
  125. *
  126. * See std::ios_base::iostate for the possible bit values. Most
  127. * users will not need to pass an argument.
  128. */
  129. void
  130. clear(iostate __state = goodbit);
  131. /**
  132. * @brief Sets additional flags in the error state.
  133. * @param __state The additional state flag(s) to set.
  134. *
  135. * See std::ios_base::iostate for the possible bit values.
  136. */
  137. void
  138. setstate(iostate __state)
  139. { this->clear(this->rdstate() | __state); }
  140. // Flip the internal state on for the proper state bits, then
  141. // rethrows the propagated exception if bit also set in
  142. // exceptions().
  143. void
  144. _M_setstate(iostate __state)
  145. {
  146. // 27.6.1.2.1 Common requirements.
  147. // Turn this on without causing an ios::failure to be thrown.
  148. _M_streambuf_state |= __state;
  149. if (this->exceptions() & __state)
  150. __throw_exception_again;
  151. }
  152. /**
  153. * @brief Fast error checking.
  154. * @return True if no error flags are set.
  155. *
  156. * A wrapper around rdstate.
  157. */
  158. bool
  159. good() const
  160. { return this->rdstate() == 0; }
  161. /**
  162. * @brief Fast error checking.
  163. * @return True if the eofbit is set.
  164. *
  165. * Note that other iostate flags may also be set.
  166. */
  167. bool
  168. eof() const
  169. { return (this->rdstate() & eofbit) != 0; }
  170. /**
  171. * @brief Fast error checking.
  172. * @return True if either the badbit or the failbit is set.
  173. *
  174. * Checking the badbit in fail() is historical practice.
  175. * Note that other iostate flags may also be set.
  176. */
  177. bool
  178. fail() const
  179. { return (this->rdstate() & (badbit | failbit)) != 0; }
  180. /**
  181. * @brief Fast error checking.
  182. * @return True if the badbit is set.
  183. *
  184. * Note that other iostate flags may also be set.
  185. */
  186. bool
  187. bad() const
  188. { return (this->rdstate() & badbit) != 0; }
  189. /**
  190. * @brief Throwing exceptions on errors.
  191. * @return The current exceptions mask.
  192. *
  193. * This changes nothing in the stream. See the one-argument version
  194. * of exceptions(iostate) for the meaning of the return value.
  195. */
  196. iostate
  197. exceptions() const
  198. { return _M_exception; }
  199. /**
  200. * @brief Throwing exceptions on errors.
  201. * @param __except The new exceptions mask.
  202. *
  203. * By default, error flags are set silently. You can set an
  204. * exceptions mask for each stream; if a bit in the mask becomes set
  205. * in the error flags, then an exception of type
  206. * std::ios_base::failure is thrown.
  207. *
  208. * If the error flag is already set when the exceptions mask is
  209. * added, the exception is immediately thrown. Try running the
  210. * following under GCC 3.1 or later:
  211. * @code
  212. * #include <iostream>
  213. * #include <fstream>
  214. * #include <exception>
  215. *
  216. * int main()
  217. * {
  218. * std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
  219. *
  220. * std::ifstream f ("/etc/motd");
  221. *
  222. * std::cerr << "Setting badbit\n";
  223. * f.setstate (std::ios_base::badbit);
  224. *
  225. * std::cerr << "Setting exception mask\n";
  226. * f.exceptions (std::ios_base::badbit);
  227. * }
  228. * @endcode
  229. */
  230. void
  231. exceptions(iostate __except)
  232. {
  233. _M_exception = __except;
  234. this->clear(_M_streambuf_state);
  235. }
  236. // Constructor/destructor:
  237. /**
  238. * @brief Constructor performs initialization.
  239. *
  240. * The parameter is passed by derived streams.
  241. */
  242. explicit
  243. basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
  244. : ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
  245. _M_ctype(0), _M_num_put(0), _M_num_get(0)
  246. { this->init(__sb); }
  247. /**
  248. * @brief Empty.
  249. *
  250. * The destructor does nothing. More specifically, it does not
  251. * destroy the streambuf held by rdbuf().
  252. */
  253. virtual
  254. ~basic_ios() { }
  255. // Members:
  256. /**
  257. * @brief Fetches the current @e tied stream.
  258. * @return A pointer to the tied stream, or NULL if the stream is
  259. * not tied.
  260. *
  261. * A stream may be @e tied (or synchronized) to a second output
  262. * stream. When this stream performs any I/O, the tied stream is
  263. * first flushed. For example, @c std::cin is tied to @c std::cout.
  264. */
  265. basic_ostream<_CharT, _Traits>*
  266. tie() const
  267. { return _M_tie; }
  268. /**
  269. * @brief Ties this stream to an output stream.
  270. * @param __tiestr The output stream.
  271. * @return The previously tied output stream, or NULL if the stream
  272. * was not tied.
  273. *
  274. * This sets up a new tie; see tie() for more.
  275. */
  276. basic_ostream<_CharT, _Traits>*
  277. tie(basic_ostream<_CharT, _Traits>* __tiestr)
  278. {
  279. basic_ostream<_CharT, _Traits>* __old = _M_tie;
  280. _M_tie = __tiestr;
  281. return __old;
  282. }
  283. /**
  284. * @brief Accessing the underlying buffer.
  285. * @return The current stream buffer.
  286. *
  287. * This does not change the state of the stream.
  288. */
  289. basic_streambuf<_CharT, _Traits>*
  290. rdbuf() const
  291. { return _M_streambuf; }
  292. /**
  293. * @brief Changing the underlying buffer.
  294. * @param __sb The new stream buffer.
  295. * @return The previous stream buffer.
  296. *
  297. * Associates a new buffer with the current stream, and clears the
  298. * error state.
  299. *
  300. * Due to historical accidents which the LWG refuses to correct, the
  301. * I/O library suffers from a design error: this function is hidden
  302. * in derived classes by overrides of the zero-argument @c rdbuf(),
  303. * which is non-virtual for hysterical raisins. As a result, you
  304. * must use explicit qualifications to access this function via any
  305. * derived class. For example:
  306. *
  307. * @code
  308. * std::fstream foo; // or some other derived type
  309. * std::streambuf* p = .....;
  310. *
  311. * foo.ios::rdbuf(p); // ios == basic_ios<char>
  312. * @endcode
  313. */
  314. basic_streambuf<_CharT, _Traits>*
  315. rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
  316. /**
  317. * @brief Copies fields of __rhs into this.
  318. * @param __rhs The source values for the copies.
  319. * @return Reference to this object.
  320. *
  321. * All fields of __rhs are copied into this object except that rdbuf()
  322. * and rdstate() remain unchanged. All values in the pword and iword
  323. * arrays are copied. Before copying, each callback is invoked with
  324. * erase_event. After copying, each (new) callback is invoked with
  325. * copyfmt_event. The final step is to copy exceptions().
  326. */
  327. basic_ios&
  328. copyfmt(const basic_ios& __rhs);
  329. /**
  330. * @brief Retrieves the @a empty character.
  331. * @return The current fill character.
  332. *
  333. * It defaults to a space (' ') in the current locale.
  334. */
  335. char_type
  336. fill() const
  337. {
  338. if (!_M_fill_init)
  339. {
  340. _M_fill = this->widen(' ');
  341. _M_fill_init = true;
  342. }
  343. return _M_fill;
  344. }
  345. /**
  346. * @brief Sets a new @a empty character.
  347. * @param __ch The new character.
  348. * @return The previous fill character.
  349. *
  350. * The fill character is used to fill out space when P+ characters
  351. * have been requested (e.g., via setw), Q characters are actually
  352. * used, and Q<P. It defaults to a space (' ') in the current locale.
  353. */
  354. char_type
  355. fill(char_type __ch)
  356. {
  357. char_type __old = this->fill();
  358. _M_fill = __ch;
  359. return __old;
  360. }
  361. // Locales:
  362. /**
  363. * @brief Moves to a new locale.
  364. * @param __loc The new locale.
  365. * @return The previous locale.
  366. *
  367. * Calls @c ios_base::imbue(loc), and if a stream buffer is associated
  368. * with this stream, calls that buffer's @c pubimbue(loc).
  369. *
  370. * Additional l10n notes are at
  371. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
  372. */
  373. locale
  374. imbue(const locale& __loc);
  375. /**
  376. * @brief Squeezes characters.
  377. * @param __c The character to narrow.
  378. * @param __dfault The character to narrow.
  379. * @return The narrowed character.
  380. *
  381. * Maps a character of @c char_type to a character of @c char,
  382. * if possible.
  383. *
  384. * Returns the result of
  385. * @code
  386. * std::use_facet<ctype<char_type> >(getloc()).narrow(c,dfault)
  387. * @endcode
  388. *
  389. * Additional l10n notes are at
  390. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
  391. */
  392. char
  393. narrow(char_type __c, char __dfault) const
  394. { return __check_facet(_M_ctype).narrow(__c, __dfault); }
  395. /**
  396. * @brief Widens characters.
  397. * @param __c The character to widen.
  398. * @return The widened character.
  399. *
  400. * Maps a character of @c char to a character of @c char_type.
  401. *
  402. * Returns the result of
  403. * @code
  404. * std::use_facet<ctype<char_type> >(getloc()).widen(c)
  405. * @endcode
  406. *
  407. * Additional l10n notes are at
  408. * http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
  409. */
  410. char_type
  411. widen(char __c) const
  412. { return __check_facet(_M_ctype).widen(__c); }
  413. protected:
  414. // 27.4.5.1 basic_ios constructors
  415. /**
  416. * @brief Empty.
  417. *
  418. * The default constructor does nothing and is not normally
  419. * accessible to users.
  420. */
  421. basic_ios()
  422. : ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false),
  423. _M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
  424. { }
  425. /**
  426. * @brief All setup is performed here.
  427. *
  428. * This is called from the public constructor. It is not virtual and
  429. * cannot be redefined.
  430. */
  431. void
  432. init(basic_streambuf<_CharT, _Traits>* __sb);
  433. #if __cplusplus >= 201103L
  434. basic_ios(const basic_ios&) = delete;
  435. basic_ios& operator=(const basic_ios&) = delete;
  436. void
  437. move(basic_ios& __rhs)
  438. {
  439. ios_base::_M_move(__rhs);
  440. _M_cache_locale(_M_ios_locale);
  441. this->tie(__rhs.tie(nullptr));
  442. _M_fill = __rhs._M_fill;
  443. _M_fill_init = __rhs._M_fill_init;
  444. _M_streambuf = nullptr;
  445. }
  446. void
  447. move(basic_ios&& __rhs)
  448. { this->move(__rhs); }
  449. void
  450. swap(basic_ios& __rhs) noexcept
  451. {
  452. ios_base::_M_swap(__rhs);
  453. _M_cache_locale(_M_ios_locale);
  454. __rhs._M_cache_locale(__rhs._M_ios_locale);
  455. std::swap(_M_tie, __rhs._M_tie);
  456. std::swap(_M_fill, __rhs._M_fill);
  457. std::swap(_M_fill_init, __rhs._M_fill_init);
  458. }
  459. void
  460. set_rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
  461. { _M_streambuf = __sb; }
  462. #endif
  463. void
  464. _M_cache_locale(const locale& __loc);
  465. };
  466. _GLIBCXX_END_NAMESPACE_VERSION
  467. } // namespace
  468. #include <bits/basic_ios.tcc>
  469. #endif /* _BASIC_IOS_H */