cxx11-ios_failure.cc revision 1.1.1.3
1// Iostreams base classes -*- C++ -*-
2
3// Copyright (C) 2014-2016 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 <typeinfo>
32#include <cxxabi.h>
33
34#if ! _GLIBCXX_USE_DUAL_ABI
35# error This file should not be compiled for this configuration.
36#endif
37
38namespace
39{
40  struct io_error_category : std::error_category
41  {
42    virtual const char*
43    name() const noexcept
44    { return "iostream"; }
45
46    _GLIBCXX_DEFAULT_ABI_TAG
47    virtual std::string message(int __ec) const
48    {
49      std::string __msg;
50      switch (std::io_errc(__ec))
51      {
52      case std::io_errc::stream:
53          __msg = "iostream error";
54          break;
55      default:
56          __msg = "Unknown error";
57          break;
58      }
59      return __msg;
60    }
61  };
62
63  const io_error_category&
64  __io_category_instance() noexcept
65  {
66    static const io_error_category __ec{};
67    return __ec;
68  }
69
70} // namespace
71
72namespace std _GLIBCXX_VISIBILITY(default)
73{
74_GLIBCXX_BEGIN_NAMESPACE_VERSION
75
76  const error_category&
77  iostream_category() noexcept
78  { return __io_category_instance(); }
79
80  ios_base::failure::failure(const string& __str)
81  : system_error(io_errc::stream, __str) { }
82
83  ios_base::failure::failure(const string& __str, const error_code& __ec)
84  : system_error(__ec, __str) { }
85
86  ios_base::failure::failure(const char* __str, const error_code& __ec)
87  : system_error(__ec, __str) { }
88
89  ios_base::failure::~failure()
90  { }
91
92  const char*
93  ios_base::failure::what() const throw()
94  { return runtime_error::what(); }
95
96  // __throw_ios_failure() is defined in src/c++98/ios_failure.cc
97
98#if __cpp_rtti
99  // If RTTI is enabled the exception type thrown will use these functions to
100  // construct/destroy a ios::failure[abi:cxx11] object in a buffer,
101  // and to catch that object via a handler of the [abi:cxx11] type.
102  void
103  __construct_ios_failure(void* buf, const char* msg)
104  { ::new(buf) ios_base::failure(msg); }
105
106  void
107  __destroy_ios_failure(void* buf)
108  { static_cast<ios_base::failure*>(buf)->~failure(); }
109
110  bool
111  __is_ios_failure_handler(const __cxxabiv1::__class_type_info* type)
112  { return *type == typeid(ios::failure); }
113
114  // static assertions to ensure ios::failure fits in a buffer
115  // with the same size and alignment as system_error:
116  static_assert(sizeof(ios::failure) <= sizeof(system_error), "");
117  static_assert(__alignof(ios::failure) <= __alignof(system_error), "");
118#endif // __cpp_rtti
119
120_GLIBCXX_END_NAMESPACE_VERSION
121} // namespace
122