ostream 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. // -*- C++ -*-
  2. //===-------------------------- ostream -----------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_OSTREAM
  10. #define _LIBCPP_OSTREAM
  11. /*
  12. ostream synopsis
  13. template <class charT, class traits = char_traits<charT> >
  14. class basic_ostream
  15. : virtual public basic_ios<charT,traits>
  16. {
  17. public:
  18. // types (inherited from basic_ios (27.5.4)):
  19. typedef charT char_type;
  20. typedef traits traits_type;
  21. typedef typename traits_type::int_type int_type;
  22. typedef typename traits_type::pos_type pos_type;
  23. typedef typename traits_type::off_type off_type;
  24. // 27.7.2.2 Constructor/destructor:
  25. explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
  26. basic_ostream(basic_ostream&& rhs);
  27. virtual ~basic_ostream();
  28. // 27.7.2.3 Assign/swap
  29. basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
  30. basic_ostream& operator=(basic_ostream&& rhs);
  31. void swap(basic_ostream& rhs);
  32. // 27.7.2.4 Prefix/suffix:
  33. class sentry;
  34. // 27.7.2.6 Formatted output:
  35. basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
  36. basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT,traits>&));
  37. basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
  38. basic_ostream& operator<<(bool n);
  39. basic_ostream& operator<<(short n);
  40. basic_ostream& operator<<(unsigned short n);
  41. basic_ostream& operator<<(int n);
  42. basic_ostream& operator<<(unsigned int n);
  43. basic_ostream& operator<<(long n);
  44. basic_ostream& operator<<(unsigned long n);
  45. basic_ostream& operator<<(long long n);
  46. basic_ostream& operator<<(unsigned long long n);
  47. basic_ostream& operator<<(float f);
  48. basic_ostream& operator<<(double f);
  49. basic_ostream& operator<<(long double f);
  50. basic_ostream& operator<<(const void* p);
  51. basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb);
  52. basic_ostream& operator<<(nullptr_t);
  53. // 27.7.2.7 Unformatted output:
  54. basic_ostream& put(char_type c);
  55. basic_ostream& write(const char_type* s, streamsize n);
  56. basic_ostream& flush();
  57. // 27.7.2.5 seeks:
  58. pos_type tellp();
  59. basic_ostream& seekp(pos_type);
  60. basic_ostream& seekp(off_type, ios_base::seekdir);
  61. protected:
  62. basic_ostream(const basic_ostream& rhs) = delete;
  63. basic_ostream(basic_ostream&& rhs);
  64. // 27.7.3.3 Assign/swap
  65. basic_ostream& operator=(basic_ostream& rhs) = delete;
  66. basic_ostream& operator=(const basic_ostream&& rhs);
  67. void swap(basic_ostream& rhs);
  68. };
  69. // 27.7.2.6.4 character inserters
  70. template<class charT, class traits>
  71. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
  72. template<class charT, class traits>
  73. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
  74. template<class traits>
  75. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
  76. // signed and unsigned
  77. template<class traits>
  78. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
  79. template<class traits>
  80. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
  81. // NTBS
  82. template<class charT, class traits>
  83. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
  84. template<class charT, class traits>
  85. basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
  86. template<class traits>
  87. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
  88. // signed and unsigned
  89. template<class traits>
  90. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
  91. template<class traits>
  92. basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
  93. // swap:
  94. template <class charT, class traits>
  95. void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
  96. template <class charT, class traits>
  97. basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
  98. template <class charT, class traits>
  99. basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
  100. template <class charT, class traits>
  101. basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
  102. // rvalue stream insertion
  103. template <class charT, class traits, class T>
  104. basic_ostream<charT, traits>&
  105. operator<<(basic_ostream<charT, traits>&& os, const T& x);
  106. } // std
  107. */
  108. #include <__config>
  109. #include <ios>
  110. #include <streambuf>
  111. #include <locale>
  112. #include <iterator>
  113. #include <bitset>
  114. #include <version>
  115. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  116. #pragma GCC system_header
  117. #endif
  118. _LIBCPP_BEGIN_NAMESPACE_STD
  119. template <class _CharT, class _Traits>
  120. class _LIBCPP_TEMPLATE_VIS basic_ostream
  121. : virtual public basic_ios<_CharT, _Traits>
  122. {
  123. public:
  124. // types (inherited from basic_ios (27.5.4)):
  125. typedef _CharT char_type;
  126. typedef _Traits traits_type;
  127. typedef typename traits_type::int_type int_type;
  128. typedef typename traits_type::pos_type pos_type;
  129. typedef typename traits_type::off_type off_type;
  130. // 27.7.2.2 Constructor/destructor:
  131. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  132. explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
  133. { this->init(__sb); }
  134. virtual ~basic_ostream();
  135. protected:
  136. #ifndef _LIBCPP_CXX03_LANG
  137. inline _LIBCPP_INLINE_VISIBILITY
  138. basic_ostream(basic_ostream&& __rhs);
  139. // 27.7.2.3 Assign/swap
  140. inline _LIBCPP_INLINE_VISIBILITY
  141. basic_ostream& operator=(basic_ostream&& __rhs);
  142. #endif
  143. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  144. void swap(basic_ostream& __rhs)
  145. { basic_ios<char_type, traits_type>::swap(__rhs); }
  146. #ifndef _LIBCPP_CXX03_LANG
  147. basic_ostream (const basic_ostream& __rhs) = delete;
  148. basic_ostream& operator=(const basic_ostream& __rhs) = delete;
  149. #else
  150. basic_ostream (const basic_ostream& __rhs); // not defined
  151. basic_ostream& operator=(const basic_ostream& __rhs); // not defined
  152. #endif
  153. public:
  154. // 27.7.2.4 Prefix/suffix:
  155. class _LIBCPP_TEMPLATE_VIS sentry;
  156. // 27.7.2.6 Formatted output:
  157. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  158. basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
  159. { return __pf(*this); }
  160. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  161. basic_ostream& operator<<(basic_ios<char_type, traits_type>&
  162. (*__pf)(basic_ios<char_type,traits_type>&))
  163. { __pf(*this); return *this; }
  164. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  165. basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
  166. { __pf(*this); return *this; }
  167. basic_ostream& operator<<(bool __n);
  168. basic_ostream& operator<<(short __n);
  169. basic_ostream& operator<<(unsigned short __n);
  170. basic_ostream& operator<<(int __n);
  171. basic_ostream& operator<<(unsigned int __n);
  172. basic_ostream& operator<<(long __n);
  173. basic_ostream& operator<<(unsigned long __n);
  174. basic_ostream& operator<<(long long __n);
  175. basic_ostream& operator<<(unsigned long long __n);
  176. basic_ostream& operator<<(float __f);
  177. basic_ostream& operator<<(double __f);
  178. basic_ostream& operator<<(long double __f);
  179. basic_ostream& operator<<(const void* __p);
  180. basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
  181. _LIBCPP_INLINE_VISIBILITY
  182. basic_ostream& operator<<(nullptr_t)
  183. { return *this << "nullptr"; }
  184. // 27.7.2.7 Unformatted output:
  185. basic_ostream& put(char_type __c);
  186. basic_ostream& write(const char_type* __s, streamsize __n);
  187. basic_ostream& flush();
  188. // 27.7.2.5 seeks:
  189. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  190. pos_type tellp();
  191. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  192. basic_ostream& seekp(pos_type __pos);
  193. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  194. basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
  195. protected:
  196. _LIBCPP_INLINE_VISIBILITY
  197. basic_ostream() {} // extension, intentially does not initialize
  198. };
  199. template <class _CharT, class _Traits>
  200. class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
  201. {
  202. bool __ok_;
  203. basic_ostream<_CharT, _Traits>& __os_;
  204. sentry(const sentry&); // = delete;
  205. sentry& operator=(const sentry&); // = delete;
  206. public:
  207. explicit sentry(basic_ostream<_CharT, _Traits>& __os);
  208. ~sentry();
  209. _LIBCPP_INLINE_VISIBILITY
  210. _LIBCPP_EXPLICIT
  211. operator bool() const {return __ok_;}
  212. };
  213. template <class _CharT, class _Traits>
  214. basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
  215. : __ok_(false),
  216. __os_(__os)
  217. {
  218. if (__os.good())
  219. {
  220. if (__os.tie())
  221. __os.tie()->flush();
  222. __ok_ = true;
  223. }
  224. }
  225. template <class _CharT, class _Traits>
  226. basic_ostream<_CharT, _Traits>::sentry::~sentry()
  227. {
  228. if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
  229. && !uncaught_exception())
  230. {
  231. #ifndef _LIBCPP_NO_EXCEPTIONS
  232. try
  233. {
  234. #endif // _LIBCPP_NO_EXCEPTIONS
  235. if (__os_.rdbuf()->pubsync() == -1)
  236. __os_.setstate(ios_base::badbit);
  237. #ifndef _LIBCPP_NO_EXCEPTIONS
  238. }
  239. catch (...)
  240. {
  241. }
  242. #endif // _LIBCPP_NO_EXCEPTIONS
  243. }
  244. }
  245. #ifndef _LIBCPP_CXX03_LANG
  246. template <class _CharT, class _Traits>
  247. basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
  248. {
  249. this->move(__rhs);
  250. }
  251. template <class _CharT, class _Traits>
  252. basic_ostream<_CharT, _Traits>&
  253. basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
  254. {
  255. swap(__rhs);
  256. return *this;
  257. }
  258. #endif // _LIBCPP_CXX03_LANG
  259. template <class _CharT, class _Traits>
  260. basic_ostream<_CharT, _Traits>::~basic_ostream()
  261. {
  262. }
  263. template <class _CharT, class _Traits>
  264. basic_ostream<_CharT, _Traits>&
  265. basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
  266. {
  267. #ifndef _LIBCPP_NO_EXCEPTIONS
  268. try
  269. {
  270. #endif // _LIBCPP_NO_EXCEPTIONS
  271. sentry __s(*this);
  272. if (__s)
  273. {
  274. if (__sb)
  275. {
  276. #ifndef _LIBCPP_NO_EXCEPTIONS
  277. try
  278. {
  279. #endif // _LIBCPP_NO_EXCEPTIONS
  280. typedef istreambuf_iterator<_CharT, _Traits> _Ip;
  281. typedef ostreambuf_iterator<_CharT, _Traits> _Op;
  282. _Ip __i(__sb);
  283. _Ip __eof;
  284. _Op __o(*this);
  285. size_t __c = 0;
  286. for (; __i != __eof; ++__i, ++__o, ++__c)
  287. {
  288. *__o = *__i;
  289. if (__o.failed())
  290. break;
  291. }
  292. if (__c == 0)
  293. this->setstate(ios_base::failbit);
  294. #ifndef _LIBCPP_NO_EXCEPTIONS
  295. }
  296. catch (...)
  297. {
  298. this->__set_failbit_and_consider_rethrow();
  299. }
  300. #endif // _LIBCPP_NO_EXCEPTIONS
  301. }
  302. else
  303. this->setstate(ios_base::badbit);
  304. }
  305. #ifndef _LIBCPP_NO_EXCEPTIONS
  306. }
  307. catch (...)
  308. {
  309. this->__set_badbit_and_consider_rethrow();
  310. }
  311. #endif // _LIBCPP_NO_EXCEPTIONS
  312. return *this;
  313. }
  314. template <class _CharT, class _Traits>
  315. basic_ostream<_CharT, _Traits>&
  316. basic_ostream<_CharT, _Traits>::operator<<(bool __n)
  317. {
  318. #ifndef _LIBCPP_NO_EXCEPTIONS
  319. try
  320. {
  321. #endif // _LIBCPP_NO_EXCEPTIONS
  322. sentry __s(*this);
  323. if (__s)
  324. {
  325. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  326. const _Fp& __f = use_facet<_Fp>(this->getloc());
  327. if (__f.put(*this, *this, this->fill(), __n).failed())
  328. this->setstate(ios_base::badbit | ios_base::failbit);
  329. }
  330. #ifndef _LIBCPP_NO_EXCEPTIONS
  331. }
  332. catch (...)
  333. {
  334. this->__set_badbit_and_consider_rethrow();
  335. }
  336. #endif // _LIBCPP_NO_EXCEPTIONS
  337. return *this;
  338. }
  339. template <class _CharT, class _Traits>
  340. basic_ostream<_CharT, _Traits>&
  341. basic_ostream<_CharT, _Traits>::operator<<(short __n)
  342. {
  343. #ifndef _LIBCPP_NO_EXCEPTIONS
  344. try
  345. {
  346. #endif // _LIBCPP_NO_EXCEPTIONS
  347. sentry __s(*this);
  348. if (__s)
  349. {
  350. ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
  351. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  352. const _Fp& __f = use_facet<_Fp>(this->getloc());
  353. if (__f.put(*this, *this, this->fill(),
  354. __flags == ios_base::oct || __flags == ios_base::hex ?
  355. static_cast<long>(static_cast<unsigned short>(__n)) :
  356. static_cast<long>(__n)).failed())
  357. this->setstate(ios_base::badbit | ios_base::failbit);
  358. }
  359. #ifndef _LIBCPP_NO_EXCEPTIONS
  360. }
  361. catch (...)
  362. {
  363. this->__set_badbit_and_consider_rethrow();
  364. }
  365. #endif // _LIBCPP_NO_EXCEPTIONS
  366. return *this;
  367. }
  368. template <class _CharT, class _Traits>
  369. basic_ostream<_CharT, _Traits>&
  370. basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
  371. {
  372. #ifndef _LIBCPP_NO_EXCEPTIONS
  373. try
  374. {
  375. #endif // _LIBCPP_NO_EXCEPTIONS
  376. sentry __s(*this);
  377. if (__s)
  378. {
  379. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  380. const _Fp& __f = use_facet<_Fp>(this->getloc());
  381. if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
  382. this->setstate(ios_base::badbit | ios_base::failbit);
  383. }
  384. #ifndef _LIBCPP_NO_EXCEPTIONS
  385. }
  386. catch (...)
  387. {
  388. this->__set_badbit_and_consider_rethrow();
  389. }
  390. #endif // _LIBCPP_NO_EXCEPTIONS
  391. return *this;
  392. }
  393. template <class _CharT, class _Traits>
  394. basic_ostream<_CharT, _Traits>&
  395. basic_ostream<_CharT, _Traits>::operator<<(int __n)
  396. {
  397. #ifndef _LIBCPP_NO_EXCEPTIONS
  398. try
  399. {
  400. #endif // _LIBCPP_NO_EXCEPTIONS
  401. sentry __s(*this);
  402. if (__s)
  403. {
  404. ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
  405. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  406. const _Fp& __f = use_facet<_Fp>(this->getloc());
  407. if (__f.put(*this, *this, this->fill(),
  408. __flags == ios_base::oct || __flags == ios_base::hex ?
  409. static_cast<long>(static_cast<unsigned int>(__n)) :
  410. static_cast<long>(__n)).failed())
  411. this->setstate(ios_base::badbit | ios_base::failbit);
  412. }
  413. #ifndef _LIBCPP_NO_EXCEPTIONS
  414. }
  415. catch (...)
  416. {
  417. this->__set_badbit_and_consider_rethrow();
  418. }
  419. #endif // _LIBCPP_NO_EXCEPTIONS
  420. return *this;
  421. }
  422. template <class _CharT, class _Traits>
  423. basic_ostream<_CharT, _Traits>&
  424. basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
  425. {
  426. #ifndef _LIBCPP_NO_EXCEPTIONS
  427. try
  428. {
  429. #endif // _LIBCPP_NO_EXCEPTIONS
  430. sentry __s(*this);
  431. if (__s)
  432. {
  433. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  434. const _Fp& __f = use_facet<_Fp>(this->getloc());
  435. if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
  436. this->setstate(ios_base::badbit | ios_base::failbit);
  437. }
  438. #ifndef _LIBCPP_NO_EXCEPTIONS
  439. }
  440. catch (...)
  441. {
  442. this->__set_badbit_and_consider_rethrow();
  443. }
  444. #endif // _LIBCPP_NO_EXCEPTIONS
  445. return *this;
  446. }
  447. template <class _CharT, class _Traits>
  448. basic_ostream<_CharT, _Traits>&
  449. basic_ostream<_CharT, _Traits>::operator<<(long __n)
  450. {
  451. #ifndef _LIBCPP_NO_EXCEPTIONS
  452. try
  453. {
  454. #endif // _LIBCPP_NO_EXCEPTIONS
  455. sentry __s(*this);
  456. if (__s)
  457. {
  458. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  459. const _Fp& __f = use_facet<_Fp>(this->getloc());
  460. if (__f.put(*this, *this, this->fill(), __n).failed())
  461. this->setstate(ios_base::badbit | ios_base::failbit);
  462. }
  463. #ifndef _LIBCPP_NO_EXCEPTIONS
  464. }
  465. catch (...)
  466. {
  467. this->__set_badbit_and_consider_rethrow();
  468. }
  469. #endif // _LIBCPP_NO_EXCEPTIONS
  470. return *this;
  471. }
  472. template <class _CharT, class _Traits>
  473. basic_ostream<_CharT, _Traits>&
  474. basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
  475. {
  476. #ifndef _LIBCPP_NO_EXCEPTIONS
  477. try
  478. {
  479. #endif // _LIBCPP_NO_EXCEPTIONS
  480. sentry __s(*this);
  481. if (__s)
  482. {
  483. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  484. const _Fp& __f = use_facet<_Fp>(this->getloc());
  485. if (__f.put(*this, *this, this->fill(), __n).failed())
  486. this->setstate(ios_base::badbit | ios_base::failbit);
  487. }
  488. #ifndef _LIBCPP_NO_EXCEPTIONS
  489. }
  490. catch (...)
  491. {
  492. this->__set_badbit_and_consider_rethrow();
  493. }
  494. #endif // _LIBCPP_NO_EXCEPTIONS
  495. return *this;
  496. }
  497. template <class _CharT, class _Traits>
  498. basic_ostream<_CharT, _Traits>&
  499. basic_ostream<_CharT, _Traits>::operator<<(long long __n)
  500. {
  501. #ifndef _LIBCPP_NO_EXCEPTIONS
  502. try
  503. {
  504. #endif // _LIBCPP_NO_EXCEPTIONS
  505. sentry __s(*this);
  506. if (__s)
  507. {
  508. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  509. const _Fp& __f = use_facet<_Fp>(this->getloc());
  510. if (__f.put(*this, *this, this->fill(), __n).failed())
  511. this->setstate(ios_base::badbit | ios_base::failbit);
  512. }
  513. #ifndef _LIBCPP_NO_EXCEPTIONS
  514. }
  515. catch (...)
  516. {
  517. this->__set_badbit_and_consider_rethrow();
  518. }
  519. #endif // _LIBCPP_NO_EXCEPTIONS
  520. return *this;
  521. }
  522. template <class _CharT, class _Traits>
  523. basic_ostream<_CharT, _Traits>&
  524. basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
  525. {
  526. #ifndef _LIBCPP_NO_EXCEPTIONS
  527. try
  528. {
  529. #endif // _LIBCPP_NO_EXCEPTIONS
  530. sentry __s(*this);
  531. if (__s)
  532. {
  533. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  534. const _Fp& __f = use_facet<_Fp>(this->getloc());
  535. if (__f.put(*this, *this, this->fill(), __n).failed())
  536. this->setstate(ios_base::badbit | ios_base::failbit);
  537. }
  538. #ifndef _LIBCPP_NO_EXCEPTIONS
  539. }
  540. catch (...)
  541. {
  542. this->__set_badbit_and_consider_rethrow();
  543. }
  544. #endif // _LIBCPP_NO_EXCEPTIONS
  545. return *this;
  546. }
  547. template <class _CharT, class _Traits>
  548. basic_ostream<_CharT, _Traits>&
  549. basic_ostream<_CharT, _Traits>::operator<<(float __n)
  550. {
  551. #ifndef _LIBCPP_NO_EXCEPTIONS
  552. try
  553. {
  554. #endif // _LIBCPP_NO_EXCEPTIONS
  555. sentry __s(*this);
  556. if (__s)
  557. {
  558. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  559. const _Fp& __f = use_facet<_Fp>(this->getloc());
  560. if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
  561. this->setstate(ios_base::badbit | ios_base::failbit);
  562. }
  563. #ifndef _LIBCPP_NO_EXCEPTIONS
  564. }
  565. catch (...)
  566. {
  567. this->__set_badbit_and_consider_rethrow();
  568. }
  569. #endif // _LIBCPP_NO_EXCEPTIONS
  570. return *this;
  571. }
  572. template <class _CharT, class _Traits>
  573. basic_ostream<_CharT, _Traits>&
  574. basic_ostream<_CharT, _Traits>::operator<<(double __n)
  575. {
  576. #ifndef _LIBCPP_NO_EXCEPTIONS
  577. try
  578. {
  579. #endif // _LIBCPP_NO_EXCEPTIONS
  580. sentry __s(*this);
  581. if (__s)
  582. {
  583. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  584. const _Fp& __f = use_facet<_Fp>(this->getloc());
  585. if (__f.put(*this, *this, this->fill(), __n).failed())
  586. this->setstate(ios_base::badbit | ios_base::failbit);
  587. }
  588. #ifndef _LIBCPP_NO_EXCEPTIONS
  589. }
  590. catch (...)
  591. {
  592. this->__set_badbit_and_consider_rethrow();
  593. }
  594. #endif // _LIBCPP_NO_EXCEPTIONS
  595. return *this;
  596. }
  597. template <class _CharT, class _Traits>
  598. basic_ostream<_CharT, _Traits>&
  599. basic_ostream<_CharT, _Traits>::operator<<(long double __n)
  600. {
  601. #ifndef _LIBCPP_NO_EXCEPTIONS
  602. try
  603. {
  604. #endif // _LIBCPP_NO_EXCEPTIONS
  605. sentry __s(*this);
  606. if (__s)
  607. {
  608. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  609. const _Fp& __f = use_facet<_Fp>(this->getloc());
  610. if (__f.put(*this, *this, this->fill(), __n).failed())
  611. this->setstate(ios_base::badbit | ios_base::failbit);
  612. }
  613. #ifndef _LIBCPP_NO_EXCEPTIONS
  614. }
  615. catch (...)
  616. {
  617. this->__set_badbit_and_consider_rethrow();
  618. }
  619. #endif // _LIBCPP_NO_EXCEPTIONS
  620. return *this;
  621. }
  622. template <class _CharT, class _Traits>
  623. basic_ostream<_CharT, _Traits>&
  624. basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
  625. {
  626. #ifndef _LIBCPP_NO_EXCEPTIONS
  627. try
  628. {
  629. #endif // _LIBCPP_NO_EXCEPTIONS
  630. sentry __s(*this);
  631. if (__s)
  632. {
  633. typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
  634. const _Fp& __f = use_facet<_Fp>(this->getloc());
  635. if (__f.put(*this, *this, this->fill(), __n).failed())
  636. this->setstate(ios_base::badbit | ios_base::failbit);
  637. }
  638. #ifndef _LIBCPP_NO_EXCEPTIONS
  639. }
  640. catch (...)
  641. {
  642. this->__set_badbit_and_consider_rethrow();
  643. }
  644. #endif // _LIBCPP_NO_EXCEPTIONS
  645. return *this;
  646. }
  647. template<class _CharT, class _Traits>
  648. basic_ostream<_CharT, _Traits>&
  649. __put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
  650. const _CharT* __str, size_t __len)
  651. {
  652. #ifndef _LIBCPP_NO_EXCEPTIONS
  653. try
  654. {
  655. #endif // _LIBCPP_NO_EXCEPTIONS
  656. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  657. if (__s)
  658. {
  659. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  660. if (__pad_and_output(_Ip(__os),
  661. __str,
  662. (__os.flags() & ios_base::adjustfield) == ios_base::left ?
  663. __str + __len :
  664. __str,
  665. __str + __len,
  666. __os,
  667. __os.fill()).failed())
  668. __os.setstate(ios_base::badbit | ios_base::failbit);
  669. }
  670. #ifndef _LIBCPP_NO_EXCEPTIONS
  671. }
  672. catch (...)
  673. {
  674. __os.__set_badbit_and_consider_rethrow();
  675. }
  676. #endif // _LIBCPP_NO_EXCEPTIONS
  677. return __os;
  678. }
  679. template<class _CharT, class _Traits>
  680. basic_ostream<_CharT, _Traits>&
  681. operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
  682. {
  683. return _VSTD::__put_character_sequence(__os, &__c, 1);
  684. }
  685. template<class _CharT, class _Traits>
  686. basic_ostream<_CharT, _Traits>&
  687. operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
  688. {
  689. #ifndef _LIBCPP_NO_EXCEPTIONS
  690. try
  691. {
  692. #endif // _LIBCPP_NO_EXCEPTIONS
  693. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  694. if (__s)
  695. {
  696. _CharT __c = __os.widen(__cn);
  697. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  698. if (__pad_and_output(_Ip(__os),
  699. &__c,
  700. (__os.flags() & ios_base::adjustfield) == ios_base::left ?
  701. &__c + 1 :
  702. &__c,
  703. &__c + 1,
  704. __os,
  705. __os.fill()).failed())
  706. __os.setstate(ios_base::badbit | ios_base::failbit);
  707. }
  708. #ifndef _LIBCPP_NO_EXCEPTIONS
  709. }
  710. catch (...)
  711. {
  712. __os.__set_badbit_and_consider_rethrow();
  713. }
  714. #endif // _LIBCPP_NO_EXCEPTIONS
  715. return __os;
  716. }
  717. template<class _Traits>
  718. basic_ostream<char, _Traits>&
  719. operator<<(basic_ostream<char, _Traits>& __os, char __c)
  720. {
  721. return _VSTD::__put_character_sequence(__os, &__c, 1);
  722. }
  723. template<class _Traits>
  724. basic_ostream<char, _Traits>&
  725. operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
  726. {
  727. return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
  728. }
  729. template<class _Traits>
  730. basic_ostream<char, _Traits>&
  731. operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
  732. {
  733. return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
  734. }
  735. template<class _CharT, class _Traits>
  736. basic_ostream<_CharT, _Traits>&
  737. operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
  738. {
  739. return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
  740. }
  741. template<class _CharT, class _Traits>
  742. basic_ostream<_CharT, _Traits>&
  743. operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
  744. {
  745. #ifndef _LIBCPP_NO_EXCEPTIONS
  746. try
  747. {
  748. #endif // _LIBCPP_NO_EXCEPTIONS
  749. typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
  750. if (__s)
  751. {
  752. typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
  753. size_t __len = char_traits<char>::length(__strn);
  754. const int __bs = 100;
  755. _CharT __wbb[__bs];
  756. _CharT* __wb = __wbb;
  757. unique_ptr<_CharT, void(*)(void*)> __h(0, free);
  758. if (__len > __bs)
  759. {
  760. __wb = (_CharT*)malloc(__len*sizeof(_CharT));
  761. if (__wb == 0)
  762. __throw_bad_alloc();
  763. __h.reset(__wb);
  764. }
  765. for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
  766. *__p = __os.widen(*__strn);
  767. if (__pad_and_output(_Ip(__os),
  768. __wb,
  769. (__os.flags() & ios_base::adjustfield) == ios_base::left ?
  770. __wb + __len :
  771. __wb,
  772. __wb + __len,
  773. __os,
  774. __os.fill()).failed())
  775. __os.setstate(ios_base::badbit | ios_base::failbit);
  776. }
  777. #ifndef _LIBCPP_NO_EXCEPTIONS
  778. }
  779. catch (...)
  780. {
  781. __os.__set_badbit_and_consider_rethrow();
  782. }
  783. #endif // _LIBCPP_NO_EXCEPTIONS
  784. return __os;
  785. }
  786. template<class _Traits>
  787. basic_ostream<char, _Traits>&
  788. operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
  789. {
  790. return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
  791. }
  792. template<class _Traits>
  793. basic_ostream<char, _Traits>&
  794. operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
  795. {
  796. const char *__s = (const char *) __str;
  797. return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
  798. }
  799. template<class _Traits>
  800. basic_ostream<char, _Traits>&
  801. operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
  802. {
  803. const char *__s = (const char *) __str;
  804. return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
  805. }
  806. template <class _CharT, class _Traits>
  807. basic_ostream<_CharT, _Traits>&
  808. basic_ostream<_CharT, _Traits>::put(char_type __c)
  809. {
  810. #ifndef _LIBCPP_NO_EXCEPTIONS
  811. try
  812. {
  813. #endif // _LIBCPP_NO_EXCEPTIONS
  814. sentry __s(*this);
  815. if (__s)
  816. {
  817. typedef ostreambuf_iterator<_CharT, _Traits> _Op;
  818. _Op __o(*this);
  819. *__o = __c;
  820. if (__o.failed())
  821. this->setstate(ios_base::badbit);
  822. }
  823. #ifndef _LIBCPP_NO_EXCEPTIONS
  824. }
  825. catch (...)
  826. {
  827. this->__set_badbit_and_consider_rethrow();
  828. }
  829. #endif // _LIBCPP_NO_EXCEPTIONS
  830. return *this;
  831. }
  832. template <class _CharT, class _Traits>
  833. basic_ostream<_CharT, _Traits>&
  834. basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
  835. {
  836. #ifndef _LIBCPP_NO_EXCEPTIONS
  837. try
  838. {
  839. #endif // _LIBCPP_NO_EXCEPTIONS
  840. sentry __sen(*this);
  841. if (__sen && __n)
  842. {
  843. if (this->rdbuf()->sputn(__s, __n) != __n)
  844. this->setstate(ios_base::badbit);
  845. }
  846. #ifndef _LIBCPP_NO_EXCEPTIONS
  847. }
  848. catch (...)
  849. {
  850. this->__set_badbit_and_consider_rethrow();
  851. }
  852. #endif // _LIBCPP_NO_EXCEPTIONS
  853. return *this;
  854. }
  855. template <class _CharT, class _Traits>
  856. basic_ostream<_CharT, _Traits>&
  857. basic_ostream<_CharT, _Traits>::flush()
  858. {
  859. #ifndef _LIBCPP_NO_EXCEPTIONS
  860. try
  861. {
  862. #endif // _LIBCPP_NO_EXCEPTIONS
  863. if (this->rdbuf())
  864. {
  865. sentry __s(*this);
  866. if (__s)
  867. {
  868. if (this->rdbuf()->pubsync() == -1)
  869. this->setstate(ios_base::badbit);
  870. }
  871. }
  872. #ifndef _LIBCPP_NO_EXCEPTIONS
  873. }
  874. catch (...)
  875. {
  876. this->__set_badbit_and_consider_rethrow();
  877. }
  878. #endif // _LIBCPP_NO_EXCEPTIONS
  879. return *this;
  880. }
  881. template <class _CharT, class _Traits>
  882. typename basic_ostream<_CharT, _Traits>::pos_type
  883. basic_ostream<_CharT, _Traits>::tellp()
  884. {
  885. if (this->fail())
  886. return pos_type(-1);
  887. return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
  888. }
  889. template <class _CharT, class _Traits>
  890. basic_ostream<_CharT, _Traits>&
  891. basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
  892. {
  893. sentry __s(*this);
  894. if (!this->fail())
  895. {
  896. if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
  897. this->setstate(ios_base::failbit);
  898. }
  899. return *this;
  900. }
  901. template <class _CharT, class _Traits>
  902. basic_ostream<_CharT, _Traits>&
  903. basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
  904. {
  905. sentry __s(*this);
  906. if (!this->fail())
  907. {
  908. if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
  909. this->setstate(ios_base::failbit);
  910. }
  911. return *this;
  912. }
  913. template <class _CharT, class _Traits>
  914. inline _LIBCPP_INLINE_VISIBILITY
  915. basic_ostream<_CharT, _Traits>&
  916. endl(basic_ostream<_CharT, _Traits>& __os)
  917. {
  918. __os.put(__os.widen('\n'));
  919. __os.flush();
  920. return __os;
  921. }
  922. template <class _CharT, class _Traits>
  923. inline _LIBCPP_INLINE_VISIBILITY
  924. basic_ostream<_CharT, _Traits>&
  925. ends(basic_ostream<_CharT, _Traits>& __os)
  926. {
  927. __os.put(_CharT());
  928. return __os;
  929. }
  930. template <class _CharT, class _Traits>
  931. inline _LIBCPP_INLINE_VISIBILITY
  932. basic_ostream<_CharT, _Traits>&
  933. flush(basic_ostream<_CharT, _Traits>& __os)
  934. {
  935. __os.flush();
  936. return __os;
  937. }
  938. #ifndef _LIBCPP_CXX03_LANG
  939. template <class _Stream, class _Tp>
  940. inline _LIBCPP_INLINE_VISIBILITY
  941. typename enable_if
  942. <
  943. !is_lvalue_reference<_Stream>::value &&
  944. is_base_of<ios_base, _Stream>::value,
  945. _Stream&&
  946. >::type
  947. operator<<(_Stream&& __os, const _Tp& __x)
  948. {
  949. __os << __x;
  950. return _VSTD::move(__os);
  951. }
  952. #endif // _LIBCPP_CXX03_LANG
  953. template<class _CharT, class _Traits, class _Allocator>
  954. basic_ostream<_CharT, _Traits>&
  955. operator<<(basic_ostream<_CharT, _Traits>& __os,
  956. const basic_string<_CharT, _Traits, _Allocator>& __str)
  957. {
  958. return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
  959. }
  960. template<class _CharT, class _Traits>
  961. basic_ostream<_CharT, _Traits>&
  962. operator<<(basic_ostream<_CharT, _Traits>& __os,
  963. basic_string_view<_CharT, _Traits> __sv)
  964. {
  965. return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
  966. }
  967. template <class _CharT, class _Traits>
  968. inline _LIBCPP_INLINE_VISIBILITY
  969. basic_ostream<_CharT, _Traits>&
  970. operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
  971. {
  972. return __os << __ec.category().name() << ':' << __ec.value();
  973. }
  974. template<class _CharT, class _Traits, class _Yp>
  975. inline _LIBCPP_INLINE_VISIBILITY
  976. basic_ostream<_CharT, _Traits>&
  977. operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
  978. {
  979. return __os << __p.get();
  980. }
  981. template<class _CharT, class _Traits, class _Yp, class _Dp>
  982. inline _LIBCPP_INLINE_VISIBILITY
  983. typename enable_if
  984. <
  985. is_same<void, typename __void_t<decltype((declval<basic_ostream<_CharT, _Traits>&>() << declval<typename unique_ptr<_Yp, _Dp>::pointer>()))>::type>::value,
  986. basic_ostream<_CharT, _Traits>&
  987. >::type
  988. operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
  989. {
  990. return __os << __p.get();
  991. }
  992. template <class _CharT, class _Traits, size_t _Size>
  993. basic_ostream<_CharT, _Traits>&
  994. operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
  995. {
  996. return __os << __x.template to_string<_CharT, _Traits>
  997. (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
  998. use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
  999. }
  1000. #ifndef _LIBCPP_DO_NOT_ASSUME_STREAMS_EXPLICIT_INSTANTIATION_IN_DYLIB
  1001. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>)
  1002. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>)
  1003. #endif
  1004. _LIBCPP_END_NAMESPACE_STD
  1005. #endif // _LIBCPP_OSTREAM