xref: /freebsd/contrib/llvm-project/libcxx/src/ios.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
981ad6265SDimitry Andric #include <__config>
1081ad6265SDimitry Andric #include <__locale>
1181ad6265SDimitry Andric #include <algorithm>
1281ad6265SDimitry Andric #include <ios>
1381ad6265SDimitry Andric #include <limits>
1481ad6265SDimitry Andric #include <memory>
1581ad6265SDimitry Andric #include <new>
160b57cec5SDimitry Andric #include <stdlib.h>
1781ad6265SDimitry Andric #include <string>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "include/config_elast.h"
2081ad6265SDimitry Andric 
2181ad6265SDimitry Andric _LIBCPP_PUSH_MACROS
2281ad6265SDimitry Andric #include <__undef_macros>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
250b57cec5SDimitry Andric 
26*cb14a3feSDimitry Andric class _LIBCPP_HIDDEN __iostream_category : public __do_message {
270b57cec5SDimitry Andric public:
28fe6060f1SDimitry Andric   virtual const char* name() const noexcept;
290b57cec5SDimitry Andric   virtual string message(int ev) const;
300b57cec5SDimitry Andric };
310b57cec5SDimitry Andric 
32*cb14a3feSDimitry Andric const char* __iostream_category::name() const noexcept { return "iostream"; }
330b57cec5SDimitry Andric 
34*cb14a3feSDimitry Andric string __iostream_category::message(int ev) const {
350b57cec5SDimitry Andric   if (ev != static_cast<int>(io_errc::stream)
360b57cec5SDimitry Andric #ifdef _LIBCPP_ELAST
370b57cec5SDimitry Andric       && ev <= _LIBCPP_ELAST
380b57cec5SDimitry Andric #endif // _LIBCPP_ELAST
390b57cec5SDimitry Andric   )
400b57cec5SDimitry Andric     return __do_message::message(ev);
410b57cec5SDimitry Andric   return string("unspecified iostream_category error");
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
44*cb14a3feSDimitry Andric const error_category& iostream_category() noexcept {
455f757f3fSDimitry Andric   union AvoidDestroyingIostreamCategory {
465f757f3fSDimitry Andric     __iostream_category iostream_error_category;
475f757f3fSDimitry Andric     constexpr explicit AvoidDestroyingIostreamCategory() : iostream_error_category() {}
485f757f3fSDimitry Andric     ~AvoidDestroyingIostreamCategory() {}
495f757f3fSDimitry Andric   };
505f757f3fSDimitry Andric   constinit static AvoidDestroyingIostreamCategory helper;
515f757f3fSDimitry Andric   return helper.iostream_error_category;
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric // ios_base::failure
550b57cec5SDimitry Andric 
56*cb14a3feSDimitry Andric ios_base::failure::failure(const string& msg, const error_code& ec) : system_error(ec, msg) {}
570b57cec5SDimitry Andric 
58*cb14a3feSDimitry Andric ios_base::failure::failure(const char* msg, const error_code& ec) : system_error(ec, msg) {}
590b57cec5SDimitry Andric 
60*cb14a3feSDimitry Andric ios_base::failure::~failure() throw() {}
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric // ios_base locale
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric const ios_base::fmtflags ios_base::boolalpha;
650b57cec5SDimitry Andric const ios_base::fmtflags ios_base::dec;
660b57cec5SDimitry Andric const ios_base::fmtflags ios_base::fixed;
670b57cec5SDimitry Andric const ios_base::fmtflags ios_base::hex;
680b57cec5SDimitry Andric const ios_base::fmtflags ios_base::internal;
690b57cec5SDimitry Andric const ios_base::fmtflags ios_base::left;
700b57cec5SDimitry Andric const ios_base::fmtflags ios_base::oct;
710b57cec5SDimitry Andric const ios_base::fmtflags ios_base::right;
720b57cec5SDimitry Andric const ios_base::fmtflags ios_base::scientific;
730b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showbase;
740b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showpoint;
750b57cec5SDimitry Andric const ios_base::fmtflags ios_base::showpos;
760b57cec5SDimitry Andric const ios_base::fmtflags ios_base::skipws;
770b57cec5SDimitry Andric const ios_base::fmtflags ios_base::unitbuf;
780b57cec5SDimitry Andric const ios_base::fmtflags ios_base::uppercase;
790b57cec5SDimitry Andric const ios_base::fmtflags ios_base::adjustfield;
800b57cec5SDimitry Andric const ios_base::fmtflags ios_base::basefield;
810b57cec5SDimitry Andric const ios_base::fmtflags ios_base::floatfield;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric const ios_base::iostate ios_base::badbit;
840b57cec5SDimitry Andric const ios_base::iostate ios_base::eofbit;
850b57cec5SDimitry Andric const ios_base::iostate ios_base::failbit;
860b57cec5SDimitry Andric const ios_base::iostate ios_base::goodbit;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric const ios_base::openmode ios_base::app;
890b57cec5SDimitry Andric const ios_base::openmode ios_base::ate;
900b57cec5SDimitry Andric const ios_base::openmode ios_base::binary;
910b57cec5SDimitry Andric const ios_base::openmode ios_base::in;
920b57cec5SDimitry Andric const ios_base::openmode ios_base::out;
930b57cec5SDimitry Andric const ios_base::openmode ios_base::trunc;
940b57cec5SDimitry Andric 
95*cb14a3feSDimitry Andric void ios_base::__call_callbacks(event ev) {
96*cb14a3feSDimitry Andric   for (size_t i = __event_size_; i;) {
970b57cec5SDimitry Andric     --i;
980b57cec5SDimitry Andric     __fn_[i](ev, *this, __index_[i]);
990b57cec5SDimitry Andric   }
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric // locale
1030b57cec5SDimitry Andric 
104*cb14a3feSDimitry Andric locale ios_base::imbue(const locale& newloc) {
1050b57cec5SDimitry Andric   static_assert(sizeof(locale) == sizeof(__loc_), "");
1060b57cec5SDimitry Andric   locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
1070b57cec5SDimitry Andric   locale oldloc       = loc_storage;
1080b57cec5SDimitry Andric   loc_storage         = newloc;
1090b57cec5SDimitry Andric   __call_callbacks(imbue_event);
1100b57cec5SDimitry Andric   return oldloc;
1110b57cec5SDimitry Andric }
1120b57cec5SDimitry Andric 
113*cb14a3feSDimitry Andric locale ios_base::getloc() const {
1140b57cec5SDimitry Andric   const locale& loc_storage = *reinterpret_cast<const locale*>(&__loc_);
1150b57cec5SDimitry Andric   return loc_storage;
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric // xalloc
1190b57cec5SDimitry Andric #if defined(_LIBCPP_HAS_C_ATOMIC_IMP) && !defined(_LIBCPP_HAS_NO_THREADS)
1200eae32dcSDimitry Andric atomic<int> ios_base::__xindex_{0};
1210b57cec5SDimitry Andric #else
1220b57cec5SDimitry Andric int ios_base::__xindex_ = 0;
1230b57cec5SDimitry Andric #endif
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric template <typename _Tp>
126*cb14a3feSDimitry Andric static size_t __ios_new_cap(size_t __req_size, size_t __current_cap) { // Precondition: __req_size > __current_cap
1270b57cec5SDimitry Andric   const size_t mx = std::numeric_limits<size_t>::max() / sizeof(_Tp);
1280b57cec5SDimitry Andric   if (__req_size < mx / 2)
1295f757f3fSDimitry Andric     return std::max(2 * __current_cap, __req_size);
1300b57cec5SDimitry Andric   else
1310b57cec5SDimitry Andric     return mx;
1320b57cec5SDimitry Andric }
1330b57cec5SDimitry Andric 
134*cb14a3feSDimitry Andric int ios_base::xalloc() { return __xindex_++; }
1350b57cec5SDimitry Andric 
136*cb14a3feSDimitry Andric long& ios_base::iword(int index) {
1370b57cec5SDimitry Andric   size_t req_size = static_cast<size_t>(index) + 1;
138*cb14a3feSDimitry Andric   if (req_size > __iarray_cap_) {
1390b57cec5SDimitry Andric     size_t newcap = __ios_new_cap<long>(req_size, __iarray_cap_);
1400b57cec5SDimitry Andric     long* iarray  = static_cast<long*>(realloc(__iarray_, newcap * sizeof(long)));
141*cb14a3feSDimitry Andric     if (iarray == 0) {
1420b57cec5SDimitry Andric       setstate(badbit);
1430b57cec5SDimitry Andric       static long error;
1440b57cec5SDimitry Andric       error = 0;
1450b57cec5SDimitry Andric       return error;
1460b57cec5SDimitry Andric     }
1470b57cec5SDimitry Andric     __iarray_ = iarray;
1480b57cec5SDimitry Andric     for (long* p = __iarray_ + __iarray_size_; p < __iarray_ + newcap; ++p)
1490b57cec5SDimitry Andric       *p = 0;
1500b57cec5SDimitry Andric     __iarray_cap_ = newcap;
1510b57cec5SDimitry Andric   }
1520b57cec5SDimitry Andric   __iarray_size_ = max<size_t>(__iarray_size_, req_size);
1530b57cec5SDimitry Andric   return __iarray_[index];
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
156*cb14a3feSDimitry Andric void*& ios_base::pword(int index) {
1570b57cec5SDimitry Andric   size_t req_size = static_cast<size_t>(index) + 1;
158*cb14a3feSDimitry Andric   if (req_size > __parray_cap_) {
1590b57cec5SDimitry Andric     size_t newcap = __ios_new_cap<void*>(req_size, __iarray_cap_);
1600b57cec5SDimitry Andric     void** parray = static_cast<void**>(realloc(__parray_, newcap * sizeof(void*)));
161*cb14a3feSDimitry Andric     if (parray == 0) {
1620b57cec5SDimitry Andric       setstate(badbit);
1630b57cec5SDimitry Andric       static void* error;
1640b57cec5SDimitry Andric       error = 0;
1650b57cec5SDimitry Andric       return error;
1660b57cec5SDimitry Andric     }
1670b57cec5SDimitry Andric     __parray_ = parray;
1680b57cec5SDimitry Andric     for (void** p = __parray_ + __parray_size_; p < __parray_ + newcap; ++p)
1690b57cec5SDimitry Andric       *p = 0;
1700b57cec5SDimitry Andric     __parray_cap_ = newcap;
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric   __parray_size_ = max<size_t>(__parray_size_, req_size);
1730b57cec5SDimitry Andric   return __parray_[index];
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric // register_callback
1770b57cec5SDimitry Andric 
178*cb14a3feSDimitry Andric void ios_base::register_callback(event_callback fn, int index) {
1790b57cec5SDimitry Andric   size_t req_size = __event_size_ + 1;
180*cb14a3feSDimitry Andric   if (req_size > __event_cap_) {
1810b57cec5SDimitry Andric     size_t newcap       = __ios_new_cap<event_callback>(req_size, __event_cap_);
1820b57cec5SDimitry Andric     event_callback* fns = static_cast<event_callback*>(realloc(__fn_, newcap * sizeof(event_callback)));
1830b57cec5SDimitry Andric     if (fns == 0)
1840b57cec5SDimitry Andric       setstate(badbit);
1850b57cec5SDimitry Andric     __fn_      = fns;
1860b57cec5SDimitry Andric     int* indxs = static_cast<int*>(realloc(__index_, newcap * sizeof(int)));
1870b57cec5SDimitry Andric     if (indxs == 0)
1880b57cec5SDimitry Andric       setstate(badbit);
1890b57cec5SDimitry Andric     __index_     = indxs;
1900b57cec5SDimitry Andric     __event_cap_ = newcap;
1910b57cec5SDimitry Andric   }
1920b57cec5SDimitry Andric   __fn_[__event_size_]    = fn;
1930b57cec5SDimitry Andric   __index_[__event_size_] = index;
1940b57cec5SDimitry Andric   ++__event_size_;
1950b57cec5SDimitry Andric }
1960b57cec5SDimitry Andric 
197*cb14a3feSDimitry Andric ios_base::~ios_base() {
1980b57cec5SDimitry Andric   __call_callbacks(erase_event);
1990b57cec5SDimitry Andric   locale& loc_storage = *reinterpret_cast<locale*>(&__loc_);
2000b57cec5SDimitry Andric   loc_storage.~locale();
2010b57cec5SDimitry Andric   free(__fn_);
2020b57cec5SDimitry Andric   free(__index_);
2030b57cec5SDimitry Andric   free(__iarray_);
2040b57cec5SDimitry Andric   free(__parray_);
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric // iostate
2080b57cec5SDimitry Andric 
209*cb14a3feSDimitry Andric void ios_base::clear(iostate state) {
2100b57cec5SDimitry Andric   if (__rdbuf_)
2110b57cec5SDimitry Andric     __rdstate_ = state;
2120b57cec5SDimitry Andric   else
2130b57cec5SDimitry Andric     __rdstate_ = state | badbit;
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric   if (((state | (__rdbuf_ ? goodbit : badbit)) & __exceptions_) != 0)
2160b57cec5SDimitry Andric     __throw_failure("ios_base::clear");
2170b57cec5SDimitry Andric }
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric // init
2200b57cec5SDimitry Andric 
221*cb14a3feSDimitry Andric void ios_base::init(void* sb) {
2220b57cec5SDimitry Andric   __rdbuf_       = sb;
2230b57cec5SDimitry Andric   __rdstate_     = __rdbuf_ ? goodbit : badbit;
2240b57cec5SDimitry Andric   __exceptions_  = goodbit;
2250b57cec5SDimitry Andric   __fmtflags_    = skipws | dec;
2260b57cec5SDimitry Andric   __width_       = 0;
2270b57cec5SDimitry Andric   __precision_   = 6;
2280b57cec5SDimitry Andric   __fn_          = 0;
2290b57cec5SDimitry Andric   __index_       = 0;
2300b57cec5SDimitry Andric   __event_size_  = 0;
2310b57cec5SDimitry Andric   __event_cap_   = 0;
2320b57cec5SDimitry Andric   __iarray_      = 0;
2330b57cec5SDimitry Andric   __iarray_size_ = 0;
2340b57cec5SDimitry Andric   __iarray_cap_  = 0;
2350b57cec5SDimitry Andric   __parray_      = 0;
2360b57cec5SDimitry Andric   __parray_size_ = 0;
2370b57cec5SDimitry Andric   __parray_cap_  = 0;
2380b57cec5SDimitry Andric   ::new (&__loc_) locale;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
241*cb14a3feSDimitry Andric void ios_base::copyfmt(const ios_base& rhs) {
2420b57cec5SDimitry Andric   // If we can't acquire the needed resources, throw bad_alloc (can't set badbit)
2430b57cec5SDimitry Andric   // Don't alter *this until all needed resources are acquired
2440b57cec5SDimitry Andric   unique_ptr<event_callback, void (*)(void*)> new_callbacks(0, free);
2450b57cec5SDimitry Andric   unique_ptr<int, void (*)(void*)> new_ints(0, free);
2460b57cec5SDimitry Andric   unique_ptr<long, void (*)(void*)> new_longs(0, free);
2470b57cec5SDimitry Andric   unique_ptr<void*, void (*)(void*)> new_pointers(0, free);
248*cb14a3feSDimitry Andric   if (__event_cap_ < rhs.__event_size_) {
2490b57cec5SDimitry Andric     size_t newesize = sizeof(event_callback) * rhs.__event_size_;
2500b57cec5SDimitry Andric     new_callbacks.reset(static_cast<event_callback*>(malloc(newesize)));
2510b57cec5SDimitry Andric     if (!new_callbacks)
2520b57cec5SDimitry Andric       __throw_bad_alloc();
2530b57cec5SDimitry Andric 
2540b57cec5SDimitry Andric     size_t newisize = sizeof(int) * rhs.__event_size_;
2550b57cec5SDimitry Andric     new_ints.reset(static_cast<int*>(malloc(newisize)));
2560b57cec5SDimitry Andric     if (!new_ints)
2570b57cec5SDimitry Andric       __throw_bad_alloc();
2580b57cec5SDimitry Andric   }
259*cb14a3feSDimitry Andric   if (__iarray_cap_ < rhs.__iarray_size_) {
2600b57cec5SDimitry Andric     size_t newsize = sizeof(long) * rhs.__iarray_size_;
2610b57cec5SDimitry Andric     new_longs.reset(static_cast<long*>(malloc(newsize)));
2620b57cec5SDimitry Andric     if (!new_longs)
2630b57cec5SDimitry Andric       __throw_bad_alloc();
2640b57cec5SDimitry Andric   }
265*cb14a3feSDimitry Andric   if (__parray_cap_ < rhs.__parray_size_) {
2660b57cec5SDimitry Andric     size_t newsize = sizeof(void*) * rhs.__parray_size_;
2670b57cec5SDimitry Andric     new_pointers.reset(static_cast<void**>(malloc(newsize)));
2680b57cec5SDimitry Andric     if (!new_pointers)
2690b57cec5SDimitry Andric       __throw_bad_alloc();
2700b57cec5SDimitry Andric   }
2710b57cec5SDimitry Andric   // Got everything we need.  Copy everything but __rdstate_, __rdbuf_ and __exceptions_
2720b57cec5SDimitry Andric   __fmtflags_           = rhs.__fmtflags_;
2730b57cec5SDimitry Andric   __precision_          = rhs.__precision_;
2740b57cec5SDimitry Andric   __width_              = rhs.__width_;
2750b57cec5SDimitry Andric   locale& lhs_loc       = *reinterpret_cast<locale*>(&__loc_);
2760b57cec5SDimitry Andric   const locale& rhs_loc = *reinterpret_cast<const locale*>(&rhs.__loc_);
2770b57cec5SDimitry Andric   lhs_loc               = rhs_loc;
278*cb14a3feSDimitry Andric   if (__event_cap_ < rhs.__event_size_) {
2790b57cec5SDimitry Andric     free(__fn_);
2800b57cec5SDimitry Andric     __fn_ = new_callbacks.release();
2810b57cec5SDimitry Andric     free(__index_);
2820b57cec5SDimitry Andric     __index_     = new_ints.release();
2830b57cec5SDimitry Andric     __event_cap_ = rhs.__event_size_;
2840b57cec5SDimitry Andric   }
285*cb14a3feSDimitry Andric   for (__event_size_ = 0; __event_size_ < rhs.__event_size_; ++__event_size_) {
2860b57cec5SDimitry Andric     __fn_[__event_size_]    = rhs.__fn_[__event_size_];
2870b57cec5SDimitry Andric     __index_[__event_size_] = rhs.__index_[__event_size_];
2880b57cec5SDimitry Andric   }
289*cb14a3feSDimitry Andric   if (__iarray_cap_ < rhs.__iarray_size_) {
2900b57cec5SDimitry Andric     free(__iarray_);
2910b57cec5SDimitry Andric     __iarray_     = new_longs.release();
2920b57cec5SDimitry Andric     __iarray_cap_ = rhs.__iarray_size_;
2930b57cec5SDimitry Andric   }
2940b57cec5SDimitry Andric   for (__iarray_size_ = 0; __iarray_size_ < rhs.__iarray_size_; ++__iarray_size_)
2950b57cec5SDimitry Andric     __iarray_[__iarray_size_] = rhs.__iarray_[__iarray_size_];
296*cb14a3feSDimitry Andric   if (__parray_cap_ < rhs.__parray_size_) {
2970b57cec5SDimitry Andric     free(__parray_);
2980b57cec5SDimitry Andric     __parray_     = new_pointers.release();
2990b57cec5SDimitry Andric     __parray_cap_ = rhs.__parray_size_;
3000b57cec5SDimitry Andric   }
3010b57cec5SDimitry Andric   for (__parray_size_ = 0; __parray_size_ < rhs.__parray_size_; ++__parray_size_)
3020b57cec5SDimitry Andric     __parray_[__parray_size_] = rhs.__parray_[__parray_size_];
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric 
305*cb14a3feSDimitry Andric void ios_base::move(ios_base& rhs) {
3060b57cec5SDimitry Andric   // *this is uninitialized
3070b57cec5SDimitry Andric   __fmtflags_     = rhs.__fmtflags_;
3080b57cec5SDimitry Andric   __precision_    = rhs.__precision_;
3090b57cec5SDimitry Andric   __width_        = rhs.__width_;
3100b57cec5SDimitry Andric   __rdstate_      = rhs.__rdstate_;
3110b57cec5SDimitry Andric   __exceptions_   = rhs.__exceptions_;
3120b57cec5SDimitry Andric   __rdbuf_        = 0;
3130b57cec5SDimitry Andric   locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
3140b57cec5SDimitry Andric   ::new (&__loc_) locale(rhs_loc);
3150b57cec5SDimitry Andric   __fn_              = rhs.__fn_;
3160b57cec5SDimitry Andric   rhs.__fn_          = 0;
3170b57cec5SDimitry Andric   __index_           = rhs.__index_;
3180b57cec5SDimitry Andric   rhs.__index_       = 0;
3190b57cec5SDimitry Andric   __event_size_      = rhs.__event_size_;
3200b57cec5SDimitry Andric   rhs.__event_size_  = 0;
3210b57cec5SDimitry Andric   __event_cap_       = rhs.__event_cap_;
3220b57cec5SDimitry Andric   rhs.__event_cap_   = 0;
3230b57cec5SDimitry Andric   __iarray_          = rhs.__iarray_;
3240b57cec5SDimitry Andric   rhs.__iarray_      = 0;
3250b57cec5SDimitry Andric   __iarray_size_     = rhs.__iarray_size_;
3260b57cec5SDimitry Andric   rhs.__iarray_size_ = 0;
3270b57cec5SDimitry Andric   __iarray_cap_      = rhs.__iarray_cap_;
3280b57cec5SDimitry Andric   rhs.__iarray_cap_  = 0;
3290b57cec5SDimitry Andric   __parray_          = rhs.__parray_;
3300b57cec5SDimitry Andric   rhs.__parray_      = 0;
3310b57cec5SDimitry Andric   __parray_size_     = rhs.__parray_size_;
3320b57cec5SDimitry Andric   rhs.__parray_size_ = 0;
3330b57cec5SDimitry Andric   __parray_cap_      = rhs.__parray_cap_;
3340b57cec5SDimitry Andric   rhs.__parray_cap_  = 0;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
337*cb14a3feSDimitry Andric void ios_base::swap(ios_base& rhs) noexcept {
3385f757f3fSDimitry Andric   std::swap(__fmtflags_, rhs.__fmtflags_);
3395f757f3fSDimitry Andric   std::swap(__precision_, rhs.__precision_);
3405f757f3fSDimitry Andric   std::swap(__width_, rhs.__width_);
3415f757f3fSDimitry Andric   std::swap(__rdstate_, rhs.__rdstate_);
3425f757f3fSDimitry Andric   std::swap(__exceptions_, rhs.__exceptions_);
3430b57cec5SDimitry Andric   locale& lhs_loc = *reinterpret_cast<locale*>(&__loc_);
3440b57cec5SDimitry Andric   locale& rhs_loc = *reinterpret_cast<locale*>(&rhs.__loc_);
3455f757f3fSDimitry Andric   std::swap(lhs_loc, rhs_loc);
3465f757f3fSDimitry Andric   std::swap(__fn_, rhs.__fn_);
3475f757f3fSDimitry Andric   std::swap(__index_, rhs.__index_);
3485f757f3fSDimitry Andric   std::swap(__event_size_, rhs.__event_size_);
3495f757f3fSDimitry Andric   std::swap(__event_cap_, rhs.__event_cap_);
3505f757f3fSDimitry Andric   std::swap(__iarray_, rhs.__iarray_);
3515f757f3fSDimitry Andric   std::swap(__iarray_size_, rhs.__iarray_size_);
3525f757f3fSDimitry Andric   std::swap(__iarray_cap_, rhs.__iarray_cap_);
3535f757f3fSDimitry Andric   std::swap(__parray_, rhs.__parray_);
3545f757f3fSDimitry Andric   std::swap(__parray_size_, rhs.__parray_size_);
3555f757f3fSDimitry Andric   std::swap(__parray_cap_, rhs.__parray_cap_);
3560b57cec5SDimitry Andric }
3570b57cec5SDimitry Andric 
358*cb14a3feSDimitry Andric void ios_base::__set_badbit_and_consider_rethrow() {
3590b57cec5SDimitry Andric   __rdstate_ |= badbit;
36006c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
3610b57cec5SDimitry Andric   if (__exceptions_ & badbit)
3620b57cec5SDimitry Andric     throw;
36306c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_EXCEPTIONS
3640b57cec5SDimitry Andric }
3650b57cec5SDimitry Andric 
366*cb14a3feSDimitry Andric void ios_base::__set_failbit_and_consider_rethrow() {
3670b57cec5SDimitry Andric   __rdstate_ |= failbit;
36806c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
3690b57cec5SDimitry Andric   if (__exceptions_ & failbit)
3700b57cec5SDimitry Andric     throw;
37106c3fb27SDimitry Andric #endif // _LIBCPP_HAS_NO_EXCEPTIONS
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric 
374*cb14a3feSDimitry Andric bool ios_base::sync_with_stdio(bool sync) {
3750b57cec5SDimitry Andric   static bool previous_state = true;
3760b57cec5SDimitry Andric   bool r                     = previous_state;
3770b57cec5SDimitry Andric   previous_state             = sync;
3780b57cec5SDimitry Andric   return r;
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric _LIBCPP_END_NAMESPACE_STD
38281ad6265SDimitry Andric 
38381ad6265SDimitry Andric _LIBCPP_POP_MACROS
384