ostream 32 KB

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