ostream 32 KB

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