You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1110 lines
31KB

  1. // Iostreams base classes -*- C++ -*-
  2. // Copyright (C) 1997-2020 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. #if __cplusplus >= 201103L
  240. // Define the new members required by C++11,
  241. // even though the error_code cannot be stored.
  242. explicit
  243. failure(const string& __s, const error_code&) noexcept
  244. : failure(__s)
  245. { }
  246. explicit
  247. failure(const char* __s, const error_code& = error_code{})
  248. : failure(string(__s))
  249. { }
  250. // Stand-in for system_error::code() but returning by value.
  251. error_code code() const noexcept { return error_code{}; }
  252. #endif
  253. private:
  254. string _M_msg;
  255. };
  256. #endif
  257. // 27.4.2.1.2 Type ios_base::fmtflags
  258. /**
  259. * @brief This is a bitmask type.
  260. *
  261. * @c @a _Ios_Fmtflags is implementation-defined, but it is valid to
  262. * perform bitwise operations on these values and expect the Right
  263. * Thing to happen. Defined objects of type fmtflags are:
  264. * - boolalpha
  265. * - dec
  266. * - fixed
  267. * - hex
  268. * - internal
  269. * - left
  270. * - oct
  271. * - right
  272. * - scientific
  273. * - showbase
  274. * - showpoint
  275. * - showpos
  276. * - skipws
  277. * - unitbuf
  278. * - uppercase
  279. * - adjustfield
  280. * - basefield
  281. * - floatfield
  282. */
  283. typedef _Ios_Fmtflags fmtflags;
  284. /// Insert/extract @c bool in alphabetic rather than numeric format.
  285. static const fmtflags boolalpha = _S_boolalpha;
  286. /// Converts integer input or generates integer output in decimal base.
  287. static const fmtflags dec = _S_dec;
  288. /// Generate floating-point output in fixed-point notation.
  289. static const fmtflags fixed = _S_fixed;
  290. /// Converts integer input or generates integer output in hexadecimal base.
  291. static const fmtflags hex = _S_hex;
  292. /// Adds fill characters at a designated internal point in certain
  293. /// generated output, or identical to @c right if no such point is
  294. /// designated.
  295. static const fmtflags internal = _S_internal;
  296. /// Adds fill characters on the right (final positions) of certain
  297. /// generated output. (I.e., the thing you print is flush left.)
  298. static const fmtflags left = _S_left;
  299. /// Converts integer input or generates integer output in octal base.
  300. static const fmtflags oct = _S_oct;
  301. /// Adds fill characters on the left (initial positions) of certain
  302. /// generated output. (I.e., the thing you print is flush right.)
  303. static const fmtflags right = _S_right;
  304. /// Generates floating-point output in scientific notation.
  305. static const fmtflags scientific = _S_scientific;
  306. /// Generates a prefix indicating the numeric base of generated integer
  307. /// output.
  308. static const fmtflags showbase = _S_showbase;
  309. /// Generates a decimal-point character unconditionally in generated
  310. /// floating-point output.
  311. static const fmtflags showpoint = _S_showpoint;
  312. /// Generates a + sign in non-negative generated numeric output.
  313. static const fmtflags showpos = _S_showpos;
  314. /// Skips leading white space before certain input operations.
  315. static const fmtflags skipws = _S_skipws;
  316. /// Flushes output after each output operation.
  317. static const fmtflags unitbuf = _S_unitbuf;
  318. /// Replaces certain lowercase letters with their uppercase equivalents
  319. /// in generated output.
  320. static const fmtflags uppercase = _S_uppercase;
  321. /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
  322. static const fmtflags adjustfield = _S_adjustfield;
  323. /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
  324. static const fmtflags basefield = _S_basefield;
  325. /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
  326. static const fmtflags floatfield = _S_floatfield;
  327. // 27.4.2.1.3 Type ios_base::iostate
  328. /**
  329. * @brief This is a bitmask type.
  330. *
  331. * @c @a _Ios_Iostate is implementation-defined, but it is valid to
  332. * perform bitwise operations on these values and expect the Right
  333. * Thing to happen. Defined objects of type iostate are:
  334. * - badbit
  335. * - eofbit
  336. * - failbit
  337. * - goodbit
  338. */
  339. typedef _Ios_Iostate iostate;
  340. /// Indicates a loss of integrity in an input or output sequence (such
  341. /// as an irrecoverable read error from a file).
  342. static const iostate badbit = _S_badbit;
  343. /// Indicates that an input operation reached the end of an input sequence.
  344. static const iostate eofbit = _S_eofbit;
  345. /// Indicates that an input operation failed to read the expected
  346. /// characters, or that an output operation failed to generate the
  347. /// desired characters.
  348. static const iostate failbit = _S_failbit;
  349. /// Indicates all is well.
  350. static const iostate goodbit = _S_goodbit;
  351. // 27.4.2.1.4 Type ios_base::openmode
  352. /**
  353. * @brief This is a bitmask type.
  354. *
  355. * @c @a _Ios_Openmode is implementation-defined, but it is valid to
  356. * perform bitwise operations on these values and expect the Right
  357. * Thing to happen. Defined objects of type openmode are:
  358. * - app
  359. * - ate
  360. * - binary
  361. * - in
  362. * - out
  363. * - trunc
  364. */
  365. typedef _Ios_Openmode openmode;
  366. /// Seek to end before each write.
  367. static const openmode app = _S_app;
  368. /// Open and seek to end immediately after opening.
  369. static const openmode ate = _S_ate;
  370. /// Perform input and output in binary mode (as opposed to text mode).
  371. /// This is probably not what you think it is; see
  372. /// https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
  373. static const openmode binary = _S_bin;
  374. /// Open for input. Default for @c ifstream and fstream.
  375. static const openmode in = _S_in;
  376. /// Open for output. Default for @c ofstream and fstream.
  377. static const openmode out = _S_out;
  378. /// Truncate an existing stream when opening. Default for @c ofstream.
  379. static const openmode trunc = _S_trunc;
  380. // 27.4.2.1.5 Type ios_base::seekdir
  381. /**
  382. * @brief This is an enumerated type.
  383. *
  384. * @c @a _Ios_Seekdir is implementation-defined. Defined values
  385. * of type seekdir are:
  386. * - beg
  387. * - cur, equivalent to @c SEEK_CUR in the C standard library.
  388. * - end, equivalent to @c SEEK_END in the C standard library.
  389. */
  390. typedef _Ios_Seekdir seekdir;
  391. /// Request a seek relative to the beginning of the stream.
  392. static const seekdir beg = _S_beg;
  393. /// Request a seek relative to the current position within the sequence.
  394. static const seekdir cur = _S_cur;
  395. /// Request a seek relative to the current end of the sequence.
  396. static const seekdir end = _S_end;
  397. #if __cplusplus <= 201402L
  398. // Annex D.6 (removed in C++17)
  399. typedef int io_state
  400. _GLIBCXX_DEPRECATED_SUGGEST("std::iostate");
  401. typedef int open_mode
  402. _GLIBCXX_DEPRECATED_SUGGEST("std::openmode");
  403. typedef int seek_dir
  404. _GLIBCXX_DEPRECATED_SUGGEST("std::seekdir");
  405. typedef std::streampos streampos
  406. _GLIBCXX_DEPRECATED_SUGGEST("std::streampos");
  407. typedef std::streamoff streamoff
  408. _GLIBCXX_DEPRECATED_SUGGEST("std::streamoff");
  409. #endif
  410. // Callbacks;
  411. /**
  412. * @brief The set of events that may be passed to an event callback.
  413. *
  414. * erase_event is used during ~ios() and copyfmt(). imbue_event is used
  415. * during imbue(). copyfmt_event is used during copyfmt().
  416. */
  417. enum event
  418. {
  419. erase_event,
  420. imbue_event,
  421. copyfmt_event
  422. };
  423. /**
  424. * @brief The type of an event callback function.
  425. * @param __e One of the members of the event enum.
  426. * @param __b Reference to the ios_base object.
  427. * @param __i The integer provided when the callback was registered.
  428. *
  429. * Event callbacks are user defined functions that get called during
  430. * several ios_base and basic_ios functions, specifically imbue(),
  431. * copyfmt(), and ~ios().
  432. */
  433. typedef void (*event_callback) (event __e, ios_base& __b, int __i);
  434. /**
  435. * @brief Add the callback __fn with parameter __index.
  436. * @param __fn The function to add.
  437. * @param __index The integer to pass to the function when invoked.
  438. *
  439. * Registers a function as an event callback with an integer parameter to
  440. * be passed to the function when invoked. Multiple copies of the
  441. * function are allowed. If there are multiple callbacks, they are
  442. * invoked in the order they were registered.
  443. */
  444. void
  445. register_callback(event_callback __fn, int __index);
  446. protected:
  447. streamsize _M_precision;
  448. streamsize _M_width;
  449. fmtflags _M_flags;
  450. iostate _M_exception;
  451. iostate _M_streambuf_state;
  452. // 27.4.2.6 Members for callbacks
  453. // 27.4.2.6 ios_base callbacks
  454. struct _Callback_list
  455. {
  456. // Data Members
  457. _Callback_list* _M_next;
  458. ios_base::event_callback _M_fn;
  459. int _M_index;
  460. _Atomic_word _M_refcount; // 0 means one reference.
  461. _Callback_list(ios_base::event_callback __fn, int __index,
  462. _Callback_list* __cb)
  463. : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
  464. void
  465. _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
  466. // 0 => OK to delete.
  467. int
  468. _M_remove_reference()
  469. {
  470. // Be race-detector-friendly. For more info see bits/c++config.
  471. _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
  472. int __res = __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1);
  473. if (__res == 0)
  474. {
  475. _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
  476. }
  477. return __res;
  478. }
  479. };
  480. _Callback_list* _M_callbacks;
  481. void
  482. _M_call_callbacks(event __ev) throw();
  483. void
  484. _M_dispose_callbacks(void) throw();
  485. // 27.4.2.5 Members for iword/pword storage
  486. struct _Words
  487. {
  488. void* _M_pword;
  489. long _M_iword;
  490. _Words() : _M_pword(0), _M_iword(0) { }
  491. };
  492. // Only for failed iword/pword calls.
  493. _Words _M_word_zero;
  494. // Guaranteed storage.
  495. // The first 5 iword and pword slots are reserved for internal use.
  496. enum { _S_local_word_size = 8 };
  497. _Words _M_local_word[_S_local_word_size];
  498. // Allocated storage.
  499. int _M_word_size;
  500. _Words* _M_word;
  501. _Words&
  502. _M_grow_words(int __index, bool __iword);
  503. // Members for locale and locale caching.
  504. locale _M_ios_locale;
  505. void
  506. _M_init() throw();
  507. public:
  508. // 27.4.2.1.6 Class ios_base::Init
  509. // Used to initialize standard streams. In theory, g++ could use
  510. // -finit-priority to order this stuff correctly without going
  511. // through these machinations.
  512. class Init
  513. {
  514. friend class ios_base;
  515. public:
  516. Init();
  517. ~Init();
  518. #if __cplusplus >= 201103L
  519. Init(const Init&) = default;
  520. Init& operator=(const Init&) = default;
  521. #endif
  522. private:
  523. static _Atomic_word _S_refcount;
  524. static bool _S_synced_with_stdio;
  525. };
  526. // [27.4.2.2] fmtflags state functions
  527. /**
  528. * @brief Access to format flags.
  529. * @return The format control flags for both input and output.
  530. */
  531. fmtflags
  532. flags() const
  533. { return _M_flags; }
  534. /**
  535. * @brief Setting new format flags all at once.
  536. * @param __fmtfl The new flags to set.
  537. * @return The previous format control flags.
  538. *
  539. * This function overwrites all the format flags with @a __fmtfl.
  540. */
  541. fmtflags
  542. flags(fmtflags __fmtfl)
  543. {
  544. fmtflags __old = _M_flags;
  545. _M_flags = __fmtfl;
  546. return __old;
  547. }
  548. /**
  549. * @brief Setting new format flags.
  550. * @param __fmtfl Additional flags to set.
  551. * @return The previous format control flags.
  552. *
  553. * This function sets additional flags in format control. Flags that
  554. * were previously set remain set.
  555. */
  556. fmtflags
  557. setf(fmtflags __fmtfl)
  558. {
  559. fmtflags __old = _M_flags;
  560. _M_flags |= __fmtfl;
  561. return __old;
  562. }
  563. /**
  564. * @brief Setting new format flags.
  565. * @param __fmtfl Additional flags to set.
  566. * @param __mask The flags mask for @a fmtfl.
  567. * @return The previous format control flags.
  568. *
  569. * This function clears @a mask in the format flags, then sets
  570. * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
  571. */
  572. fmtflags
  573. setf(fmtflags __fmtfl, fmtflags __mask)
  574. {
  575. fmtflags __old = _M_flags;
  576. _M_flags &= ~__mask;
  577. _M_flags |= (__fmtfl & __mask);
  578. return __old;
  579. }
  580. /**
  581. * @brief Clearing format flags.
  582. * @param __mask The flags to unset.
  583. *
  584. * This function clears @a __mask in the format flags.
  585. */
  586. void
  587. unsetf(fmtflags __mask)
  588. { _M_flags &= ~__mask; }
  589. /**
  590. * @brief Flags access.
  591. * @return The precision to generate on certain output operations.
  592. *
  593. * Be careful if you try to give a definition of @a precision here; see
  594. * DR 189.
  595. */
  596. streamsize
  597. precision() const
  598. { return _M_precision; }
  599. /**
  600. * @brief Changing flags.
  601. * @param __prec The new precision value.
  602. * @return The previous value of precision().
  603. */
  604. streamsize
  605. precision(streamsize __prec)
  606. {
  607. streamsize __old = _M_precision;
  608. _M_precision = __prec;
  609. return __old;
  610. }
  611. /**
  612. * @brief Flags access.
  613. * @return The minimum field width to generate on output operations.
  614. *
  615. * <em>Minimum field width</em> refers to the number of characters.
  616. */
  617. streamsize
  618. width() const
  619. { return _M_width; }
  620. /**
  621. * @brief Changing flags.
  622. * @param __wide The new width value.
  623. * @return The previous value of width().
  624. */
  625. streamsize
  626. width(streamsize __wide)
  627. {
  628. streamsize __old = _M_width;
  629. _M_width = __wide;
  630. return __old;
  631. }
  632. // [27.4.2.4] ios_base static members
  633. /**
  634. * @brief Interaction with the standard C I/O objects.
  635. * @param __sync Whether to synchronize or not.
  636. * @return True if the standard streams were previously synchronized.
  637. *
  638. * The synchronization referred to is @e only that between the standard
  639. * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
  640. * cout). User-declared streams are unaffected. See
  641. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
  642. */
  643. static bool
  644. sync_with_stdio(bool __sync = true);
  645. // [27.4.2.3] ios_base locale functions
  646. /**
  647. * @brief Setting a new locale.
  648. * @param __loc The new locale.
  649. * @return The previous locale.
  650. *
  651. * Sets the new locale for this stream, and then invokes each callback
  652. * with imbue_event.
  653. */
  654. locale
  655. imbue(const locale& __loc) throw();
  656. /**
  657. * @brief Locale access
  658. * @return A copy of the current locale.
  659. *
  660. * If @c imbue(loc) has previously been called, then this function
  661. * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
  662. * the global C++ locale.
  663. */
  664. locale
  665. getloc() const
  666. { return _M_ios_locale; }
  667. /**
  668. * @brief Locale access
  669. * @return A reference to the current locale.
  670. *
  671. * Like getloc above, but returns a reference instead of
  672. * generating a copy.
  673. */
  674. const locale&
  675. _M_getloc() const
  676. { return _M_ios_locale; }
  677. // [27.4.2.5] ios_base storage functions
  678. /**
  679. * @brief Access to unique indices.
  680. * @return An integer different from all previous calls.
  681. *
  682. * This function returns a unique integer every time it is called. It
  683. * can be used for any purpose, but is primarily intended to be a unique
  684. * index for the iword and pword functions. The expectation is that an
  685. * application calls xalloc in order to obtain an index in the iword and
  686. * pword arrays that can be used without fear of conflict.
  687. *
  688. * The implementation maintains a static variable that is incremented and
  689. * returned on each invocation. xalloc is guaranteed to return an index
  690. * that is safe to use in the iword and pword arrays.
  691. */
  692. static int
  693. xalloc() throw();
  694. /**
  695. * @brief Access to integer array.
  696. * @param __ix Index into the array.
  697. * @return A reference to an integer associated with the index.
  698. *
  699. * The iword function provides access to an array of integers that can be
  700. * used for any purpose. The array grows as required to hold the
  701. * supplied index. All integers in the array are initialized to 0.
  702. *
  703. * The implementation reserves several indices. You should use xalloc to
  704. * obtain an index that is safe to use. Also note that since the array
  705. * can grow dynamically, it is not safe to hold onto the reference.
  706. */
  707. long&
  708. iword(int __ix)
  709. {
  710. _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
  711. ? _M_word[__ix] : _M_grow_words(__ix, true);
  712. return __word._M_iword;
  713. }
  714. /**
  715. * @brief Access to void pointer array.
  716. * @param __ix Index into the array.
  717. * @return A reference to a void* associated with the index.
  718. *
  719. * The pword function provides access to an array of pointers that can be
  720. * used for any purpose. The array grows as required to hold the
  721. * supplied index. All pointers in the array are initialized to 0.
  722. *
  723. * The implementation reserves several indices. You should use xalloc to
  724. * obtain an index that is safe to use. Also note that since the array
  725. * can grow dynamically, it is not safe to hold onto the reference.
  726. */
  727. void*&
  728. pword(int __ix)
  729. {
  730. _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
  731. ? _M_word[__ix] : _M_grow_words(__ix, false);
  732. return __word._M_pword;
  733. }
  734. // Destructor
  735. /**
  736. * Invokes each callback with erase_event. Destroys local storage.
  737. *
  738. * Note that the ios_base object for the standard streams never gets
  739. * destroyed. As a result, any callbacks registered with the standard
  740. * streams will not get invoked with erase_event (unless copyfmt is
  741. * used).
  742. */
  743. virtual ~ios_base();
  744. protected:
  745. ios_base() throw ();
  746. #if __cplusplus < 201103L
  747. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  748. // 50. Copy constructor and assignment operator of ios_base
  749. private:
  750. ios_base(const ios_base&);
  751. ios_base&
  752. operator=(const ios_base&);
  753. #else
  754. public:
  755. ios_base(const ios_base&) = delete;
  756. ios_base&
  757. operator=(const ios_base&) = delete;
  758. protected:
  759. void
  760. _M_move(ios_base&) noexcept;
  761. void
  762. _M_swap(ios_base& __rhs) noexcept;
  763. #endif
  764. };
  765. // [27.4.5.1] fmtflags manipulators
  766. /// Calls base.setf(ios_base::boolalpha).
  767. inline ios_base&
  768. boolalpha(ios_base& __base)
  769. {
  770. __base.setf(ios_base::boolalpha);
  771. return __base;
  772. }
  773. /// Calls base.unsetf(ios_base::boolalpha).
  774. inline ios_base&
  775. noboolalpha(ios_base& __base)
  776. {
  777. __base.unsetf(ios_base::boolalpha);
  778. return __base;
  779. }
  780. /// Calls base.setf(ios_base::showbase).
  781. inline ios_base&
  782. showbase(ios_base& __base)
  783. {
  784. __base.setf(ios_base::showbase);
  785. return __base;
  786. }
  787. /// Calls base.unsetf(ios_base::showbase).
  788. inline ios_base&
  789. noshowbase(ios_base& __base)
  790. {
  791. __base.unsetf(ios_base::showbase);
  792. return __base;
  793. }
  794. /// Calls base.setf(ios_base::showpoint).
  795. inline ios_base&
  796. showpoint(ios_base& __base)
  797. {
  798. __base.setf(ios_base::showpoint);
  799. return __base;
  800. }
  801. /// Calls base.unsetf(ios_base::showpoint).
  802. inline ios_base&
  803. noshowpoint(ios_base& __base)
  804. {
  805. __base.unsetf(ios_base::showpoint);
  806. return __base;
  807. }
  808. /// Calls base.setf(ios_base::showpos).
  809. inline ios_base&
  810. showpos(ios_base& __base)
  811. {
  812. __base.setf(ios_base::showpos);
  813. return __base;
  814. }
  815. /// Calls base.unsetf(ios_base::showpos).
  816. inline ios_base&
  817. noshowpos(ios_base& __base)
  818. {
  819. __base.unsetf(ios_base::showpos);
  820. return __base;
  821. }
  822. /// Calls base.setf(ios_base::skipws).
  823. inline ios_base&
  824. skipws(ios_base& __base)
  825. {
  826. __base.setf(ios_base::skipws);
  827. return __base;
  828. }
  829. /// Calls base.unsetf(ios_base::skipws).
  830. inline ios_base&
  831. noskipws(ios_base& __base)
  832. {
  833. __base.unsetf(ios_base::skipws);
  834. return __base;
  835. }
  836. /// Calls base.setf(ios_base::uppercase).
  837. inline ios_base&
  838. uppercase(ios_base& __base)
  839. {
  840. __base.setf(ios_base::uppercase);
  841. return __base;
  842. }
  843. /// Calls base.unsetf(ios_base::uppercase).
  844. inline ios_base&
  845. nouppercase(ios_base& __base)
  846. {
  847. __base.unsetf(ios_base::uppercase);
  848. return __base;
  849. }
  850. /// Calls base.setf(ios_base::unitbuf).
  851. inline ios_base&
  852. unitbuf(ios_base& __base)
  853. {
  854. __base.setf(ios_base::unitbuf);
  855. return __base;
  856. }
  857. /// Calls base.unsetf(ios_base::unitbuf).
  858. inline ios_base&
  859. nounitbuf(ios_base& __base)
  860. {
  861. __base.unsetf(ios_base::unitbuf);
  862. return __base;
  863. }
  864. // [27.4.5.2] adjustfield manipulators
  865. /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
  866. inline ios_base&
  867. internal(ios_base& __base)
  868. {
  869. __base.setf(ios_base::internal, ios_base::adjustfield);
  870. return __base;
  871. }
  872. /// Calls base.setf(ios_base::left, ios_base::adjustfield).
  873. inline ios_base&
  874. left(ios_base& __base)
  875. {
  876. __base.setf(ios_base::left, ios_base::adjustfield);
  877. return __base;
  878. }
  879. /// Calls base.setf(ios_base::right, ios_base::adjustfield).
  880. inline ios_base&
  881. right(ios_base& __base)
  882. {
  883. __base.setf(ios_base::right, ios_base::adjustfield);
  884. return __base;
  885. }
  886. // [27.4.5.3] basefield manipulators
  887. /// Calls base.setf(ios_base::dec, ios_base::basefield).
  888. inline ios_base&
  889. dec(ios_base& __base)
  890. {
  891. __base.setf(ios_base::dec, ios_base::basefield);
  892. return __base;
  893. }
  894. /// Calls base.setf(ios_base::hex, ios_base::basefield).
  895. inline ios_base&
  896. hex(ios_base& __base)
  897. {
  898. __base.setf(ios_base::hex, ios_base::basefield);
  899. return __base;
  900. }
  901. /// Calls base.setf(ios_base::oct, ios_base::basefield).
  902. inline ios_base&
  903. oct(ios_base& __base)
  904. {
  905. __base.setf(ios_base::oct, ios_base::basefield);
  906. return __base;
  907. }
  908. // [27.4.5.4] floatfield manipulators
  909. /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
  910. inline ios_base&
  911. fixed(ios_base& __base)
  912. {
  913. __base.setf(ios_base::fixed, ios_base::floatfield);
  914. return __base;
  915. }
  916. /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
  917. inline ios_base&
  918. scientific(ios_base& __base)
  919. {
  920. __base.setf(ios_base::scientific, ios_base::floatfield);
  921. return __base;
  922. }
  923. #if __cplusplus >= 201103L
  924. // New C++11 floatfield manipulators
  925. /// Calls
  926. /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
  927. inline ios_base&
  928. hexfloat(ios_base& __base)
  929. {
  930. __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
  931. return __base;
  932. }
  933. /// Calls @c base.unsetf(ios_base::floatfield)
  934. inline ios_base&
  935. defaultfloat(ios_base& __base)
  936. {
  937. __base.unsetf(ios_base::floatfield);
  938. return __base;
  939. }
  940. #endif
  941. _GLIBCXX_END_NAMESPACE_VERSION
  942. } // namespace
  943. #endif /* _IOS_BASE_H */