ostream 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. // Output streams -*- C++ -*-
  2. // Copyright (C) 1997-2015 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 include/ostream
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.6.2 Output streams
  25. //
  26. #ifndef _GLIBCXX_OSTREAM
  27. #define _GLIBCXX_OSTREAM 1
  28. #pragma GCC system_header
  29. #include <ios>
  30. #include <bits/ostream_insert.h>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /**
  35. * @brief Template class basic_ostream.
  36. * @ingroup io
  37. *
  38. * @tparam _CharT Type of character stream.
  39. * @tparam _Traits Traits for character type, defaults to
  40. * char_traits<_CharT>.
  41. *
  42. * This is the base class for all output streams. It provides text
  43. * formatting of all builtin types, and communicates with any class
  44. * derived from basic_streambuf to do the actual output.
  45. */
  46. template<typename _CharT, typename _Traits>
  47. class basic_ostream : virtual public basic_ios<_CharT, _Traits>
  48. {
  49. public:
  50. // Types (inherited from basic_ios):
  51. typedef _CharT char_type;
  52. typedef typename _Traits::int_type int_type;
  53. typedef typename _Traits::pos_type pos_type;
  54. typedef typename _Traits::off_type off_type;
  55. typedef _Traits traits_type;
  56. // Non-standard Types:
  57. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  58. typedef basic_ios<_CharT, _Traits> __ios_type;
  59. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  60. typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
  61. __num_put_type;
  62. typedef ctype<_CharT> __ctype_type;
  63. /**
  64. * @brief Base constructor.
  65. *
  66. * This ctor is almost never called by the user directly, rather from
  67. * derived classes' initialization lists, which pass a pointer to
  68. * their own stream buffer.
  69. */
  70. explicit
  71. basic_ostream(__streambuf_type* __sb)
  72. { this->init(__sb); }
  73. /**
  74. * @brief Base destructor.
  75. *
  76. * This does very little apart from providing a virtual base dtor.
  77. */
  78. virtual
  79. ~basic_ostream() { }
  80. /// Safe prefix/suffix operations.
  81. class sentry;
  82. friend class sentry;
  83. //@{
  84. /**
  85. * @brief Interface for manipulators.
  86. *
  87. * Manipulators such as @c std::endl and @c std::hex use these
  88. * functions in constructs like "std::cout << std::endl". For more
  89. * information, see the iomanip header.
  90. */
  91. __ostream_type&
  92. operator<<(__ostream_type& (*__pf)(__ostream_type&))
  93. {
  94. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  95. // DR 60. What is a formatted input function?
  96. // The inserters for manipulators are *not* formatted output functions.
  97. return __pf(*this);
  98. }
  99. __ostream_type&
  100. operator<<(__ios_type& (*__pf)(__ios_type&))
  101. {
  102. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  103. // DR 60. What is a formatted input function?
  104. // The inserters for manipulators are *not* formatted output functions.
  105. __pf(*this);
  106. return *this;
  107. }
  108. __ostream_type&
  109. operator<<(ios_base& (*__pf) (ios_base&))
  110. {
  111. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  112. // DR 60. What is a formatted input function?
  113. // The inserters for manipulators are *not* formatted output functions.
  114. __pf(*this);
  115. return *this;
  116. }
  117. //@}
  118. //@{
  119. /**
  120. * @name Inserters
  121. *
  122. * All the @c operator<< functions (aka <em>formatted output
  123. * functions</em>) have some common behavior. Each starts by
  124. * constructing a temporary object of type std::basic_ostream::sentry.
  125. * This can have several effects, concluding with the setting of a
  126. * status flag; see the sentry documentation for more.
  127. *
  128. * If the sentry status is good, the function tries to generate
  129. * whatever data is appropriate for the type of the argument.
  130. *
  131. * If an exception is thrown during insertion, ios_base::badbit
  132. * will be turned on in the stream's error state without causing an
  133. * ios_base::failure to be thrown. The original exception will then
  134. * be rethrown.
  135. */
  136. //@{
  137. /**
  138. * @brief Integer arithmetic inserters
  139. * @param __n A variable of builtin integral type.
  140. * @return @c *this if successful
  141. *
  142. * These functions use the stream's current locale (specifically, the
  143. * @c num_get facet) to perform numeric formatting.
  144. */
  145. __ostream_type&
  146. operator<<(long __n)
  147. { return _M_insert(__n); }
  148. __ostream_type&
  149. operator<<(unsigned long __n)
  150. { return _M_insert(__n); }
  151. __ostream_type&
  152. operator<<(bool __n)
  153. { return _M_insert(__n); }
  154. __ostream_type&
  155. operator<<(short __n);
  156. __ostream_type&
  157. operator<<(unsigned short __n)
  158. {
  159. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  160. // 117. basic_ostream uses nonexistent num_put member functions.
  161. return _M_insert(static_cast<unsigned long>(__n));
  162. }
  163. __ostream_type&
  164. operator<<(int __n);
  165. __ostream_type&
  166. operator<<(unsigned int __n)
  167. {
  168. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  169. // 117. basic_ostream uses nonexistent num_put member functions.
  170. return _M_insert(static_cast<unsigned long>(__n));
  171. }
  172. #ifdef _GLIBCXX_USE_LONG_LONG
  173. __ostream_type&
  174. operator<<(long long __n)
  175. { return _M_insert(__n); }
  176. __ostream_type&
  177. operator<<(unsigned long long __n)
  178. { return _M_insert(__n); }
  179. #endif
  180. //@}
  181. //@{
  182. /**
  183. * @brief Floating point arithmetic inserters
  184. * @param __f A variable of builtin floating point type.
  185. * @return @c *this if successful
  186. *
  187. * These functions use the stream's current locale (specifically, the
  188. * @c num_get facet) to perform numeric formatting.
  189. */
  190. __ostream_type&
  191. operator<<(double __f)
  192. { return _M_insert(__f); }
  193. __ostream_type&
  194. operator<<(float __f)
  195. {
  196. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  197. // 117. basic_ostream uses nonexistent num_put member functions.
  198. return _M_insert(static_cast<double>(__f));
  199. }
  200. __ostream_type&
  201. operator<<(long double __f)
  202. { return _M_insert(__f); }
  203. //@}
  204. /**
  205. * @brief Pointer arithmetic inserters
  206. * @param __p A variable of pointer type.
  207. * @return @c *this if successful
  208. *
  209. * These functions use the stream's current locale (specifically, the
  210. * @c num_get facet) to perform numeric formatting.
  211. */
  212. __ostream_type&
  213. operator<<(const void* __p)
  214. { return _M_insert(__p); }
  215. /**
  216. * @brief Extracting from another streambuf.
  217. * @param __sb A pointer to a streambuf
  218. *
  219. * This function behaves like one of the basic arithmetic extractors,
  220. * in that it also constructs a sentry object and has the same error
  221. * handling behavior.
  222. *
  223. * If @p __sb is NULL, the stream will set failbit in its error state.
  224. *
  225. * Characters are extracted from @p __sb and inserted into @c *this
  226. * until one of the following occurs:
  227. *
  228. * - the input stream reaches end-of-file,
  229. * - insertion into the output sequence fails (in this case, the
  230. * character that would have been inserted is not extracted), or
  231. * - an exception occurs while getting a character from @p __sb, which
  232. * sets failbit in the error state
  233. *
  234. * If the function inserts no characters, failbit is set.
  235. */
  236. __ostream_type&
  237. operator<<(__streambuf_type* __sb);
  238. //@}
  239. //@{
  240. /**
  241. * @name Unformatted Output Functions
  242. *
  243. * All the unformatted output functions have some common behavior.
  244. * Each starts by constructing a temporary object of type
  245. * std::basic_ostream::sentry. This has several effects, concluding
  246. * with the setting of a status flag; see the sentry documentation
  247. * for more.
  248. *
  249. * If the sentry status is good, the function tries to generate
  250. * whatever data is appropriate for the type of the argument.
  251. *
  252. * If an exception is thrown during insertion, ios_base::badbit
  253. * will be turned on in the stream's error state. If badbit is on in
  254. * the stream's exceptions mask, the exception will be rethrown
  255. * without completing its actions.
  256. */
  257. /**
  258. * @brief Simple insertion.
  259. * @param __c The character to insert.
  260. * @return *this
  261. *
  262. * Tries to insert @p __c.
  263. *
  264. * @note This function is not overloaded on signed char and
  265. * unsigned char.
  266. */
  267. __ostream_type&
  268. put(char_type __c);
  269. /**
  270. * @brief Core write functionality, without sentry.
  271. * @param __s The array to insert.
  272. * @param __n Maximum number of characters to insert.
  273. */
  274. void
  275. _M_write(const char_type* __s, streamsize __n)
  276. {
  277. const streamsize __put = this->rdbuf()->sputn(__s, __n);
  278. if (__put != __n)
  279. this->setstate(ios_base::badbit);
  280. }
  281. /**
  282. * @brief Character string insertion.
  283. * @param __s The array to insert.
  284. * @param __n Maximum number of characters to insert.
  285. * @return *this
  286. *
  287. * Characters are copied from @p __s and inserted into the stream until
  288. * one of the following happens:
  289. *
  290. * - @p __n characters are inserted
  291. * - inserting into the output sequence fails (in this case, badbit
  292. * will be set in the stream's error state)
  293. *
  294. * @note This function is not overloaded on signed char and
  295. * unsigned char.
  296. */
  297. __ostream_type&
  298. write(const char_type* __s, streamsize __n);
  299. //@}
  300. /**
  301. * @brief Synchronizing the stream buffer.
  302. * @return *this
  303. *
  304. * If @c rdbuf() is a null pointer, changes nothing.
  305. *
  306. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  307. * sets badbit.
  308. */
  309. __ostream_type&
  310. flush();
  311. /**
  312. * @brief Getting the current write position.
  313. * @return A file position object.
  314. *
  315. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  316. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
  317. */
  318. pos_type
  319. tellp();
  320. /**
  321. * @brief Changing the current write position.
  322. * @param __pos A file position object.
  323. * @return *this
  324. *
  325. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
  326. * that function fails, sets failbit.
  327. */
  328. __ostream_type&
  329. seekp(pos_type);
  330. /**
  331. * @brief Changing the current write position.
  332. * @param __off A file offset object.
  333. * @param __dir The direction in which to seek.
  334. * @return *this
  335. *
  336. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
  337. * If that function fails, sets failbit.
  338. */
  339. __ostream_type&
  340. seekp(off_type, ios_base::seekdir);
  341. protected:
  342. basic_ostream()
  343. { this->init(0); }
  344. #if __cplusplus >= 201103L
  345. // Non-standard constructor that does not call init()
  346. basic_ostream(basic_iostream<_CharT, _Traits>&) { }
  347. basic_ostream(const basic_ostream&) = delete;
  348. basic_ostream(basic_ostream&& __rhs)
  349. : __ios_type()
  350. { __ios_type::move(__rhs); }
  351. // 27.7.3.3 Assign/swap
  352. basic_ostream& operator=(const basic_ostream&) = delete;
  353. basic_ostream&
  354. operator=(basic_ostream&& __rhs)
  355. {
  356. swap(__rhs);
  357. return *this;
  358. }
  359. void
  360. swap(basic_ostream& __rhs)
  361. { __ios_type::swap(__rhs); }
  362. #endif
  363. template<typename _ValueT>
  364. __ostream_type&
  365. _M_insert(_ValueT __v);
  366. };
  367. /**
  368. * @brief Performs setup work for output streams.
  369. *
  370. * Objects of this class are created before all of the standard
  371. * inserters are run. It is responsible for <em>exception-safe prefix and
  372. * suffix operations</em>.
  373. */
  374. template <typename _CharT, typename _Traits>
  375. class basic_ostream<_CharT, _Traits>::sentry
  376. {
  377. // Data Members.
  378. bool _M_ok;
  379. basic_ostream<_CharT, _Traits>& _M_os;
  380. public:
  381. /**
  382. * @brief The constructor performs preparatory work.
  383. * @param __os The output stream to guard.
  384. *
  385. * If the stream state is good (@a __os.good() is true), then if the
  386. * stream is tied to another output stream, @c is.tie()->flush()
  387. * is called to synchronize the output sequences.
  388. *
  389. * If the stream state is still good, then the sentry state becomes
  390. * true (@a okay).
  391. */
  392. explicit
  393. sentry(basic_ostream<_CharT, _Traits>& __os);
  394. /**
  395. * @brief Possibly flushes the stream.
  396. *
  397. * If @c ios_base::unitbuf is set in @c os.flags(), and
  398. * @c std::uncaught_exception() is true, the sentry destructor calls
  399. * @c flush() on the output stream.
  400. */
  401. ~sentry()
  402. {
  403. // XXX MT
  404. if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
  405. {
  406. // Can't call flush directly or else will get into recursive lock.
  407. if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
  408. _M_os.setstate(ios_base::badbit);
  409. }
  410. }
  411. /**
  412. * @brief Quick status checking.
  413. * @return The sentry state.
  414. *
  415. * For ease of use, sentries may be converted to booleans. The
  416. * return value is that of the sentry state (true == okay).
  417. */
  418. #if __cplusplus >= 201103L
  419. explicit
  420. #endif
  421. operator bool() const
  422. { return _M_ok; }
  423. };
  424. //@{
  425. /**
  426. * @brief Character inserters
  427. * @param __out An output stream.
  428. * @param __c A character.
  429. * @return out
  430. *
  431. * Behaves like one of the formatted arithmetic inserters described in
  432. * std::basic_ostream. After constructing a sentry object with good
  433. * status, this function inserts a single character and any required
  434. * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
  435. * called.
  436. *
  437. * If @p __c is of type @c char and the character type of the stream is not
  438. * @c char, the character is widened before insertion.
  439. */
  440. template<typename _CharT, typename _Traits>
  441. inline basic_ostream<_CharT, _Traits>&
  442. operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
  443. { return __ostream_insert(__out, &__c, 1); }
  444. template<typename _CharT, typename _Traits>
  445. inline basic_ostream<_CharT, _Traits>&
  446. operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
  447. { return (__out << __out.widen(__c)); }
  448. // Specialization
  449. template <class _Traits>
  450. inline basic_ostream<char, _Traits>&
  451. operator<<(basic_ostream<char, _Traits>& __out, char __c)
  452. { return __ostream_insert(__out, &__c, 1); }
  453. // Signed and unsigned
  454. template<class _Traits>
  455. inline basic_ostream<char, _Traits>&
  456. operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
  457. { return (__out << static_cast<char>(__c)); }
  458. template<class _Traits>
  459. inline basic_ostream<char, _Traits>&
  460. operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
  461. { return (__out << static_cast<char>(__c)); }
  462. //@}
  463. //@{
  464. /**
  465. * @brief String inserters
  466. * @param __out An output stream.
  467. * @param __s A character string.
  468. * @return out
  469. * @pre @p __s must be a non-NULL pointer
  470. *
  471. * Behaves like one of the formatted arithmetic inserters described in
  472. * std::basic_ostream. After constructing a sentry object with good
  473. * status, this function inserts @c traits::length(__s) characters starting
  474. * at @p __s, widened if necessary, followed by any required padding (as
  475. * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
  476. */
  477. template<typename _CharT, typename _Traits>
  478. inline basic_ostream<_CharT, _Traits>&
  479. operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
  480. {
  481. if (!__s)
  482. __out.setstate(ios_base::badbit);
  483. else
  484. __ostream_insert(__out, __s,
  485. static_cast<streamsize>(_Traits::length(__s)));
  486. return __out;
  487. }
  488. template<typename _CharT, typename _Traits>
  489. basic_ostream<_CharT, _Traits> &
  490. operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
  491. // Partial specializations
  492. template<class _Traits>
  493. inline basic_ostream<char, _Traits>&
  494. operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
  495. {
  496. if (!__s)
  497. __out.setstate(ios_base::badbit);
  498. else
  499. __ostream_insert(__out, __s,
  500. static_cast<streamsize>(_Traits::length(__s)));
  501. return __out;
  502. }
  503. // Signed and unsigned
  504. template<class _Traits>
  505. inline basic_ostream<char, _Traits>&
  506. operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
  507. { return (__out << reinterpret_cast<const char*>(__s)); }
  508. template<class _Traits>
  509. inline basic_ostream<char, _Traits> &
  510. operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
  511. { return (__out << reinterpret_cast<const char*>(__s)); }
  512. //@}
  513. // Standard basic_ostream manipulators
  514. /**
  515. * @brief Write a newline and flush the stream.
  516. *
  517. * This manipulator is often mistakenly used when a simple newline is
  518. * desired, leading to poor buffering performance. See
  519. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  520. * for more on this subject.
  521. */
  522. template<typename _CharT, typename _Traits>
  523. inline basic_ostream<_CharT, _Traits>&
  524. endl(basic_ostream<_CharT, _Traits>& __os)
  525. { return flush(__os.put(__os.widen('\n'))); }
  526. /**
  527. * @brief Write a null character into the output sequence.
  528. *
  529. * <em>Null character</em> is @c CharT() by definition. For CharT
  530. * of @c char, this correctly writes the ASCII @c NUL character
  531. * string terminator.
  532. */
  533. template<typename _CharT, typename _Traits>
  534. inline basic_ostream<_CharT, _Traits>&
  535. ends(basic_ostream<_CharT, _Traits>& __os)
  536. { return __os.put(_CharT()); }
  537. /**
  538. * @brief Flushes the output stream.
  539. *
  540. * This manipulator simply calls the stream's @c flush() member function.
  541. */
  542. template<typename _CharT, typename _Traits>
  543. inline basic_ostream<_CharT, _Traits>&
  544. flush(basic_ostream<_CharT, _Traits>& __os)
  545. { return __os.flush(); }
  546. #if __cplusplus >= 201103L
  547. /**
  548. * @brief Generic inserter for rvalue stream
  549. * @param __os An input stream.
  550. * @param __x A reference to the object being inserted.
  551. * @return os
  552. *
  553. * This is just a forwarding function to allow insertion to
  554. * rvalue streams since they won't bind to the inserter functions
  555. * that take an lvalue reference.
  556. */
  557. template<typename _CharT, typename _Traits, typename _Tp>
  558. inline basic_ostream<_CharT, _Traits>&
  559. operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
  560. {
  561. __os << __x;
  562. return __os;
  563. }
  564. #endif // C++11
  565. _GLIBCXX_END_NAMESPACE_VERSION
  566. } // namespace std
  567. #include <bits/ostream.tcc>
  568. #endif /* _GLIBCXX_OSTREAM */