| 1 | //===---------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===---------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___OSTREAM_BASIC_OSTREAM_H |
| 10 | #define _LIBCPP___OSTREAM_BASIC_OSTREAM_H |
| 11 | |
| 12 | #include <__config> |
| 13 | |
| 14 | #if _LIBCPP_HAS_LOCALIZATION |
| 15 | |
| 16 | # include <__exception/operations.h> |
| 17 | # include <__fwd/memory.h> |
| 18 | # include <__memory/addressof.h> |
| 19 | # include <__memory/unique_ptr.h> |
| 20 | # include <__new/exceptions.h> |
| 21 | # include <__ostream/put_character_sequence.h> |
| 22 | # include <__system_error/error_code.h> |
| 23 | # include <__type_traits/conjunction.h> |
| 24 | # include <__type_traits/enable_if.h> |
| 25 | # include <__type_traits/is_base_of.h> |
| 26 | # include <__type_traits/void_t.h> |
| 27 | # include <__utility/declval.h> |
| 28 | # include <bitset> |
| 29 | # include <ios> |
| 30 | # include <locale> |
| 31 | # include <streambuf> |
| 32 | # include <string_view> |
| 33 | |
| 34 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 35 | # pragma GCC system_header |
| 36 | # endif |
| 37 | |
| 38 | _LIBCPP_PUSH_MACROS |
| 39 | # include <__undef_macros> |
| 40 | |
| 41 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 42 | |
| 43 | template <class _CharT, class _Traits> |
| 44 | class basic_ostream : virtual public basic_ios<_CharT, _Traits> { |
| 45 | public: |
| 46 | // types (inherited from basic_ios (27.5.4)): |
| 47 | typedef _CharT char_type; |
| 48 | typedef _Traits traits_type; |
| 49 | typedef typename traits_type::int_type int_type; |
| 50 | typedef typename traits_type::pos_type pos_type; |
| 51 | typedef typename traits_type::off_type off_type; |
| 52 | |
| 53 | // 27.7.2.2 Constructor/destructor: |
| 54 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) { |
| 55 | this->init(__sb); |
| 56 | } |
| 57 | ~basic_ostream() override; |
| 58 | |
| 59 | basic_ostream(const basic_ostream& __rhs) = delete; |
| 60 | basic_ostream& operator=(const basic_ostream& __rhs) = delete; |
| 61 | |
| 62 | protected: |
| 63 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs); |
| 64 | |
| 65 | // 27.7.2.3 Assign/swap |
| 66 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs); |
| 67 | |
| 68 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) { |
| 69 | basic_ios<char_type, traits_type>::swap(__rhs); |
| 70 | } |
| 71 | |
| 72 | public: |
| 73 | // 27.7.2.4 Prefix/suffix: |
| 74 | class sentry; |
| 75 | |
| 76 | // 27.7.2.6 Formatted output: |
| 77 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) { |
| 78 | return __pf(*this); |
| 79 | } |
| 80 | |
| 81 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& |
| 82 | operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) { |
| 83 | __pf(*this); |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) { |
| 88 | __pf(*this); |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | template <class _Tp> |
| 93 | _LIBCPP_HIDE_FROM_ABI basic_ostream& __put_num(_Tp __value) { |
| 94 | # if _LIBCPP_HAS_EXCEPTIONS |
| 95 | try { |
| 96 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 97 | sentry __s(*this); |
| 98 | if (__s) { |
| 99 | using _Fp = num_put<char_type, ostreambuf_iterator<char_type, traits_type> >; |
| 100 | const _Fp& __facet = std::use_facet<_Fp>(this->getloc()); |
| 101 | if (__facet.put(*this, *this, this->fill(), __value).failed()) |
| 102 | this->setstate(ios_base::badbit | ios_base::failbit); |
| 103 | } |
| 104 | # if _LIBCPP_HAS_EXCEPTIONS |
| 105 | } catch (...) { |
| 106 | this->__set_badbit_and_consider_rethrow(); |
| 107 | } |
| 108 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 109 | return *this; |
| 110 | } |
| 111 | |
| 112 | template <class _Tp> |
| 113 | _LIBCPP_HIDE_FROM_ABI basic_ostream& __put_num_integer_promote(_Tp __value) { |
| 114 | # if _LIBCPP_HAS_EXCEPTIONS |
| 115 | try { |
| 116 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 117 | sentry __s(*this); |
| 118 | if (__s) { |
| 119 | ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; |
| 120 | |
| 121 | using _Fp = num_put<char_type, ostreambuf_iterator<char_type, traits_type> >; |
| 122 | const _Fp& __facet = std::use_facet<_Fp>(this->getloc()); |
| 123 | if (__facet |
| 124 | .put(*this, |
| 125 | *this, |
| 126 | this->fill(), |
| 127 | __flags == ios_base::oct || __flags == ios_base::hex |
| 128 | ? static_cast<__copy_unsigned_t<_Tp, long> >(std::__to_unsigned_like(__value)) |
| 129 | : static_cast<__copy_unsigned_t<_Tp, long> >(__value)) |
| 130 | .failed()) |
| 131 | this->setstate(ios_base::badbit | ios_base::failbit); |
| 132 | } |
| 133 | # if _LIBCPP_HAS_EXCEPTIONS |
| 134 | } catch (...) { |
| 135 | this->__set_badbit_and_consider_rethrow(); |
| 136 | } |
| 137 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 138 | return *this; |
| 139 | } |
| 140 | |
| 141 | basic_ostream& operator<<(bool __n); |
| 142 | basic_ostream& operator<<(short __n); |
| 143 | basic_ostream& operator<<(unsigned short __n); |
| 144 | basic_ostream& operator<<(int __n); |
| 145 | basic_ostream& operator<<(unsigned int __n); |
| 146 | basic_ostream& operator<<(long __n); |
| 147 | basic_ostream& operator<<(unsigned long __n); |
| 148 | basic_ostream& operator<<(long long __n); |
| 149 | basic_ostream& operator<<(unsigned long long __n); |
| 150 | basic_ostream& operator<<(float __f); |
| 151 | basic_ostream& operator<<(double __f); |
| 152 | basic_ostream& operator<<(long double __f); |
| 153 | basic_ostream& operator<<(const void* __p); |
| 154 | |
| 155 | # if _LIBCPP_STD_VER >= 23 |
| 156 | _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) { |
| 157 | return operator<<(const_cast<const void*>(__p)); |
| 158 | } |
| 159 | # endif |
| 160 | |
| 161 | basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb); |
| 162 | |
| 163 | # if _LIBCPP_STD_VER >= 17 |
| 164 | // LWG 2221 - nullptr. This is not backported to older standards modes. |
| 165 | // See https://reviews.llvm.org/D127033 for more info on the rationale. |
| 166 | _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr" ; } |
| 167 | # endif |
| 168 | |
| 169 | // 27.7.2.7 Unformatted output: |
| 170 | basic_ostream& put(char_type __c); |
| 171 | basic_ostream& write(const char_type* __s, streamsize __n); |
| 172 | basic_ostream& flush(); |
| 173 | |
| 174 | // 27.7.2.5 seeks: |
| 175 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp(); |
| 176 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos); |
| 177 | inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir); |
| 178 | |
| 179 | protected: |
| 180 | _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize |
| 181 | }; |
| 182 | |
| 183 | template <class _CharT, class _Traits> |
| 184 | class basic_ostream<_CharT, _Traits>::sentry { |
| 185 | bool __ok_; |
| 186 | basic_ostream<_CharT, _Traits>& __os_; |
| 187 | |
| 188 | public: |
| 189 | explicit sentry(basic_ostream<_CharT, _Traits>& __os); |
| 190 | ~sentry(); |
| 191 | sentry(const sentry&) = delete; |
| 192 | sentry& operator=(const sentry&) = delete; |
| 193 | |
| 194 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; } |
| 195 | }; |
| 196 | |
| 197 | template <class _CharT, class _Traits> |
| 198 | basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) { |
| 199 | if (__os.good()) { |
| 200 | if (__os.tie()) |
| 201 | __os.tie()->flush(); |
| 202 | __ok_ = true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | template <class _CharT, class _Traits> |
| 207 | basic_ostream<_CharT, _Traits>::sentry::~sentry() { |
| 208 | if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) { |
| 209 | # if _LIBCPP_HAS_EXCEPTIONS |
| 210 | try { |
| 211 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 212 | if (__os_.rdbuf()->pubsync() == -1) |
| 213 | __os_.setstate(ios_base::badbit); |
| 214 | # if _LIBCPP_HAS_EXCEPTIONS |
| 215 | } catch (...) { |
| 216 | } |
| 217 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | template <class _CharT, class _Traits> |
| 222 | basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) { |
| 223 | this->move(__rhs); |
| 224 | } |
| 225 | |
| 226 | template <class _CharT, class _Traits> |
| 227 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) { |
| 228 | swap(__rhs); |
| 229 | return *this; |
| 230 | } |
| 231 | |
| 232 | template <class _CharT, class _Traits> |
| 233 | basic_ostream<_CharT, _Traits>::~basic_ostream() {} |
| 234 | |
| 235 | template <class _CharT, class _Traits> |
| 236 | basic_ostream<_CharT, _Traits>& |
| 237 | basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) { |
| 238 | # if _LIBCPP_HAS_EXCEPTIONS |
| 239 | try { |
| 240 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 241 | sentry __s(*this); |
| 242 | if (__s) { |
| 243 | if (__sb) { |
| 244 | # if _LIBCPP_HAS_EXCEPTIONS |
| 245 | try { |
| 246 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 247 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 248 | typedef ostreambuf_iterator<_CharT, _Traits> _Op; |
| 249 | _Ip __i(__sb); |
| 250 | _Ip __eof; |
| 251 | _Op __o(*this); |
| 252 | size_t __c = 0; |
| 253 | for (; __i != __eof; ++__i, ++__o, ++__c) { |
| 254 | *__o = *__i; |
| 255 | if (__o.failed()) |
| 256 | break; |
| 257 | } |
| 258 | if (__c == 0) |
| 259 | this->setstate(ios_base::failbit); |
| 260 | # if _LIBCPP_HAS_EXCEPTIONS |
| 261 | } catch (...) { |
| 262 | this->__set_failbit_and_consider_rethrow(); |
| 263 | } |
| 264 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 265 | } else |
| 266 | this->setstate(ios_base::badbit); |
| 267 | } |
| 268 | # if _LIBCPP_HAS_EXCEPTIONS |
| 269 | } catch (...) { |
| 270 | this->__set_badbit_and_consider_rethrow(); |
| 271 | } |
| 272 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 273 | return *this; |
| 274 | } |
| 275 | |
| 276 | template <class _CharT, class _Traits> |
| 277 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) { |
| 278 | return __put_num(__n); |
| 279 | } |
| 280 | |
| 281 | template <class _CharT, class _Traits> |
| 282 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) { |
| 283 | return __put_num_integer_promote(__n); |
| 284 | } |
| 285 | |
| 286 | template <class _CharT, class _Traits> |
| 287 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) { |
| 288 | return __put_num_integer_promote(__n); |
| 289 | } |
| 290 | |
| 291 | template <class _CharT, class _Traits> |
| 292 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) { |
| 293 | return __put_num_integer_promote(__n); |
| 294 | } |
| 295 | |
| 296 | template <class _CharT, class _Traits> |
| 297 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) { |
| 298 | return __put_num_integer_promote(__n); |
| 299 | } |
| 300 | |
| 301 | template <class _CharT, class _Traits> |
| 302 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) { |
| 303 | return __put_num(__n); |
| 304 | } |
| 305 | |
| 306 | template <class _CharT, class _Traits> |
| 307 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) { |
| 308 | return __put_num(__n); |
| 309 | } |
| 310 | |
| 311 | template <class _CharT, class _Traits> |
| 312 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) { |
| 313 | return __put_num(__n); |
| 314 | } |
| 315 | |
| 316 | template <class _CharT, class _Traits> |
| 317 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) { |
| 318 | return __put_num(__n); |
| 319 | } |
| 320 | |
| 321 | template <class _CharT, class _Traits> |
| 322 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) { |
| 323 | return *this << static_cast<double>(__n); |
| 324 | } |
| 325 | |
| 326 | template <class _CharT, class _Traits> |
| 327 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) { |
| 328 | return __put_num(__n); |
| 329 | } |
| 330 | |
| 331 | template <class _CharT, class _Traits> |
| 332 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) { |
| 333 | return __put_num(__n); |
| 334 | } |
| 335 | |
| 336 | template <class _CharT, class _Traits> |
| 337 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) { |
| 338 | return __put_num(__n); |
| 339 | } |
| 340 | |
| 341 | template <class _CharT, class _Traits> |
| 342 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) { |
| 343 | return std::__put_character_sequence(__os, std::addressof(__c), 1); |
| 344 | } |
| 345 | |
| 346 | template <class _CharT, class _Traits> |
| 347 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) { |
| 348 | # if _LIBCPP_HAS_EXCEPTIONS |
| 349 | try { |
| 350 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 351 | typename basic_ostream<_CharT, _Traits>::sentry __s(__os); |
| 352 | if (__s) { |
| 353 | _CharT __c = __os.widen(__cn); |
| 354 | typedef ostreambuf_iterator<_CharT, _Traits> _Ip; |
| 355 | if (std::__pad_and_output( |
| 356 | _Ip(__os), |
| 357 | std::addressof(__c), |
| 358 | std::addressof(__c) + (((__os.flags() & ios_base::adjustfield) == ios_base::left) ? 1 : 0), |
| 359 | std::addressof(__c) + 1, |
| 360 | __os, |
| 361 | __os.fill()) |
| 362 | .failed()) |
| 363 | __os.setstate(ios_base::badbit | ios_base::failbit); |
| 364 | } |
| 365 | # if _LIBCPP_HAS_EXCEPTIONS |
| 366 | } catch (...) { |
| 367 | __os.__set_badbit_and_consider_rethrow(); |
| 368 | } |
| 369 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 370 | return __os; |
| 371 | } |
| 372 | |
| 373 | template <class _Traits> |
| 374 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) { |
| 375 | return std::__put_character_sequence(__os, &__c, 1); |
| 376 | } |
| 377 | |
| 378 | template <class _Traits> |
| 379 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) { |
| 380 | return std::__put_character_sequence(__os, (char*)&__c, 1); |
| 381 | } |
| 382 | |
| 383 | template <class _Traits> |
| 384 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) { |
| 385 | return std::__put_character_sequence(__os, (char*)&__c, 1); |
| 386 | } |
| 387 | |
| 388 | template <class _CharT, class _Traits> |
| 389 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 390 | operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) { |
| 391 | return std::__put_character_sequence(__os, __str, _Traits::length(__str)); |
| 392 | } |
| 393 | |
| 394 | template <class _CharT, class _Traits> |
| 395 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 396 | operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) { |
| 397 | # if _LIBCPP_HAS_EXCEPTIONS |
| 398 | try { |
| 399 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 400 | typename basic_ostream<_CharT, _Traits>::sentry __s(__os); |
| 401 | if (__s) { |
| 402 | typedef ostreambuf_iterator<_CharT, _Traits> _Ip; |
| 403 | size_t __len = char_traits<char>::length(s: __strn); |
| 404 | const int __bs = 100; |
| 405 | _CharT __wbb[__bs]; |
| 406 | _CharT* __wb = __wbb; |
| 407 | unique_ptr<_CharT, void (*)(void*)> __h(0, free); |
| 408 | if (__len > __bs) { |
| 409 | __wb = (_CharT*)malloc(size: __len * sizeof(_CharT)); |
| 410 | if (__wb == 0) |
| 411 | std::__throw_bad_alloc(); |
| 412 | __h.reset(__wb); |
| 413 | } |
| 414 | for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p) |
| 415 | *__p = __os.widen(*__strn); |
| 416 | if (std::__pad_and_output( |
| 417 | _Ip(__os), |
| 418 | __wb, |
| 419 | (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb, |
| 420 | __wb + __len, |
| 421 | __os, |
| 422 | __os.fill()) |
| 423 | .failed()) |
| 424 | __os.setstate(ios_base::badbit | ios_base::failbit); |
| 425 | } |
| 426 | # if _LIBCPP_HAS_EXCEPTIONS |
| 427 | } catch (...) { |
| 428 | __os.__set_badbit_and_consider_rethrow(); |
| 429 | } |
| 430 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 431 | return __os; |
| 432 | } |
| 433 | |
| 434 | template <class _Traits> |
| 435 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) { |
| 436 | return std::__put_character_sequence(__os, __str, _Traits::length(__str)); |
| 437 | } |
| 438 | |
| 439 | template <class _Traits> |
| 440 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& |
| 441 | operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) { |
| 442 | const char* __s = (const char*)__str; |
| 443 | return std::__put_character_sequence(__os, __s, _Traits::length(__s)); |
| 444 | } |
| 445 | |
| 446 | template <class _Traits> |
| 447 | _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& |
| 448 | operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) { |
| 449 | const char* __s = (const char*)__str; |
| 450 | return std::__put_character_sequence(__os, __s, _Traits::length(__s)); |
| 451 | } |
| 452 | |
| 453 | template <class _CharT, class _Traits> |
| 454 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) { |
| 455 | # if _LIBCPP_HAS_EXCEPTIONS |
| 456 | try { |
| 457 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 458 | sentry __s(*this); |
| 459 | if (__s) { |
| 460 | typedef ostreambuf_iterator<_CharT, _Traits> _Op; |
| 461 | _Op __o(*this); |
| 462 | *__o = __c; |
| 463 | if (__o.failed()) |
| 464 | this->setstate(ios_base::badbit); |
| 465 | } |
| 466 | # if _LIBCPP_HAS_EXCEPTIONS |
| 467 | } catch (...) { |
| 468 | this->__set_badbit_and_consider_rethrow(); |
| 469 | } |
| 470 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 471 | return *this; |
| 472 | } |
| 473 | |
| 474 | template <class _CharT, class _Traits> |
| 475 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) { |
| 476 | # if _LIBCPP_HAS_EXCEPTIONS |
| 477 | try { |
| 478 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 479 | sentry __sen(*this); |
| 480 | if (__sen && __n) { |
| 481 | if (this->rdbuf()->sputn(__s, __n) != __n) |
| 482 | this->setstate(ios_base::badbit); |
| 483 | } |
| 484 | # if _LIBCPP_HAS_EXCEPTIONS |
| 485 | } catch (...) { |
| 486 | this->__set_badbit_and_consider_rethrow(); |
| 487 | } |
| 488 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 489 | return *this; |
| 490 | } |
| 491 | |
| 492 | template <class _CharT, class _Traits> |
| 493 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() { |
| 494 | # if _LIBCPP_HAS_EXCEPTIONS |
| 495 | try { |
| 496 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 497 | if (this->rdbuf()) { |
| 498 | sentry __s(*this); |
| 499 | if (__s) { |
| 500 | if (this->rdbuf()->pubsync() == -1) |
| 501 | this->setstate(ios_base::badbit); |
| 502 | } |
| 503 | } |
| 504 | # if _LIBCPP_HAS_EXCEPTIONS |
| 505 | } catch (...) { |
| 506 | this->__set_badbit_and_consider_rethrow(); |
| 507 | } |
| 508 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 509 | return *this; |
| 510 | } |
| 511 | |
| 512 | template <class _CharT, class _Traits> |
| 513 | typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() { |
| 514 | if (this->fail()) |
| 515 | return pos_type(-1); |
| 516 | return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); |
| 517 | } |
| 518 | |
| 519 | template <class _CharT, class _Traits> |
| 520 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) { |
| 521 | sentry __s(*this); |
| 522 | if (!this->fail()) { |
| 523 | if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) |
| 524 | this->setstate(ios_base::failbit); |
| 525 | } |
| 526 | return *this; |
| 527 | } |
| 528 | |
| 529 | template <class _CharT, class _Traits> |
| 530 | basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) { |
| 531 | sentry __s(*this); |
| 532 | if (!this->fail()) { |
| 533 | if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) |
| 534 | this->setstate(ios_base::failbit); |
| 535 | } |
| 536 | return *this; |
| 537 | } |
| 538 | |
| 539 | template <class _CharT, class _Traits> |
| 540 | _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) { |
| 541 | __os.put(__os.widen('\n')); |
| 542 | __os.flush(); |
| 543 | return __os; |
| 544 | } |
| 545 | |
| 546 | template <class _CharT, class _Traits> |
| 547 | _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) { |
| 548 | __os.put(_CharT()); |
| 549 | return __os; |
| 550 | } |
| 551 | |
| 552 | template <class _CharT, class _Traits> |
| 553 | _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) { |
| 554 | __os.flush(); |
| 555 | return __os; |
| 556 | } |
| 557 | |
| 558 | template <class _Stream, class _Tp, class = void> |
| 559 | struct __is_ostreamable : false_type {}; |
| 560 | |
| 561 | template <class _Stream, class _Tp> |
| 562 | struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {}; |
| 563 | |
| 564 | template <class _Stream, |
| 565 | class _Tp, |
| 566 | __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0> |
| 567 | _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) { |
| 568 | __os << __x; |
| 569 | return std::move(__os); |
| 570 | } |
| 571 | |
| 572 | template <class _CharT, class _Traits, class _Allocator> |
| 573 | basic_ostream<_CharT, _Traits>& |
| 574 | operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) { |
| 575 | return std::__put_character_sequence(__os, __str.data(), __str.size()); |
| 576 | } |
| 577 | |
| 578 | template <class _CharT, class _Traits> |
| 579 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 580 | operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) { |
| 581 | return std::__put_character_sequence(__os, __sv.data(), __sv.size()); |
| 582 | } |
| 583 | |
| 584 | template <class _CharT, class _Traits> |
| 585 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 586 | operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) { |
| 587 | return __os << __ec.category().name() << ':' << __ec.value(); |
| 588 | } |
| 589 | |
| 590 | template <class _CharT, class _Traits, class _Yp> |
| 591 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 592 | operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) { |
| 593 | return __os << __p.get(); |
| 594 | } |
| 595 | |
| 596 | template < |
| 597 | class _CharT, |
| 598 | class _Traits, |
| 599 | class _Yp, |
| 600 | class _Dp, |
| 601 | __enable_if_t<is_same<void, |
| 602 | __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>() |
| 603 | << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value, |
| 604 | int> = 0> |
| 605 | inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 606 | operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) { |
| 607 | return __os << __p.get(); |
| 608 | } |
| 609 | |
| 610 | template <class _CharT, class _Traits, size_t _Size> |
| 611 | _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& |
| 612 | operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) { |
| 613 | return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'), |
| 614 | std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1')); |
| 615 | } |
| 616 | |
| 617 | # if _LIBCPP_STD_VER >= 20 |
| 618 | |
| 619 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 620 | template <class _Traits> |
| 621 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete; |
| 622 | |
| 623 | template <class _Traits> |
| 624 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete; |
| 625 | |
| 626 | template <class _Traits> |
| 627 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete; |
| 628 | |
| 629 | template <class _Traits> |
| 630 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete; |
| 631 | |
| 632 | template <class _Traits> |
| 633 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete; |
| 634 | |
| 635 | template <class _Traits> |
| 636 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete; |
| 637 | |
| 638 | # endif // _LIBCPP_HAS_WIDE_CHARACTERS |
| 639 | |
| 640 | # if _LIBCPP_HAS_CHAR8_T |
| 641 | template <class _Traits> |
| 642 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete; |
| 643 | |
| 644 | template <class _Traits> |
| 645 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete; |
| 646 | |
| 647 | template <class _Traits> |
| 648 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete; |
| 649 | |
| 650 | template <class _Traits> |
| 651 | basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete; |
| 652 | # endif |
| 653 | |
| 654 | template <class _Traits> |
| 655 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete; |
| 656 | |
| 657 | template <class _Traits> |
| 658 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete; |
| 659 | |
| 660 | template <class _Traits> |
| 661 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete; |
| 662 | |
| 663 | template <class _Traits> |
| 664 | basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete; |
| 665 | |
| 666 | # endif // _LIBCPP_STD_VER >= 20 |
| 667 | |
| 668 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>; |
| 669 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 670 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>; |
| 671 | # endif |
| 672 | |
| 673 | _LIBCPP_END_NAMESPACE_STD |
| 674 | |
| 675 | _LIBCPP_POP_MACROS |
| 676 | |
| 677 | #endif // _LIBCPP_HAS_LOCALIZATION |
| 678 | |
| 679 | #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H |
| 680 | |