]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++11/cxx11-ios_failure.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / src / c++11 / cxx11-ios_failure.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 2014-2020 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 //
26 // ISO C++ 14882:2011: 27.5.3.1.1 Class ios_base::failure
27 //
28
29 #define _GLIBCXX_USE_CXX11_ABI 1
30 #include <ios>
31 #include <bits/functexcept.h>
32 #include <cxxabi.h>
33
34 #ifdef _GLIBCXX_USE_NLS
35 # include <libintl.h>
36 # define _(msgid) gettext (msgid)
37 #else
38 # define _(msgid) (msgid)
39 #endif
40
41 #if ! _GLIBCXX_USE_DUAL_ABI
42 # error This file should not be compiled for this configuration.
43 #endif
44
45 namespace
46 {
47 struct io_error_category : std::error_category
48 {
49 virtual const char*
50 name() const noexcept
51 { return "iostream"; }
52
53 _GLIBCXX_DEFAULT_ABI_TAG
54 virtual std::string message(int __ec) const
55 {
56 std::string __msg;
57 switch (std::io_errc(__ec))
58 {
59 case std::io_errc::stream:
60 __msg = "iostream error";
61 break;
62 default:
63 __msg = "Unknown error";
64 break;
65 }
66 return __msg;
67 }
68 };
69
70 const io_error_category&
71 __io_category_instance() noexcept
72 {
73 static const io_error_category __ec{};
74 return __ec;
75 }
76
77 } // namespace
78
79 namespace std _GLIBCXX_VISIBILITY(default)
80 {
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
82
83 const error_category&
84 iostream_category() noexcept
85 { return __io_category_instance(); }
86
87 ios_base::failure::failure(const string& __str)
88 : system_error(io_errc::stream, __str) { }
89
90 ios_base::failure::failure(const string& __str, const error_code& __ec)
91 : system_error(__ec, __str) { }
92
93 ios_base::failure::failure(const char* __str, const error_code& __ec)
94 : system_error(__ec, __str) { }
95
96 ios_base::failure::~failure()
97 { }
98
99 const char*
100 ios_base::failure::what() const throw()
101 { return runtime_error::what(); }
102
103 #if __cpp_rtti
104 // These functions are defined in src/c++98/ios_failure.cc
105 extern void __construct_ios_failure(void*, const char*);
106 extern void __destroy_ios_failure(void*);
107 extern bool __is_ios_failure_handler(const __cxxabiv1::__class_type_info*);
108
109 // The type thrown to report errors during stream buffer operations.
110 // In addition to the ios::failure[abi:cxx11] base class it also has a
111 // member of the gcc4-compatible ios::failure type (in an opaque buffer).
112 struct __ios_failure : std::ios::failure
113 {
114 __ios_failure(const char* s) : failure(s)
115 { __construct_ios_failure(buf, runtime_error::what()); }
116
117 __ios_failure(const char* s, int e) : failure(s, to_error_code(e))
118 { __construct_ios_failure(buf, runtime_error::what()); }
119
120 ~__ios_failure()
121 { __destroy_ios_failure(buf); }
122
123 // Use std::runtime_error as a proxy for the gcc4-compatible ios::failure
124 // (which can't be declared here because _GLIBCXX_USE_CXX11_ABI == 1).
125 // There are assertions in src/c++98/ios_failure.cc to ensure the size
126 // and alignment assumptions are valid.
127 alignas(runtime_error) unsigned char buf[sizeof(runtime_error)];
128
129 static error_code
130 to_error_code(int e)
131 { return e ? error_code(e, system_category()) : io_errc::stream; }
132 };
133
134 // Custom type info for __ios_failure.
135 class __iosfail_type_info : __cxxabiv1::__si_class_type_info
136 {
137 ~__iosfail_type_info();
138
139 bool
140 __do_upcast (const __class_type_info *dst_type,
141 void **obj_ptr) const override;
142 };
143
144 __iosfail_type_info::~__iosfail_type_info() = default;
145
146 // This function gets called to see if an exception of type
147 // __ios_failure can be upcast to the type in a catch handler.
148 bool
149 __iosfail_type_info::__do_upcast(const __class_type_info *dst_type,
150 void **obj_ptr) const
151 {
152 // If the handler is for the gcc4-compatible ios::failure type then
153 // catch the object stored in __ios_failure::buf instead of
154 // the __ios_failure exception object itself.
155 if (__is_ios_failure_handler(dst_type))
156 {
157 *obj_ptr = static_cast<__ios_failure*>(*obj_ptr)->buf;
158 return true;
159 }
160 // Otherwise proceed as normal to see if the handler matches.
161 return __class_type_info::__do_upcast(dst_type, obj_ptr);
162 }
163 #else // ! __cpp_rtti
164 using __ios_failure = ios::failure;
165 #endif
166
167 void
168 __throw_ios_failure(const char* __s __attribute__((unused)))
169 { _GLIBCXX_THROW_OR_ABORT(__ios_failure(_(__s))); }
170
171 void
172 __throw_ios_failure(const char* str __attribute__((unused)),
173 int err __attribute__((unused)))
174 { _GLIBCXX_THROW_OR_ABORT(__ios_failure(_(str), err)); }
175
176 _GLIBCXX_END_NAMESPACE_VERSION
177 } // namespace