ostream 32 KB

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