// Iostreams base classes -*- C++ -*-
-// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
-// 2006, 2007, 2008, 2009, 2010
-// Free Software Foundation, Inc.
+// Copyright (C) 1997-2024 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
#include <bits/localefwd.h>
#include <bits/locale_classes.h>
+#if __cplusplus < 201103L
+# include <stdexcept>
+#else
+# include <system_error>
+#endif
+
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
// The following definitions of bitmask types are enums, not ints,
// as permitted (but not required) in the standard, in order to provide
- // better type safety in iostream calls. A side effect is that
- // expressions involving them are no longer compile-time constants.
+ // better type safety in iostream calls. A side effect is that in C++98
+ // expressions involving them are not compile-time constants.
enum _Ios_Fmtflags
{
_S_boolalpha = 1L << 0,
_S_adjustfield = _S_left | _S_right | _S_internal,
_S_basefield = _S_dec | _S_oct | _S_hex,
_S_floatfield = _S_scientific | _S_fixed,
- _S_ios_fmtflags_end = 1L << 16
+ _S_ios_fmtflags_end = 1L << 16,
+ _S_ios_fmtflags_max = __INT_MAX__,
+ _S_ios_fmtflags_min = ~__INT_MAX__
};
inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
_S_in = 1L << 3,
_S_out = 1L << 4,
_S_trunc = 1L << 5,
- _S_ios_openmode_end = 1L << 16
+ _S_noreplace = 1L << 6,
+ _S_ios_openmode_end = 1L << 16,
+ _S_ios_openmode_max = __INT_MAX__,
+ _S_ios_openmode_min = ~__INT_MAX__
};
inline _GLIBCXX_CONSTEXPR _Ios_Openmode
_S_badbit = 1L << 0,
_S_eofbit = 1L << 1,
_S_failbit = 1L << 2,
- _S_ios_iostate_end = 1L << 16
+ _S_ios_iostate_end = 1L << 16,
+ _S_ios_iostate_max = __INT_MAX__,
+ _S_ios_iostate_min = ~__INT_MAX__
};
inline _GLIBCXX_CONSTEXPR _Ios_Iostate
_S_ios_seekdir_end = 1L << 16
};
+#if __cplusplus >= 201103L
+ /// I/O error code
+ enum class io_errc { stream = 1 };
+
+ template <> struct is_error_code_enum<io_errc> : public true_type { };
+
+ [[__nodiscard__, __gnu__::__const__]]
+ const error_category&
+ iostream_category() noexcept;
+
+ [[__nodiscard__]]
+ inline error_code
+ make_error_code(io_errc __e) noexcept
+ { return error_code(static_cast<int>(__e), iostream_category()); }
+
+ [[__nodiscard__]]
+ inline error_condition
+ make_error_condition(io_errc __e) noexcept
+ { return error_condition(static_cast<int>(__e), iostream_category()); }
+#endif
+
// 27.4.2 Class ios_base
/**
* @brief The base of the I/O class hierarchy.
*/
class ios_base
{
+#if _GLIBCXX_USE_CXX11_ABI
+#if __cplusplus < 201103L
+ // Type that is layout-compatible with std::system_error
+ struct system_error : std::runtime_error
+ {
+ // Type that is layout-compatible with std::error_code
+ struct error_code
+ {
+ error_code() { }
+ private:
+ int _M_value;
+ const void* _M_cat;
+ } _M_code;
+ };
+#endif
+#endif
public:
/**
*
* 27.4.2.1.1 Class ios_base::failure
*/
+#if _GLIBCXX_USE_CXX11_ABI
+ class _GLIBCXX_ABI_TAG_CXX11 failure : public system_error
+ {
+ public:
+ explicit
+ failure(const string& __str);
+
+#if __cplusplus >= 201103L
+ explicit
+ failure(const string&, const error_code&);
+
+ explicit
+ failure(const char*, const error_code& = io_errc::stream);
+#endif
+
+ virtual
+ ~failure() throw();
+
+ virtual const char*
+ what() const throw();
+ };
+#else
class failure : public exception
{
public:
virtual const char*
what() const throw();
+#if __cplusplus >= 201103L
+ // Define the new members required by C++11,
+ // even though the error_code cannot be stored.
+
+ explicit
+ failure(const string& __s, const error_code&) noexcept
+ : failure(__s)
+ { }
+
+ explicit
+ failure(const char* __s, const error_code& = error_code{})
+ : failure(string(__s))
+ { }
+
+ // Stand-in for system_error::code() but returning by value.
+ error_code code() const noexcept { return error_code{}; }
+#endif
+
private:
string _M_msg;
};
+#endif
// 27.4.2.1.2 Type ios_base::fmtflags
/**
/// Perform input and output in binary mode (as opposed to text mode).
/// This is probably not what you think it is; see
- /// http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch27s02.html
+ /// https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
static const openmode binary = _S_bin;
/// Open for input. Default for @c ifstream and fstream.
/// Open for output. Default for @c ofstream and fstream.
static const openmode out = _S_out;
- /// Open for input. Default for @c ofstream.
+ /// Truncate an existing stream when opening. Default for @c ofstream.
static const openmode trunc = _S_trunc;
+ static const openmode __noreplace = _S_noreplace;
+
+#ifdef __glibcxx_ios_noreplace // C++ >= 23 && HOSTED
+ /// Open a file in exclusive mode.
+ static const openmode noreplace = _S_noreplace;
+#endif
+
// 27.4.2.1.5 Type ios_base::seekdir
/**
* @brief This is an enumerated type.
/// Request a seek relative to the current end of the sequence.
static const seekdir end = _S_end;
- // Annex D.6
- typedef int io_state;
- typedef int open_mode;
- typedef int seek_dir;
-
- typedef std::streampos streampos;
- typedef std::streamoff streamoff;
+#if __cplusplus <= 201402L
+ // Annex D.6 (removed in C++17)
+ typedef int io_state
+ _GLIBCXX_DEPRECATED_SUGGEST("std::iostate");
+ typedef int open_mode
+ _GLIBCXX_DEPRECATED_SUGGEST("std::openmode");
+ typedef int seek_dir
+ _GLIBCXX_DEPRECATED_SUGGEST("std::seekdir");
+
+ typedef std::streampos streampos
+ _GLIBCXX_DEPRECATED_SUGGEST("std::streampos");
+ typedef std::streamoff streamoff
+ _GLIBCXX_DEPRECATED_SUGGEST("std::streamoff");
+#endif
// Callbacks;
/**
Init();
~Init();
+#if __cplusplus >= 201103L
+ Init(const Init&) = default;
+ Init& operator=(const Init&) = default;
+#endif
+
private:
static _Atomic_word _S_refcount;
static bool _S_synced_with_stdio;
* The synchronization referred to is @e only that between the standard
* C facilities (e.g., stdout) and the standard C++ objects (e.g.,
* cout). User-declared streams are unaffected. See
- * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch28s02.html
+ * https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
*/
static bool
sync_with_stdio(bool __sync = true);
long&
iword(int __ix)
{
- _Words& __word = (__ix < _M_word_size)
+ _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
? _M_word[__ix] : _M_grow_words(__ix, true);
return __word._M_iword;
}
void*&
pword(int __ix)
{
- _Words& __word = (__ix < _M_word_size)
+ _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
? _M_word[__ix] : _M_grow_words(__ix, false);
return __word._M_pword;
}
protected:
ios_base() throw ();
+#if __cplusplus < 201103L
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 50. Copy constructor and assignment operator of ios_base
private:
ios_base&
operator=(const ios_base&);
+#else
+ public:
+ ios_base(const ios_base&) = delete;
+
+ ios_base&
+ operator=(const ios_base&) = delete;
+
+ protected:
+ void
+ _M_move(ios_base&) noexcept;
+
+ void
+ _M_swap(ios_base& __rhs) noexcept;
+#endif
};
// [27.4.5.1] fmtflags manipulators
return __base;
}
+#if __cplusplus >= 201103L
+ // New C++11 floatfield manipulators
+
+ /// Calls
+ /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
+ inline ios_base&
+ hexfloat(ios_base& __base)
+ {
+ __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
+ return __base;
+ }
+
+ /// Calls @c base.unsetf(ios_base::floatfield)
+ inline ios_base&
+ defaultfloat(ios_base& __base)
+ {
+ __base.unsetf(ios_base::floatfield);
+ return __base;
+ }
+#endif
+
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace