1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 2014-2024 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 // ISO C++ 14882:2011: 27.5.3.1.1 Class ios_base::failure
29 #define _GLIBCXX_USE_CXX11_ABI 1
31 #include <bits/functexcept.h>
34 #ifdef _GLIBCXX_USE_NLS
36 # define _(msgid) gettext (msgid)
38 # define _(msgid) (msgid)
41 #if ! _GLIBCXX_USE_DUAL_ABI
42 # error This file should not be compiled for this configuration.
45 namespace std
_GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 ios_base::failure::failure(const string
& __str
)
50 : system_error(io_errc::stream
, __str
) { }
52 ios_base::failure::failure(const string
& __str
, const error_code
& __ec
)
53 : system_error(__ec
, __str
) { }
55 ios_base::failure::failure(const char* __str
, const error_code
& __ec
)
56 : system_error(__ec
, __str
) { }
58 ios_base::failure::~failure()
62 ios_base::failure::what() const throw()
63 { return runtime_error::what(); }
66 // These functions are defined in src/c++98/ios_failure.cc
67 extern void __construct_ios_failure(void*, const char*);
68 extern void __destroy_ios_failure(void*);
69 extern bool __is_ios_failure_handler(const __cxxabiv1::__class_type_info
*);
71 // The type thrown to report errors during stream buffer operations.
72 // In addition to the ios::failure[abi:cxx11] base class it also has a
73 // member of the gcc4-compatible ios::failure type (in an opaque buffer).
74 struct __ios_failure
: std::ios::failure
76 __ios_failure(const char* s
) : failure(s
)
77 { __construct_ios_failure(buf
, runtime_error::what()); }
79 __ios_failure(const char* s
, const error_code
& e
) : failure(s
, e
)
80 { __construct_ios_failure(buf
, runtime_error::what()); }
83 { __destroy_ios_failure(buf
); }
85 // Use std::runtime_error as a proxy for the gcc4-compatible ios::failure
86 // (which can't be declared here because _GLIBCXX_USE_CXX11_ABI == 1).
87 // There are assertions in src/c++98/ios_failure.cc to ensure the size
88 // and alignment assumptions are valid.
89 alignas(runtime_error
) unsigned char buf
[sizeof(runtime_error
)];
92 // Custom type info for __ios_failure.
93 class __iosfail_type_info
: __cxxabiv1::__si_class_type_info
95 ~__iosfail_type_info();
97 using __si_class_type_info::__do_upcast
;
100 __do_upcast (const __class_type_info
*dst_type
,
101 void **obj_ptr
) const override
;
104 __iosfail_type_info::~__iosfail_type_info() = default;
106 // This function gets called to see if an exception of type
107 // __ios_failure can be upcast to the type in a catch handler.
109 __iosfail_type_info::__do_upcast(const __class_type_info
*dst_type
,
110 void **obj_ptr
) const
112 // If the handler is for the gcc4-compatible ios::failure type then
113 // catch the object stored in __ios_failure::buf instead of
114 // the __ios_failure exception object itself.
115 if (__is_ios_failure_handler(dst_type
))
117 *obj_ptr
= static_cast<__ios_failure
*>(*obj_ptr
)->buf
;
120 // Otherwise proceed as normal to see if the handler matches.
121 return __class_type_info::__do_upcast(dst_type
, obj_ptr
);
123 #else // ! __cpp_rtti
124 using __ios_failure
= ios::failure
;
128 __throw_ios_failure(const char* __s
__attribute__((unused
)))
129 { _GLIBCXX_THROW_OR_ABORT(__ios_failure(_(__s
))); }
132 __throw_ios_failure(const char* str
__attribute__((unused
)),
133 int err
__attribute__((unused
)))
135 _GLIBCXX_THROW_OR_ABORT(__ios_failure(_(str
),
136 err
? error_code(err
, generic_category()) : io_errc::stream
));
139 _GLIBCXX_END_NAMESPACE_VERSION