blob: 00920ffd616e2b30a0e8c7728a86f9fad0149c5e [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===---------------------- system_error.cpp ------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-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 Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
David Majnemercb036e32014-03-17 20:13:54 +000010#define _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnantbc8d3f92010-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 Hinnant1e15fd12011-05-26 19:48:01 +000019error_category::error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000020{
21}
22
Howard Hinnant1e15fd12011-05-26 19:48:01 +000023error_category::~error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024{
25}
26
27error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +000028error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000029{
30 return error_condition(ev, *this);
31}
32
33bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000034error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000035{
36 return default_error_condition(code) == condition;
37}
38
39bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000040error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-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 Hinnant1e15fd12011-05-26 19:48:01 +000055 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 virtual string message(int ev) const;
57};
58
59const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000060__generic_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000061{
62 return "generic";
63}
64
65string
66__generic_error_category::message(int ev) const
67{
Howard Hinnantadff4892010-05-24 17:49:41 +000068#ifdef ELAST
69 if (ev > ELAST)
70 return string("unspecified generic_category error");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000071#endif // ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +000072 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073}
74
75const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +000076generic_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077{
78 static __generic_error_category s;
79 return s;
80}
81
82class _LIBCPP_HIDDEN __system_error_category
83 : public __do_message
84{
85public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000086 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000087 virtual string message(int ev) const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000088 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000089};
90
91const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000092__system_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093{
94 return "system";
95}
96
97string
98__system_error_category::message(int ev) const
99{
Howard Hinnantadff4892010-05-24 17:49:41 +0000100#ifdef ELAST
101 if (ev > ELAST)
102 return string("unspecified system_category error");
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000103#endif // ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000104 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105}
106
107error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000108__system_error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109{
Howard Hinnantadff4892010-05-24 17:49:41 +0000110#ifdef ELAST
111 if (ev > ELAST)
112 return error_condition(ev, system_category());
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000113#endif // ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000114 return error_condition(ev, generic_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000115}
116
117const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000118system_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119{
120 static __system_error_category s;
121 return s;
122}
123
124// error_condition
125
126string
127error_condition::message() const
128{
129 return __cat_->message(__val_);
130}
131
132// error_code
133
134string
135error_code::message() const
136{
137 return __cat_->message(__val_);
138}
139
140// system_error
141
142string
143system_error::__init(const error_code& ec, string what_arg)
144{
145 if (ec)
146 {
147 if (!what_arg.empty())
148 what_arg += ": ";
149 what_arg += ec.message();
150 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000151 return _VSTD::move(what_arg);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152}
153
154system_error::system_error(error_code ec, const string& what_arg)
155 : runtime_error(__init(ec, what_arg)),
156 __ec_(ec)
157{
158}
159
160system_error::system_error(error_code ec, const char* what_arg)
161 : runtime_error(__init(ec, what_arg)),
162 __ec_(ec)
163{
164}
165
166system_error::system_error(error_code ec)
167 : runtime_error(__init(ec, "")),
168 __ec_(ec)
169{
170}
171
172system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
173 : runtime_error(__init(error_code(ev, ecat), what_arg)),
174 __ec_(error_code(ev, ecat))
175{
176}
177
178system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
179 : runtime_error(__init(error_code(ev, ecat), what_arg)),
180 __ec_(error_code(ev, ecat))
181{
182}
183
184system_error::system_error(int ev, const error_category& ecat)
185 : runtime_error(__init(error_code(ev, ecat), "")),
186 __ec_(error_code(ev, ecat))
187{
188}
189
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000190system_error::~system_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191{
192}
193
194void
195__throw_system_error(int ev, const char* what_arg)
196{
Howard Hinnantd4444702010-08-11 17:04:31 +0000197#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198 throw system_error(error_code(ev, system_category()), what_arg);
Howard Hinnantdb4d4782013-03-28 18:56:26 +0000199#else
200 (void)ev;
201 (void)what_arg;
Howard Hinnantd4444702010-08-11 17:04:31 +0000202#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203}
204
205_LIBCPP_END_NAMESPACE_STD