ios_base.h 31 KB

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