ostream 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // Output streams -*- C++ -*-
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2008, 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 ostream
  23. * This is a Standard C++ Library header.
  24. */
  25. //
  26. // ISO C++ 14882: 27.6.2 Output streams
  27. //
  28. #ifndef _GLIBCXX_OSTREAM
  29. #define _GLIBCXX_OSTREAM 1
  30. #pragma GCC system_header
  31. #include <ios>
  32. #include <bits/ostream_insert.h>
  33. _GLIBCXX_BEGIN_NAMESPACE(std)
  34. // [27.6.2.1] Template class basic_ostream
  35. /**
  36. * @brief Controlling output.
  37. * @ingroup io
  38. *
  39. * This is the base class for all output streams. It provides text
  40. * formatting of all builtin types, and communicates with any class
  41. * derived from basic_streambuf to do the actual output.
  42. */
  43. template<typename _CharT, typename _Traits>
  44. class basic_ostream : virtual public basic_ios<_CharT, _Traits>
  45. {
  46. public:
  47. // Types (inherited from basic_ios (27.4.4)):
  48. typedef _CharT char_type;
  49. typedef typename _Traits::int_type int_type;
  50. typedef typename _Traits::pos_type pos_type;
  51. typedef typename _Traits::off_type off_type;
  52. typedef _Traits traits_type;
  53. // Non-standard Types:
  54. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  55. typedef basic_ios<_CharT, _Traits> __ios_type;
  56. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  57. typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
  58. __num_put_type;
  59. typedef ctype<_CharT> __ctype_type;
  60. // [27.6.2.2] constructor/destructor
  61. /**
  62. * @brief Base constructor.
  63. *
  64. * This ctor is almost never called by the user directly, rather from
  65. * derived classes' initialization lists, which pass a pointer to
  66. * their own stream buffer.
  67. */
  68. explicit
  69. basic_ostream(__streambuf_type* __sb)
  70. { this->init(__sb); }
  71. /**
  72. * @brief Base destructor.
  73. *
  74. * This does very little apart from providing a virtual base dtor.
  75. */
  76. virtual
  77. ~basic_ostream() { }
  78. // [27.6.2.3] prefix/suffix
  79. class sentry;
  80. friend class sentry;
  81. // [27.6.2.5] formatted output
  82. // [27.6.2.5.3] basic_ostream::operator<<
  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. // [27.6.2.5.2] arithmetic inserters
  119. /**
  120. * @name Arithmetic 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 Basic arithmetic inserters
  139. * @param A variable of builtin 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. __ostream_type&
  181. operator<<(double __f)
  182. { return _M_insert(__f); }
  183. __ostream_type&
  184. operator<<(float __f)
  185. {
  186. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  187. // 117. basic_ostream uses nonexistent num_put member functions.
  188. return _M_insert(static_cast<double>(__f));
  189. }
  190. __ostream_type&
  191. operator<<(long double __f)
  192. { return _M_insert(__f); }
  193. __ostream_type&
  194. operator<<(const void* __p)
  195. { return _M_insert(__p); }
  196. /**
  197. * @brief Extracting from another streambuf.
  198. * @param sb A pointer to a streambuf
  199. *
  200. * This function behaves like one of the basic arithmetic extractors,
  201. * in that it also constructs a sentry object and has the same error
  202. * handling behavior.
  203. *
  204. * If @a sb is NULL, the stream will set failbit in its error state.
  205. *
  206. * Characters are extracted from @a sb and inserted into @c *this
  207. * until one of the following occurs:
  208. *
  209. * - the input stream reaches end-of-file,
  210. * - insertion into the output sequence fails (in this case, the
  211. * character that would have been inserted is not extracted), or
  212. * - an exception occurs while getting a character from @a sb, which
  213. * sets failbit in the error state
  214. *
  215. * If the function inserts no characters, failbit is set.
  216. */
  217. __ostream_type&
  218. operator<<(__streambuf_type* __sb);
  219. //@}
  220. // [27.6.2.6] unformatted output functions
  221. /**
  222. * @name Unformatted Output Functions
  223. *
  224. * All the unformatted output functions have some common behavior.
  225. * Each starts by constructing a temporary object of type
  226. * std::basic_ostream::sentry. This has several effects, concluding
  227. * with the setting of a status flag; see the sentry documentation
  228. * for more.
  229. *
  230. * If the sentry status is good, the function tries to generate
  231. * whatever data is appropriate for the type of the argument.
  232. *
  233. * If an exception is thrown during insertion, ios_base::badbit
  234. * will be turned on in the stream's error state. If badbit is on in
  235. * the stream's exceptions mask, the exception will be rethrown
  236. * without completing its actions.
  237. */
  238. //@{
  239. /**
  240. * @brief Simple insertion.
  241. * @param c The character to insert.
  242. * @return *this
  243. *
  244. * Tries to insert @a c.
  245. *
  246. * @note This function is not overloaded on signed char and
  247. * unsigned char.
  248. */
  249. __ostream_type&
  250. put(char_type __c);
  251. // Core write functionality, without sentry.
  252. void
  253. _M_write(const char_type* __s, streamsize __n)
  254. {
  255. const streamsize __put = this->rdbuf()->sputn(__s, __n);
  256. if (__put != __n)
  257. this->setstate(ios_base::badbit);
  258. }
  259. /**
  260. * @brief Character string insertion.
  261. * @param s The array to insert.
  262. * @param n Maximum number of characters to insert.
  263. * @return *this
  264. *
  265. * Characters are copied from @a s and inserted into the stream until
  266. * one of the following happens:
  267. *
  268. * - @a n characters are inserted
  269. * - inserting into the output sequence fails (in this case, badbit
  270. * will be set in the stream's error state)
  271. *
  272. * @note This function is not overloaded on signed char and
  273. * unsigned char.
  274. */
  275. __ostream_type&
  276. write(const char_type* __s, streamsize __n);
  277. //@}
  278. /**
  279. * @brief Synchronizing the stream buffer.
  280. * @return *this
  281. *
  282. * If @c rdbuf() is a null pointer, changes nothing.
  283. *
  284. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  285. * sets badbit.
  286. */
  287. __ostream_type&
  288. flush();
  289. // [27.6.2.4] seek members
  290. /**
  291. * @brief Getting the current write position.
  292. * @return A file position object.
  293. *
  294. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  295. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
  296. */
  297. pos_type
  298. tellp();
  299. /**
  300. * @brief Changing the current write position.
  301. * @param pos A file position object.
  302. * @return *this
  303. *
  304. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
  305. * that function fails, sets failbit.
  306. */
  307. __ostream_type&
  308. seekp(pos_type);
  309. /**
  310. * @brief Changing the current write position.
  311. * @param off A file offset object.
  312. * @param dir The direction in which to seek.
  313. * @return *this
  314. *
  315. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
  316. * If that function fails, sets failbit.
  317. */
  318. __ostream_type&
  319. seekp(off_type, ios_base::seekdir);
  320. protected:
  321. basic_ostream()
  322. { this->init(0); }
  323. template<typename _ValueT>
  324. __ostream_type&
  325. _M_insert(_ValueT __v);
  326. };
  327. /**
  328. * @brief Performs setup work for output streams.
  329. *
  330. * Objects of this class are created before all of the standard
  331. * inserters are run. It is responsible for "exception-safe prefix and
  332. * suffix operations."
  333. */
  334. template <typename _CharT, typename _Traits>
  335. class basic_ostream<_CharT, _Traits>::sentry
  336. {
  337. // Data Members:
  338. bool _M_ok;
  339. basic_ostream<_CharT, _Traits>& _M_os;
  340. public:
  341. /**
  342. * @brief The constructor performs preparatory work.
  343. * @param os The output stream to guard.
  344. *
  345. * If the stream state is good (@a os.good() is true), then if the
  346. * stream is tied to another output stream, @c is.tie()->flush()
  347. * is called to synchronize the output sequences.
  348. *
  349. * If the stream state is still good, then the sentry state becomes
  350. * true ("okay").
  351. */
  352. explicit
  353. sentry(basic_ostream<_CharT, _Traits>& __os);
  354. /**
  355. * @brief Possibly flushes the stream.
  356. *
  357. * If @c ios_base::unitbuf is set in @c os.flags(), and
  358. * @c std::uncaught_exception() is true, the sentry destructor calls
  359. * @c flush() on the output stream.
  360. */
  361. ~sentry()
  362. {
  363. // XXX MT
  364. if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
  365. {
  366. // Can't call flush directly or else will get into recursive lock.
  367. if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
  368. _M_os.setstate(ios_base::badbit);
  369. }
  370. }
  371. /**
  372. * @brief Quick status checking.
  373. * @return The sentry state.
  374. *
  375. * For ease of use, sentries may be converted to booleans. The
  376. * return value is that of the sentry state (true == okay).
  377. */
  378. operator bool() const
  379. { return _M_ok; }
  380. };
  381. // [27.6.2.5.4] character insertion templates
  382. //@{
  383. /**
  384. * @brief Character inserters
  385. * @param out An output stream.
  386. * @param c A character.
  387. * @return out
  388. *
  389. * Behaves like one of the formatted arithmetic inserters described in
  390. * std::basic_ostream. After constructing a sentry object with good
  391. * status, this function inserts a single character and any required
  392. * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
  393. * called.
  394. *
  395. * If @a c is of type @c char and the character type of the stream is not
  396. * @c char, the character is widened before insertion.
  397. */
  398. template<typename _CharT, typename _Traits>
  399. inline basic_ostream<_CharT, _Traits>&
  400. operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
  401. { return __ostream_insert(__out, &__c, 1); }
  402. template<typename _CharT, typename _Traits>
  403. inline basic_ostream<_CharT, _Traits>&
  404. operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
  405. { return (__out << __out.widen(__c)); }
  406. // Specialization
  407. template <class _Traits>
  408. inline basic_ostream<char, _Traits>&
  409. operator<<(basic_ostream<char, _Traits>& __out, char __c)
  410. { return __ostream_insert(__out, &__c, 1); }
  411. // Signed and unsigned
  412. template<class _Traits>
  413. inline basic_ostream<char, _Traits>&
  414. operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
  415. { return (__out << static_cast<char>(__c)); }
  416. template<class _Traits>
  417. inline basic_ostream<char, _Traits>&
  418. operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
  419. { return (__out << static_cast<char>(__c)); }
  420. //@}
  421. //@{
  422. /**
  423. * @brief String inserters
  424. * @param out An output stream.
  425. * @param s A character string.
  426. * @return out
  427. * @pre @a s must be a non-NULL pointer
  428. *
  429. * Behaves like one of the formatted arithmetic inserters described in
  430. * std::basic_ostream. After constructing a sentry object with good
  431. * status, this function inserts @c traits::length(s) characters starting
  432. * at @a s, widened if necessary, followed by any required padding (as
  433. * determined by [22.2.2.2.2]). @c out.width(0) is then called.
  434. */
  435. template<typename _CharT, typename _Traits>
  436. inline basic_ostream<_CharT, _Traits>&
  437. operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
  438. {
  439. if (!__s)
  440. __out.setstate(ios_base::badbit);
  441. else
  442. __ostream_insert(__out, __s,
  443. static_cast<streamsize>(_Traits::length(__s)));
  444. return __out;
  445. }
  446. template<typename _CharT, typename _Traits>
  447. basic_ostream<_CharT, _Traits> &
  448. operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
  449. // Partial specializations
  450. template<class _Traits>
  451. inline basic_ostream<char, _Traits>&
  452. operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
  453. {
  454. if (!__s)
  455. __out.setstate(ios_base::badbit);
  456. else
  457. __ostream_insert(__out, __s,
  458. static_cast<streamsize>(_Traits::length(__s)));
  459. return __out;
  460. }
  461. // Signed and unsigned
  462. template<class _Traits>
  463. inline basic_ostream<char, _Traits>&
  464. operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
  465. { return (__out << reinterpret_cast<const char*>(__s)); }
  466. template<class _Traits>
  467. inline basic_ostream<char, _Traits> &
  468. operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
  469. { return (__out << reinterpret_cast<const char*>(__s)); }
  470. //@}
  471. // [27.6.2.7] standard basic_ostream manipulators
  472. /**
  473. * @brief Write a newline and flush the stream.
  474. *
  475. * This manipulator is often mistakenly used when a simple newline is
  476. * desired, leading to poor buffering performance. See
  477. * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
  478. * on this subject.
  479. */
  480. template<typename _CharT, typename _Traits>
  481. inline basic_ostream<_CharT, _Traits>&
  482. endl(basic_ostream<_CharT, _Traits>& __os)
  483. { return flush(__os.put(__os.widen('\n'))); }
  484. /**
  485. * @brief Write a null character into the output sequence.
  486. *
  487. * "Null character" is @c CharT() by definition. For CharT of @c char,
  488. * this correctly writes the ASCII @c NUL character string terminator.
  489. */
  490. template<typename _CharT, typename _Traits>
  491. inline basic_ostream<_CharT, _Traits>&
  492. ends(basic_ostream<_CharT, _Traits>& __os)
  493. { return __os.put(_CharT()); }
  494. /**
  495. * @brief Flushes the output stream.
  496. *
  497. * This manipulator simply calls the stream's @c flush() member function.
  498. */
  499. template<typename _CharT, typename _Traits>
  500. inline basic_ostream<_CharT, _Traits>&
  501. flush(basic_ostream<_CharT, _Traits>& __os)
  502. { return __os.flush(); }
  503. _GLIBCXX_END_NAMESPACE
  504. #ifndef _GLIBCXX_EXPORT_TEMPLATE
  505. # include <bits/ostream.tcc>
  506. #endif
  507. #endif /* _GLIBCXX_OSTREAM */