blob: d5cb2d4ae1be2e0b5ca4ee06a7c83c85a2df3226 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:16 +00001//===---------------------- system_error.cpp ------------------------------===//
2//
Howard Hinnant5b08a8a2010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnant3e519522010-05-11 19:42:16 +00004//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant3e519522010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
David Majnemer2fd6f512014-03-17 20:13:54 +000010#define _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnant3e519522010-05-11 19:42:16 +000011#include "system_error"
12#include "string"
13#include "cstring"
14
15_LIBCPP_BEGIN_NAMESPACE_STD
16
17// class error_category
18
Howard Hinnanta62f2892011-05-26 19:48:01 +000019error_category::error_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000020{
21}
22
Howard Hinnanta62f2892011-05-26 19:48:01 +000023error_category::~error_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000024{
25}
26
27error_condition
Howard Hinnanta62f2892011-05-26 19:48:01 +000028error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000029{
30 return error_condition(ev, *this);
31}
32
33bool
Howard Hinnanta62f2892011-05-26 19:48:01 +000034error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000035{
36 return default_error_condition(code) == condition;
37}
38
39bool
Howard Hinnanta62f2892011-05-26 19:48:01 +000040error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000041{
42 return *this == code.category() && code.value() == condition;
43}
44
45string
46__do_message::message(int ev) const
47{
48 return string(strerror(ev));
49}
50
51class _LIBCPP_HIDDEN __generic_error_category
52 : public __do_message
53{
54public:
Howard Hinnanta62f2892011-05-26 19:48:01 +000055 virtual const char* name() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +000056 virtual string message(int ev) const;
57};
58
59const char*
Howard Hinnanta62f2892011-05-26 19:48:01 +000060__generic_error_category::name() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000061{
62 return "generic";
63}
64
65string
66__generic_error_category::message(int ev) const
67{
Howard Hinnant128ba712010-05-24 17:49:41 +000068#ifdef ELAST
69 if (ev > ELAST)
70 return string("unspecified generic_category error");
David Majnemer31234842014-05-29 05:02:22 +000071#elif defined(__linux__)
72 if (ev > 4095)
73 return string("unspecified generic_category error");
Howard Hinnant940e2112010-08-22 00:03:27 +000074#endif // ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +000075 return __do_message::message(ev);
Howard Hinnant3e519522010-05-11 19:42:16 +000076}
77
78const error_category&
Howard Hinnanta62f2892011-05-26 19:48:01 +000079generic_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000080{
81 static __generic_error_category s;
82 return s;
83}
84
85class _LIBCPP_HIDDEN __system_error_category
86 : public __do_message
87{
88public:
Howard Hinnanta62f2892011-05-26 19:48:01 +000089 virtual const char* name() const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +000090 virtual string message(int ev) const;
Howard Hinnanta62f2892011-05-26 19:48:01 +000091 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
Howard Hinnant3e519522010-05-11 19:42:16 +000092};
93
94const char*
Howard Hinnanta62f2892011-05-26 19:48:01 +000095__system_error_category::name() const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +000096{
97 return "system";
98}
99
100string
101__system_error_category::message(int ev) const
102{
Howard Hinnant128ba712010-05-24 17:49:41 +0000103#ifdef ELAST
104 if (ev > ELAST)
105 return string("unspecified system_category error");
David Majnemer31234842014-05-29 05:02:22 +0000106#elif defined(__linux__)
107 if (ev > 4095)
108 return string("unspecified system_category error");
Howard Hinnant940e2112010-08-22 00:03:27 +0000109#endif // ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +0000110 return __do_message::message(ev);
Howard Hinnant3e519522010-05-11 19:42:16 +0000111}
112
113error_condition
Howard Hinnanta62f2892011-05-26 19:48:01 +0000114__system_error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000115{
Howard Hinnant128ba712010-05-24 17:49:41 +0000116#ifdef ELAST
117 if (ev > ELAST)
118 return error_condition(ev, system_category());
David Majnemer31234842014-05-29 05:02:22 +0000119#elif defined(__linux__)
120 if (ev > 4095)
121 return error_condition(ev, system_category());
Howard Hinnant940e2112010-08-22 00:03:27 +0000122#endif // ELAST
Howard Hinnant128ba712010-05-24 17:49:41 +0000123 return error_condition(ev, generic_category());
Howard Hinnant3e519522010-05-11 19:42:16 +0000124}
125
126const error_category&
Howard Hinnanta62f2892011-05-26 19:48:01 +0000127system_category() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000128{
129 static __system_error_category s;
130 return s;
131}
132
133// error_condition
134
135string
136error_condition::message() const
137{
138 return __cat_->message(__val_);
139}
140
141// error_code
142
143string
144error_code::message() const
145{
146 return __cat_->message(__val_);
147}
148
149// system_error
150
151string
152system_error::__init(const error_code& ec, string what_arg)
153{
154 if (ec)
155 {
156 if (!what_arg.empty())
157 what_arg += ": ";
158 what_arg += ec.message();
159 }
Howard Hinnantce48a112011-06-30 21:18:19 +0000160 return _VSTD::move(what_arg);
Howard Hinnant3e519522010-05-11 19:42:16 +0000161}
162
163system_error::system_error(error_code ec, const string& what_arg)
164 : runtime_error(__init(ec, what_arg)),
165 __ec_(ec)
166{
167}
168
169system_error::system_error(error_code ec, const char* what_arg)
170 : runtime_error(__init(ec, what_arg)),
171 __ec_(ec)
172{
173}
174
175system_error::system_error(error_code ec)
176 : runtime_error(__init(ec, "")),
177 __ec_(ec)
178{
179}
180
181system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
182 : runtime_error(__init(error_code(ev, ecat), what_arg)),
183 __ec_(error_code(ev, ecat))
184{
185}
186
187system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
188 : runtime_error(__init(error_code(ev, ecat), what_arg)),
189 __ec_(error_code(ev, ecat))
190{
191}
192
193system_error::system_error(int ev, const error_category& ecat)
194 : runtime_error(__init(error_code(ev, ecat), "")),
195 __ec_(error_code(ev, ecat))
196{
197}
198
Howard Hinnanta62f2892011-05-26 19:48:01 +0000199system_error::~system_error() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16 +0000200{
201}
202
203void
204__throw_system_error(int ev, const char* what_arg)
205{
Howard Hinnant54b409f2010-08-11 17:04:31 +0000206#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16 +0000207 throw system_error(error_code(ev, system_category()), what_arg);
Howard Hinnante00e6f22013-03-28 18:56:26 +0000208#else
209 (void)ev;
210 (void)what_arg;
Howard Hinnant54b409f2010-08-11 17:04:31 +0000211#endif
Howard Hinnant3e519522010-05-11 19:42:16 +0000212}
213
214_LIBCPP_END_NAMESPACE_STD