Debian Codegraph @

libstdc++-v3/src/c++11/ios_errcat.cc

raw
1 // std::iostream_category() definition -*- C++ -*-
2
3 // Copyright (C) 2014-2025 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.6.5 Error reporting [error.reporting]
27 //
28
29 #define _GLIBCXX_USE_CXX11_ABI 1
30 #include <ios>
31
32 #if __has_cpp_attribute(clang::require_constant_initialization)
33 # define __constinit [[clang::require_constant_initialization]]
34 #endif
35
36 namespace
37 {
38 struct io_error_category final : std::error_category
39 {
40 const char*
41 name() const noexcept final
42 { return "iostream"; }
43
44 _GLIBCXX_DEFAULT_ABI_TAG
45 std::string
46 message(int __ec) const final
47 {
48 std::string __msg;
49 switch (std::io_errc(__ec))
50 {
51 case std::io_errc::stream:
52 __msg = "iostream error";
53 break;
54 default:
55 __msg = "Unknown error";
56 break;
57 }
58 return __msg;
59 }
60 };
61
62 struct constant_init
63 {
64 union {
65 unsigned char unused;
66 io_error_category cat;
67 };
68 constexpr constant_init() : cat() { }
69 ~constant_init() { /* do nothing, union member is not destroyed */ }
70 };
71
72 __constinit constant_init io_category_instance{};
73 } // namespace
74
75 namespace std _GLIBCXX_VISIBILITY(default)
76 {
77 _GLIBCXX_BEGIN_NAMESPACE_VERSION
78
79 const error_category&
80 iostream_category() noexcept
81 { return io_category_instance.cat; }
82
83 _GLIBCXX_END_NAMESPACE_VERSION
84 } // namespace