]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++11/cow-stdexcept.cc
New std::string implementation.
[thirdparty/gcc.git] / libstdc++-v3 / src / c++11 / cow-stdexcept.cc
1 // Methods for Exception Support for -*- C++ -*-
2
3 // Copyright (C) 2014 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: 19.1 Exception classes
27 //
28
29 // All exception classes still use the classic COW std::string.
30 #define _GLIBCXX_USE_CXX11_ABI 0
31 #define _GLIBCXX_DEFINE_STDEXCEPT_COPY_OPS 1
32 #define __cow_string __cow_stringxxx
33 #include <stdexcept>
34 #include <system_error>
35 #undef __cow_string
36
37 namespace std _GLIBCXX_VISIBILITY(default)
38 {
39 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40
41 // Copy constructors and assignment operators defined using COW std::string
42
43 logic_error::logic_error(const logic_error& e) noexcept
44 : _M_msg(e._M_msg) { }
45
46 logic_error& logic_error::operator=(const logic_error& e) noexcept
47 { _M_msg = e._M_msg; return *this; }
48
49 runtime_error::runtime_error(const runtime_error& e) noexcept
50 : _M_msg(e._M_msg) { }
51
52 runtime_error&
53 runtime_error::operator=(const runtime_error& e) noexcept
54 { _M_msg = e._M_msg; return *this; }
55
56 // New C++11 constructors:
57
58 logic_error::logic_error(const char* __arg)
59 : exception(), _M_msg(__arg) { }
60
61 domain_error::domain_error(const char* __arg)
62 : logic_error(__arg) { }
63
64 invalid_argument::invalid_argument(const char* __arg)
65 : logic_error(__arg) { }
66
67 length_error::length_error(const char* __arg)
68 : logic_error(__arg) { }
69
70 out_of_range::out_of_range(const char* __arg)
71 : logic_error(__arg) { }
72
73 runtime_error::runtime_error(const char* __arg)
74 : exception(), _M_msg(__arg) { }
75
76 range_error::range_error(const char* __arg)
77 : runtime_error(__arg) { }
78
79 overflow_error::overflow_error(const char* __arg)
80 : runtime_error(__arg) { }
81
82 underflow_error::underflow_error(const char* __arg)
83 : runtime_error(__arg) { }
84
85 #if _GLIBCXX_USE_DUAL_ABI
86 // Converting constructor from COW std::string to SSO string.
87 __sso_string::__sso_string(const string& s)
88 : __sso_string(s.c_str(), s.length()) { }
89
90 // Redefine __cow_string so that we can define and export its members
91 // in terms of the COW std::string.
92 struct __cow_string
93 {
94 union {
95 const char* _M_p;
96 char _M_bytes[sizeof(_M_p)];
97 std::string _M_str;
98 };
99
100 __cow_string();
101 __cow_string(const std::string& s);
102 __cow_string(const char*, size_t n);
103 __cow_string(const __cow_string&) noexcept;
104 __cow_string& operator=(const __cow_string&) noexcept;
105 ~__cow_string();
106 __cow_string(__cow_string&&) noexcept;
107 __cow_string& operator=(__cow_string&&) noexcept;
108 };
109
110 __cow_string::__cow_string() : _M_str() { }
111
112 __cow_string::__cow_string(const std::string& s) : _M_str(s) { }
113
114 __cow_string::__cow_string(const char* s, size_t n) : _M_str(s, n) { }
115
116 __cow_string::__cow_string(const __cow_string& s) noexcept
117 : _M_str(s._M_str) { }
118
119 __cow_string&
120 __cow_string::operator=(const __cow_string& s) noexcept
121 {
122 _M_str = s._M_str;
123 return *this;
124 }
125
126 __cow_string::~__cow_string() { _M_str.~basic_string(); }
127
128 __cow_string::__cow_string(__cow_string&& s) noexcept
129 : _M_str(std::move(s._M_str)) { }
130
131 __cow_string&
132 __cow_string::operator=(__cow_string&& s) noexcept
133 {
134 _M_str = std::move(s._M_str);
135 return *this;
136 }
137
138 static_assert(sizeof(__cow_string) == sizeof(std::string),
139 "sizeof(std::string) has changed");
140 static_assert(alignof(__cow_string) == alignof(std::string),
141 "alignof(std::string) has changed");
142 #endif
143
144 // Return error_category::message() as an SSO string
145 __sso_string
146 error_category::_M_message(int i) const
147 {
148 string msg = this->message(i);
149 return {msg.c_str(), msg.length()};
150 }
151
152 _GLIBCXX_END_NAMESPACE_VERSION
153 } // namespace