ios_base.h 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. // Iostreams base classes -*- C++ -*-
  2. // Copyright (C) 1997-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/ios_base.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{ios}
  23. */
  24. //
  25. // ISO C++ 14882: 27.4 Iostreams base classes
  26. //
  27. #ifndef _IOS_BASE_H
  28. #define _IOS_BASE_H 1
  29. #pragma GCC system_header
  30. #include <ext/atomicity.h>
  31. #include <bits/localefwd.h>
  32. #include <bits/locale_classes.h>
  33. #if __cplusplus < 201103L
  34. # include <stdexcept>
  35. #else
  36. # include <system_error>
  37. #endif
  38. namespace std _GLIBCXX_VISIBILITY(default)
  39. {
  40. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  41. // The following definitions of bitmask types are enums, not ints,
  42. // as permitted (but not required) in the standard, in order to provide
  43. // better type safety in iostream calls. A side effect is that in C++98
  44. // expressions involving them are not compile-time constants.
  45. enum _Ios_Fmtflags
  46. {
  47. _S_boolalpha = 1L << 0,
  48. _S_dec = 1L << 1,
  49. _S_fixed = 1L << 2,
  50. _S_hex = 1L << 3,
  51. _S_internal = 1L << 4,
  52. _S_left = 1L << 5,
  53. _S_oct = 1L << 6,
  54. _S_right = 1L << 7,
  55. _S_scientific = 1L << 8,
  56. _S_showbase = 1L << 9,
  57. _S_showpoint = 1L << 10,
  58. _S_showpos = 1L << 11,
  59. _S_skipws = 1L << 12,
  60. _S_unitbuf = 1L << 13,
  61. _S_uppercase = 1L << 14,
  62. _S_adjustfield = _S_left | _S_right | _S_internal,
  63. _S_basefield = _S_dec | _S_oct | _S_hex,
  64. _S_floatfield = _S_scientific | _S_fixed,
  65. _S_ios_fmtflags_end = 1L << 16,
  66. _S_ios_fmtflags_max = __INT_MAX__,
  67. _S_ios_fmtflags_min = ~__INT_MAX__
  68. };
  69. inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
  70. operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
  71. { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
  72. inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
  73. operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
  74. { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
  75. inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
  76. operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
  77. { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
  78. inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
  79. operator~(_Ios_Fmtflags __a)
  80. { return _Ios_Fmtflags(~static_cast<int>(__a)); }
  81. inline const _Ios_Fmtflags&
  82. operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
  83. { return __a = __a | __b; }
  84. inline const _Ios_Fmtflags&
  85. operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
  86. { return __a = __a & __b; }
  87. inline const _Ios_Fmtflags&
  88. operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
  89. { return __a = __a ^ __b; }
  90. enum _Ios_Openmode
  91. {
  92. _S_app = 1L << 0,
  93. _S_ate = 1L << 1,
  94. _S_bin = 1L << 2,
  95. _S_in = 1L << 3,
  96. _S_out = 1L << 4,
  97. _S_trunc = 1L << 5,
  98. _S_ios_openmode_end = 1L << 16,
  99. _S_ios_openmode_max = __INT_MAX__,
  100. _S_ios_openmode_min = ~__INT_MAX__
  101. };
  102. inline _GLIBCXX_CONSTEXPR _Ios_Openmode
  103. operator&(_Ios_Openmode __a, _Ios_Openmode __b)
  104. { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
  105. inline _GLIBCXX_CONSTEXPR _Ios_Openmode
  106. operator|(_Ios_Openmode __a, _Ios_Openmode __b)
  107. { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
  108. inline _GLIBCXX_CONSTEXPR _Ios_Openmode
  109. operator^(_Ios_Openmode __a, _Ios_Openmode __b)
  110. { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
  111. inline _GLIBCXX_CONSTEXPR _Ios_Openmode
  112. operator~(_Ios_Openmode __a)
  113. { return _Ios_Openmode(~static_cast<int>(__a)); }
  114. inline const _Ios_Openmode&
  115. operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
  116. { return __a = __a | __b; }
  117. inline const _Ios_Openmode&
  118. operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
  119. { return __a = __a & __b; }
  120. inline const _Ios_Openmode&
  121. operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
  122. { return __a = __a ^ __b; }
  123. enum _Ios_Iostate
  124. {
  125. _S_goodbit = 0,
  126. _S_badbit = 1L << 0,
  127. _S_eofbit = 1L << 1,
  128. _S_failbit = 1L << 2,
  129. _S_ios_iostate_end = 1L << 16,
  130. _S_ios_iostate_max = __INT_MAX__,
  131. _S_ios_iostate_min = ~__INT_MAX__
  132. };
  133. inline _GLIBCXX_CONSTEXPR _Ios_Iostate
  134. operator&(_Ios_Iostate __a, _Ios_Iostate __b)
  135. { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
  136. inline _GLIBCXX_CONSTEXPR _Ios_Iostate
  137. operator|(_Ios_Iostate __a, _Ios_Iostate __b)
  138. { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
  139. inline _GLIBCXX_CONSTEXPR _Ios_Iostate
  140. operator^(_Ios_Iostate __a, _Ios_Iostate __b)
  141. { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
  142. inline _GLIBCXX_CONSTEXPR _Ios_Iostate
  143. operator~(_Ios_Iostate __a)
  144. { return _Ios_Iostate(~static_cast<int>(__a)); }
  145. inline const _Ios_Iostate&
  146. operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
  147. { return __a = __a | __b; }
  148. inline const _Ios_Iostate&
  149. operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
  150. { return __a = __a & __b; }
  151. inline const _Ios_Iostate&
  152. operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
  153. { return __a = __a ^ __b; }
  154. enum _Ios_Seekdir
  155. {
  156. _S_beg = 0,
  157. _S_cur = _GLIBCXX_STDIO_SEEK_CUR,
  158. _S_end = _GLIBCXX_STDIO_SEEK_END,
  159. _S_ios_seekdir_end = 1L << 16
  160. };
  161. #if __cplusplus >= 201103L
  162. /// I/O error code
  163. enum class io_errc { stream = 1 };
  164. template <> struct is_error_code_enum<io_errc> : public true_type { };
  165. const error_category& iostream_category() noexcept;
  166. inline error_code
  167. make_error_code(io_errc __e) noexcept
  168. { return error_code(static_cast<int>(__e), iostream_category()); }
  169. inline error_condition
  170. make_error_condition(io_errc __e) noexcept
  171. { return error_condition(static_cast<int>(__e), iostream_category()); }
  172. #endif
  173. // 27.4.2 Class ios_base
  174. /**
  175. * @brief The base of the I/O class hierarchy.
  176. * @ingroup io
  177. *
  178. * This class defines everything that can be defined about I/O that does
  179. * not depend on the type of characters being input or output. Most
  180. * people will only see @c ios_base when they need to specify the full
  181. * name of the various I/O flags (e.g., the openmodes).
  182. */
  183. class ios_base
  184. {
  185. #if _GLIBCXX_USE_CXX11_ABI
  186. #if __cplusplus < 201103L
  187. // Type that is layout-compatible with std::system_error
  188. struct system_error : std::runtime_error
  189. {
  190. // Type that is layout-compatible with std::error_code
  191. struct error_code
  192. {
  193. error_code() { }
  194. private:
  195. int _M_value;
  196. const void* _M_cat;
  197. } _M_code;
  198. };
  199. #endif
  200. #endif
  201. public:
  202. /**
  203. * @brief These are thrown to indicate problems with io.
  204. * @ingroup exceptions
  205. *
  206. * 27.4.2.1.1 Class ios_base::failure
  207. */
  208. #if _GLIBCXX_USE_CXX11_ABI
  209. class _GLIBCXX_ABI_TAG_CXX11 failure : public system_error
  210. {
  211. public:
  212. explicit
  213. failure(const string& __str);
  214. #if __cplusplus >= 201103L
  215. explicit
  216. failure(const string&, const error_code&);
  217. explicit
  218. failure(const char*, const error_code& = io_errc::stream);
  219. #endif
  220. virtual
  221. ~failure() throw();
  222. virtual const char*
  223. what() const throw();
  224. };
  225. #else
  226. class failure : public exception
  227. {
  228. public:
  229. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  230. // 48. Use of non-existent exception constructor
  231. explicit
  232. failure(const string& __str) throw();
  233. // This declaration is not useless:
  234. // http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Vague-Linkage.html
  235. virtual
  236. ~failure() throw();
  237. virtual const char*
  238. what() const throw();
  239. private:
  240. string _M_msg;
  241. };
  242. #endif
  243. // 27.4.2.1.2 Type ios_base::fmtflags
  244. /**
  245. * @brief This is a bitmask type.
  246. *
  247. * @c @a _Ios_Fmtflags is implementation-defined, but it is valid to
  248. * perform bitwise operations on these values and expect the Right
  249. * Thing to happen. Defined objects of type fmtflags are:
  250. * - boolalpha
  251. * - dec
  252. * - fixed
  253. * - hex
  254. * - internal
  255. * - left
  256. * - oct
  257. * - right
  258. * - scientific
  259. * - showbase
  260. * - showpoint
  261. * - showpos
  262. * - skipws
  263. * - unitbuf
  264. * - uppercase
  265. * - adjustfield
  266. * - basefield
  267. * - floatfield
  268. */
  269. typedef _Ios_Fmtflags fmtflags;
  270. /// Insert/extract @c bool in alphabetic rather than numeric format.
  271. static const fmtflags boolalpha = _S_boolalpha;
  272. /// Converts integer input or generates integer output in decimal base.
  273. static const fmtflags dec = _S_dec;
  274. /// Generate floating-point output in fixed-point notation.
  275. static const fmtflags fixed = _S_fixed;
  276. /// Converts integer input or generates integer output in hexadecimal base.
  277. static const fmtflags hex = _S_hex;
  278. /// Adds fill characters at a designated internal point in certain
  279. /// generated output, or identical to @c right if no such point is
  280. /// designated.
  281. static const fmtflags internal = _S_internal;
  282. /// Adds fill characters on the right (final positions) of certain
  283. /// generated output. (I.e., the thing you print is flush left.)
  284. static const fmtflags left = _S_left;
  285. /// Converts integer input or generates integer output in octal base.
  286. static const fmtflags oct = _S_oct;
  287. /// Adds fill characters on the left (initial positions) of certain
  288. /// generated output. (I.e., the thing you print is flush right.)
  289. static const fmtflags right = _S_right;
  290. /// Generates floating-point output in scientific notation.
  291. static const fmtflags scientific = _S_scientific;
  292. /// Generates a prefix indicating the numeric base of generated integer
  293. /// output.
  294. static const fmtflags showbase = _S_showbase;
  295. /// Generates a decimal-point character unconditionally in generated
  296. /// floating-point output.
  297. static const fmtflags showpoint = _S_showpoint;
  298. /// Generates a + sign in non-negative generated numeric output.
  299. static const fmtflags showpos = _S_showpos;
  300. /// Skips leading white space before certain input operations.
  301. static const fmtflags skipws = _S_skipws;
  302. /// Flushes output after each output operation.
  303. static const fmtflags unitbuf = _S_unitbuf;
  304. /// Replaces certain lowercase letters with their uppercase equivalents
  305. /// in generated output.
  306. static const fmtflags uppercase = _S_uppercase;
  307. /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
  308. static const fmtflags adjustfield = _S_adjustfield;
  309. /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
  310. static const fmtflags basefield = _S_basefield;
  311. /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
  312. static const fmtflags floatfield = _S_floatfield;
  313. // 27.4.2.1.3 Type ios_base::iostate
  314. /**
  315. * @brief This is a bitmask type.
  316. *
  317. * @c @a _Ios_Iostate is implementation-defined, but it is valid to
  318. * perform bitwise operations on these values and expect the Right
  319. * Thing to happen. Defined objects of type iostate are:
  320. * - badbit
  321. * - eofbit
  322. * - failbit
  323. * - goodbit
  324. */
  325. typedef _Ios_Iostate iostate;
  326. /// Indicates a loss of integrity in an input or output sequence (such
  327. /// as an irrecoverable read error from a file).
  328. static const iostate badbit = _S_badbit;
  329. /// Indicates that an input operation reached the end of an input sequence.
  330. static const iostate eofbit = _S_eofbit;
  331. /// Indicates that an input operation failed to read the expected
  332. /// characters, or that an output operation failed to generate the
  333. /// desired characters.
  334. static const iostate failbit = _S_failbit;
  335. /// Indicates all is well.
  336. static const iostate goodbit = _S_goodbit;
  337. // 27.4.2.1.4 Type ios_base::openmode
  338. /**
  339. * @brief This is a bitmask type.
  340. *
  341. * @c @a _Ios_Openmode is implementation-defined, but it is valid to
  342. * perform bitwise operations on these values and expect the Right
  343. * Thing to happen. Defined objects of type openmode are:
  344. * - app
  345. * - ate
  346. * - binary
  347. * - in
  348. * - out
  349. * - trunc
  350. */
  351. typedef _Ios_Openmode openmode;
  352. /// Seek to end before each write.
  353. static const openmode app = _S_app;
  354. /// Open and seek to end immediately after opening.
  355. static const openmode ate = _S_ate;
  356. /// Perform input and output in binary mode (as opposed to text mode).
  357. /// This is probably not what you think it is; see
  358. /// https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
  359. static const openmode binary = _S_bin;
  360. /// Open for input. Default for @c ifstream and fstream.
  361. static const openmode in = _S_in;
  362. /// Open for output. Default for @c ofstream and fstream.
  363. static const openmode out = _S_out;
  364. /// Open for input. Default for @c ofstream.
  365. static const openmode trunc = _S_trunc;
  366. // 27.4.2.1.5 Type ios_base::seekdir
  367. /**
  368. * @brief This is an enumerated type.
  369. *
  370. * @c @a _Ios_Seekdir is implementation-defined. Defined values
  371. * of type seekdir are:
  372. * - beg
  373. * - cur, equivalent to @c SEEK_CUR in the C standard library.
  374. * - end, equivalent to @c SEEK_END in the C standard library.
  375. */
  376. typedef _Ios_Seekdir seekdir;
  377. /// Request a seek relative to the beginning of the stream.
  378. static const seekdir beg = _S_beg;
  379. /// Request a seek relative to the current position within the sequence.
  380. static const seekdir cur = _S_cur;
  381. /// Request a seek relative to the current end of the sequence.
  382. static const seekdir end = _S_end;
  383. #if __cplusplus <= 201402L
  384. // Annex D.6 (removed in C++17)
  385. typedef int io_state;
  386. typedef int open_mode;
  387. typedef int seek_dir;
  388. typedef std::streampos streampos;
  389. typedef std::streamoff streamoff;
  390. #endif
  391. // Callbacks;
  392. /**
  393. * @brief The set of events that may be passed to an event callback.
  394. *
  395. * erase_event is used during ~ios() and copyfmt(). imbue_event is used
  396. * during imbue(). copyfmt_event is used during copyfmt().
  397. */
  398. enum event
  399. {
  400. erase_event,
  401. imbue_event,
  402. copyfmt_event
  403. };
  404. /**
  405. * @brief The type of an event callback function.
  406. * @param __e One of the members of the event enum.
  407. * @param __b Reference to the ios_base object.
  408. * @param __i The integer provided when the callback was registered.
  409. *
  410. * Event callbacks are user defined functions that get called during
  411. * several ios_base and basic_ios functions, specifically imbue(),
  412. * copyfmt(), and ~ios().
  413. */
  414. typedef void (*event_callback) (event __e, ios_base& __b, int __i);
  415. /**
  416. * @brief Add the callback __fn with parameter __index.
  417. * @param __fn The function to add.
  418. * @param __index The integer to pass to the function when invoked.
  419. *
  420. * Registers a function as an event callback with an integer parameter to
  421. * be passed to the function when invoked. Multiple copies of the
  422. * function are allowed. If there are multiple callbacks, they are
  423. * invoked in the order they were registered.
  424. */
  425. void
  426. register_callback(event_callback __fn, int __index);
  427. protected:
  428. streamsize _M_precision;
  429. streamsize _M_width;
  430. fmtflags _M_flags;
  431. iostate _M_exception;
  432. iostate _M_streambuf_state;
  433. // 27.4.2.6 Members for callbacks
  434. // 27.4.2.6 ios_base callbacks
  435. struct _Callback_list
  436. {
  437. // Data Members
  438. _Callback_list* _M_next;
  439. ios_base::event_callback _M_fn;
  440. int _M_index;
  441. _Atomic_word _M_refcount; // 0 means one reference.
  442. _Callback_list(ios_base::event_callback __fn, int __index,
  443. _Callback_list* __cb)
  444. : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
  445. void
  446. _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
  447. // 0 => OK to delete.
  448. int
  449. _M_remove_reference()
  450. {
  451. // Be race-detector-friendly. For more info see bits/c++config.
  452. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
  453. int __res = __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1);
  454. if (__res == 0)
  455. {
  456. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
  457. }
  458. return __res;
  459. }
  460. };
  461. _Callback_list* _M_callbacks;
  462. void
  463. _M_call_callbacks(event __ev) throw();
  464. void
  465. _M_dispose_callbacks(void) throw();
  466. // 27.4.2.5 Members for iword/pword storage
  467. struct _Words
  468. {
  469. void* _M_pword;
  470. long _M_iword;
  471. _Words() : _M_pword(0), _M_iword(0) { }
  472. };
  473. // Only for failed iword/pword calls.
  474. _Words _M_word_zero;
  475. // Guaranteed storage.
  476. // The first 5 iword and pword slots are reserved for internal use.
  477. enum { _S_local_word_size = 8 };
  478. _Words _M_local_word[_S_local_word_size];
  479. // Allocated storage.
  480. int _M_word_size;
  481. _Words* _M_word;
  482. _Words&
  483. _M_grow_words(int __index, bool __iword);
  484. // Members for locale and locale caching.
  485. locale _M_ios_locale;
  486. void
  487. _M_init() throw();
  488. public:
  489. // 27.4.2.1.6 Class ios_base::Init
  490. // Used to initialize standard streams. In theory, g++ could use
  491. // -finit-priority to order this stuff correctly without going
  492. // through these machinations.
  493. class Init
  494. {
  495. friend class ios_base;
  496. public:
  497. Init();
  498. ~Init();
  499. private:
  500. static _Atomic_word _S_refcount;
  501. static bool _S_synced_with_stdio;
  502. };
  503. // [27.4.2.2] fmtflags state functions
  504. /**
  505. * @brief Access to format flags.
  506. * @return The format control flags for both input and output.
  507. */
  508. fmtflags
  509. flags() const
  510. { return _M_flags; }
  511. /**
  512. * @brief Setting new format flags all at once.
  513. * @param __fmtfl The new flags to set.
  514. * @return The previous format control flags.
  515. *
  516. * This function overwrites all the format flags with @a __fmtfl.
  517. */
  518. fmtflags
  519. flags(fmtflags __fmtfl)
  520. {
  521. fmtflags __old = _M_flags;
  522. _M_flags = __fmtfl;
  523. return __old;
  524. }
  525. /**
  526. * @brief Setting new format flags.
  527. * @param __fmtfl Additional flags to set.
  528. * @return The previous format control flags.
  529. *
  530. * This function sets additional flags in format control. Flags that
  531. * were previously set remain set.
  532. */
  533. fmtflags
  534. setf(fmtflags __fmtfl)
  535. {
  536. fmtflags __old = _M_flags;
  537. _M_flags |= __fmtfl;
  538. return __old;
  539. }
  540. /**
  541. * @brief Setting new format flags.
  542. * @param __fmtfl Additional flags to set.
  543. * @param __mask The flags mask for @a fmtfl.
  544. * @return The previous format control flags.
  545. *
  546. * This function clears @a mask in the format flags, then sets
  547. * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
  548. */
  549. fmtflags
  550. setf(fmtflags __fmtfl, fmtflags __mask)
  551. {
  552. fmtflags __old = _M_flags;
  553. _M_flags &= ~__mask;
  554. _M_flags |= (__fmtfl & __mask);
  555. return __old;
  556. }
  557. /**
  558. * @brief Clearing format flags.
  559. * @param __mask The flags to unset.
  560. *
  561. * This function clears @a __mask in the format flags.
  562. */
  563. void
  564. unsetf(fmtflags __mask)
  565. { _M_flags &= ~__mask; }
  566. /**
  567. * @brief Flags access.
  568. * @return The precision to generate on certain output operations.
  569. *
  570. * Be careful if you try to give a definition of @a precision here; see
  571. * DR 189.
  572. */
  573. streamsize
  574. precision() const
  575. { return _M_precision; }
  576. /**
  577. * @brief Changing flags.
  578. * @param __prec The new precision value.
  579. * @return The previous value of precision().
  580. */
  581. streamsize
  582. precision(streamsize __prec)
  583. {
  584. streamsize __old = _M_precision;
  585. _M_precision = __prec;
  586. return __old;
  587. }
  588. /**
  589. * @brief Flags access.
  590. * @return The minimum field width to generate on output operations.
  591. *
  592. * <em>Minimum field width</em> refers to the number of characters.
  593. */
  594. streamsize
  595. width() const
  596. { return _M_width; }
  597. /**
  598. * @brief Changing flags.
  599. * @param __wide The new width value.
  600. * @return The previous value of width().
  601. */
  602. streamsize
  603. width(streamsize __wide)
  604. {
  605. streamsize __old = _M_width;
  606. _M_width = __wide;
  607. return __old;
  608. }
  609. // [27.4.2.4] ios_base static members
  610. /**
  611. * @brief Interaction with the standard C I/O objects.
  612. * @param __sync Whether to synchronize or not.
  613. * @return True if the standard streams were previously synchronized.
  614. *
  615. * The synchronization referred to is @e only that between the standard
  616. * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
  617. * cout). User-declared streams are unaffected. See
  618. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
  619. */
  620. static bool
  621. sync_with_stdio(bool __sync = true);
  622. // [27.4.2.3] ios_base locale functions
  623. /**
  624. * @brief Setting a new locale.
  625. * @param __loc The new locale.
  626. * @return The previous locale.
  627. *
  628. * Sets the new locale for this stream, and then invokes each callback
  629. * with imbue_event.
  630. */
  631. locale
  632. imbue(const locale& __loc) throw();
  633. /**
  634. * @brief Locale access
  635. * @return A copy of the current locale.
  636. *
  637. * If @c imbue(loc) has previously been called, then this function
  638. * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
  639. * the global C++ locale.
  640. */
  641. locale
  642. getloc() const
  643. { return _M_ios_locale; }
  644. /**
  645. * @brief Locale access
  646. * @return A reference to the current locale.
  647. *
  648. * Like getloc above, but returns a reference instead of
  649. * generating a copy.
  650. */
  651. const locale&
  652. _M_getloc() const
  653. { return _M_ios_locale; }
  654. // [27.4.2.5] ios_base storage functions
  655. /**
  656. * @brief Access to unique indices.
  657. * @return An integer different from all previous calls.
  658. *
  659. * This function returns a unique integer every time it is called. It
  660. * can be used for any purpose, but is primarily intended to be a unique
  661. * index for the iword and pword functions. The expectation is that an
  662. * application calls xalloc in order to obtain an index in the iword and
  663. * pword arrays that can be used without fear of conflict.
  664. *
  665. * The implementation maintains a static variable that is incremented and
  666. * returned on each invocation. xalloc is guaranteed to return an index
  667. * that is safe to use in the iword and pword arrays.
  668. */
  669. static int
  670. xalloc() throw();
  671. /**
  672. * @brief Access to integer array.
  673. * @param __ix Index into the array.
  674. * @return A reference to an integer associated with the index.
  675. *
  676. * The iword function provides access to an array of integers that can be
  677. * used for any purpose. The array grows as required to hold the
  678. * supplied index. All integers in the array are initialized to 0.
  679. *
  680. * The implementation reserves several indices. You should use xalloc to
  681. * obtain an index that is safe to use. Also note that since the array
  682. * can grow dynamically, it is not safe to hold onto the reference.
  683. */
  684. long&
  685. iword(int __ix)
  686. {
  687. _Words& __word = (__ix < _M_word_size)
  688. ? _M_word[__ix] : _M_grow_words(__ix, true);
  689. return __word._M_iword;
  690. }
  691. /**
  692. * @brief Access to void pointer array.
  693. * @param __ix Index into the array.
  694. * @return A reference to a void* associated with the index.
  695. *
  696. * The pword function provides access to an array of pointers that can be
  697. * used for any purpose. The array grows as required to hold the
  698. * supplied index. All pointers in the array are initialized to 0.
  699. *
  700. * The implementation reserves several indices. You should use xalloc to
  701. * obtain an index that is safe to use. Also note that since the array
  702. * can grow dynamically, it is not safe to hold onto the reference.
  703. */
  704. void*&
  705. pword(int __ix)
  706. {
  707. _Words& __word = (__ix < _M_word_size)
  708. ? _M_word[__ix] : _M_grow_words(__ix, false);
  709. return __word._M_pword;
  710. }
  711. // Destructor
  712. /**
  713. * Invokes each callback with erase_event. Destroys local storage.
  714. *
  715. * Note that the ios_base object for the standard streams never gets
  716. * destroyed. As a result, any callbacks registered with the standard
  717. * streams will not get invoked with erase_event (unless copyfmt is
  718. * used).
  719. */
  720. virtual ~ios_base();
  721. protected:
  722. ios_base() throw ();
  723. #if __cplusplus < 201103L
  724. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  725. // 50. Copy constructor and assignment operator of ios_base
  726. private:
  727. ios_base(const ios_base&);
  728. ios_base&
  729. operator=(const ios_base&);
  730. #else
  731. public:
  732. ios_base(const ios_base&) = delete;
  733. ios_base&
  734. operator=(const ios_base&) = delete;
  735. protected:
  736. void
  737. _M_move(ios_base&) noexcept;
  738. void
  739. _M_swap(ios_base& __rhs) noexcept;
  740. #endif
  741. };
  742. // [27.4.5.1] fmtflags manipulators
  743. /// Calls base.setf(ios_base::boolalpha).
  744. inline ios_base&
  745. boolalpha(ios_base& __base)
  746. {
  747. __base.setf(ios_base::boolalpha);
  748. return __base;
  749. }
  750. /// Calls base.unsetf(ios_base::boolalpha).
  751. inline ios_base&
  752. noboolalpha(ios_base& __base)
  753. {
  754. __base.unsetf(ios_base::boolalpha);
  755. return __base;
  756. }
  757. /// Calls base.setf(ios_base::showbase).
  758. inline ios_base&
  759. showbase(ios_base& __base)
  760. {
  761. __base.setf(ios_base::showbase);
  762. return __base;
  763. }
  764. /// Calls base.unsetf(ios_base::showbase).
  765. inline ios_base&
  766. noshowbase(ios_base& __base)
  767. {
  768. __base.unsetf(ios_base::showbase);
  769. return __base;
  770. }
  771. /// Calls base.setf(ios_base::showpoint).
  772. inline ios_base&
  773. showpoint(ios_base& __base)
  774. {
  775. __base.setf(ios_base::showpoint);
  776. return __base;
  777. }
  778. /// Calls base.unsetf(ios_base::showpoint).
  779. inline ios_base&
  780. noshowpoint(ios_base& __base)
  781. {
  782. __base.unsetf(ios_base::showpoint);
  783. return __base;
  784. }
  785. /// Calls base.setf(ios_base::showpos).
  786. inline ios_base&
  787. showpos(ios_base& __base)
  788. {
  789. __base.setf(ios_base::showpos);
  790. return __base;
  791. }
  792. /// Calls base.unsetf(ios_base::showpos).
  793. inline ios_base&
  794. noshowpos(ios_base& __base)
  795. {
  796. __base.unsetf(ios_base::showpos);
  797. return __base;
  798. }
  799. /// Calls base.setf(ios_base::skipws).
  800. inline ios_base&
  801. skipws(ios_base& __base)
  802. {
  803. __base.setf(ios_base::skipws);
  804. return __base;
  805. }
  806. /// Calls base.unsetf(ios_base::skipws).
  807. inline ios_base&
  808. noskipws(ios_base& __base)
  809. {
  810. __base.unsetf(ios_base::skipws);
  811. return __base;
  812. }
  813. /// Calls base.setf(ios_base::uppercase).
  814. inline ios_base&
  815. uppercase(ios_base& __base)
  816. {
  817. __base.setf(ios_base::uppercase);
  818. return __base;
  819. }
  820. /// Calls base.unsetf(ios_base::uppercase).
  821. inline ios_base&
  822. nouppercase(ios_base& __base)
  823. {
  824. __base.unsetf(ios_base::uppercase);
  825. return __base;
  826. }
  827. /// Calls base.setf(ios_base::unitbuf).
  828. inline ios_base&
  829. unitbuf(ios_base& __base)
  830. {
  831. __base.setf(ios_base::unitbuf);
  832. return __base;
  833. }
  834. /// Calls base.unsetf(ios_base::unitbuf).
  835. inline ios_base&
  836. nounitbuf(ios_base& __base)
  837. {
  838. __base.unsetf(ios_base::unitbuf);
  839. return __base;
  840. }
  841. // [27.4.5.2] adjustfield manipulators
  842. /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
  843. inline ios_base&
  844. internal(ios_base& __base)
  845. {
  846. __base.setf(ios_base::internal, ios_base::adjustfield);
  847. return __base;
  848. }
  849. /// Calls base.setf(ios_base::left, ios_base::adjustfield).
  850. inline ios_base&
  851. left(ios_base& __base)
  852. {
  853. __base.setf(ios_base::left, ios_base::adjustfield);
  854. return __base;
  855. }
  856. /// Calls base.setf(ios_base::right, ios_base::adjustfield).
  857. inline ios_base&
  858. right(ios_base& __base)
  859. {
  860. __base.setf(ios_base::right, ios_base::adjustfield);
  861. return __base;
  862. }
  863. // [27.4.5.3] basefield manipulators
  864. /// Calls base.setf(ios_base::dec, ios_base::basefield).
  865. inline ios_base&
  866. dec(ios_base& __base)
  867. {
  868. __base.setf(ios_base::dec, ios_base::basefield);
  869. return __base;
  870. }
  871. /// Calls base.setf(ios_base::hex, ios_base::basefield).
  872. inline ios_base&
  873. hex(ios_base& __base)
  874. {
  875. __base.setf(ios_base::hex, ios_base::basefield);
  876. return __base;
  877. }
  878. /// Calls base.setf(ios_base::oct, ios_base::basefield).
  879. inline ios_base&
  880. oct(ios_base& __base)
  881. {
  882. __base.setf(ios_base::oct, ios_base::basefield);
  883. return __base;
  884. }
  885. // [27.4.5.4] floatfield manipulators
  886. /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
  887. inline ios_base&
  888. fixed(ios_base& __base)
  889. {
  890. __base.setf(ios_base::fixed, ios_base::floatfield);
  891. return __base;
  892. }
  893. /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
  894. inline ios_base&
  895. scientific(ios_base& __base)
  896. {
  897. __base.setf(ios_base::scientific, ios_base::floatfield);
  898. return __base;
  899. }
  900. #if __cplusplus >= 201103L
  901. // New C++11 floatfield manipulators
  902. /// Calls
  903. /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
  904. inline ios_base&
  905. hexfloat(ios_base& __base)
  906. {
  907. __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
  908. return __base;
  909. }
  910. /// Calls @c base.unsetf(ios_base::floatfield)
  911. inline ios_base&
  912. defaultfloat(ios_base& __base)
  913. {
  914. __base.unsetf(ios_base::floatfield);
  915. return __base;
  916. }
  917. #endif
  918. _GLIBCXX_END_NAMESPACE_VERSION
  919. } // namespace
  920. #endif /* _IOS_BASE_H */