blob: 9c8adc4f323e42bc57d66c137a06199fcbf91698 [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
Jonathan Roelofsb9420932014-09-02 20:34:23 +000011#include "__config"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000012#include "system_error"
13#include "string"
14#include "cstring"
15
16_LIBCPP_BEGIN_NAMESPACE_STD
17
18// class error_category
19
Howard Hinnant1e15fd12011-05-26 19:48:01 +000020error_category::error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000021{
22}
23
Howard Hinnant1e15fd12011-05-26 19:48:01 +000024error_category::~error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025{
26}
27
28error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +000029error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000030{
31 return error_condition(ev, *this);
32}
33
34bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000035error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000036{
37 return default_error_condition(code) == condition;
38}
39
40bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000041error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000042{
43 return *this == code.category() && code.value() == condition;
44}
45
46string
47__do_message::message(int ev) const
48{
49 return string(strerror(ev));
50}
51
52class _LIBCPP_HIDDEN __generic_error_category
53 : public __do_message
54{
55public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000056 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057 virtual string message(int ev) const;
58};
59
60const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000061__generic_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000062{
63 return "generic";
64}
65
66string
67__generic_error_category::message(int ev) const
68{
Jonathan Roelofsb9420932014-09-02 20:34:23 +000069#ifdef _LIBCPP_ELAST
70 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +000071 return string("unspecified generic_category error");
Jonathan Roelofsb9420932014-09-02 20:34:23 +000072#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +000073 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000074}
75
76const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +000077generic_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000078{
79 static __generic_error_category s;
80 return s;
81}
82
83class _LIBCPP_HIDDEN __system_error_category
84 : public __do_message
85{
86public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000087 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000088 virtual string message(int ev) const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000089 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000090};
91
92const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000093__system_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094{
95 return "system";
96}
97
98string
99__system_error_category::message(int ev) const
100{
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000101#ifdef _LIBCPP_ELAST
102 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +0000103 return string("unspecified system_category error");
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000104#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000105 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000106}
107
108error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000109__system_error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000110{
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000111#ifdef _LIBCPP_ELAST
112 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +0000113 return error_condition(ev, system_category());
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000114#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000115 return error_condition(ev, generic_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116}
117
118const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000119system_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120{
121 static __system_error_category s;
122 return s;
123}
124
125// error_condition
126
127string
128error_condition::message() const
129{
130 return __cat_->message(__val_);
131}
132
133// error_code
134
135string
136error_code::message() const
137{
138 return __cat_->message(__val_);
139}
140
141// system_error
142
143string
144system_error::__init(const error_code& ec, string what_arg)
145{
146 if (ec)
147 {
148 if (!what_arg.empty())
149 what_arg += ": ";
150 what_arg += ec.message();
151 }
Howard Hinnant0949eed2011-06-30 21:18:19 +0000152 return _VSTD::move(what_arg);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153}
154
155system_error::system_error(error_code ec, const string& what_arg)
156 : runtime_error(__init(ec, what_arg)),
157 __ec_(ec)
158{
159}
160
161system_error::system_error(error_code ec, const char* what_arg)
162 : runtime_error(__init(ec, what_arg)),
163 __ec_(ec)
164{
165}
166
167system_error::system_error(error_code ec)
168 : runtime_error(__init(ec, "")),
169 __ec_(ec)
170{
171}
172
173system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
174 : runtime_error(__init(error_code(ev, ecat), what_arg)),
175 __ec_(error_code(ev, ecat))
176{
177}
178
179system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
180 : runtime_error(__init(error_code(ev, ecat), what_arg)),
181 __ec_(error_code(ev, ecat))
182{
183}
184
185system_error::system_error(int ev, const error_category& ecat)
186 : runtime_error(__init(error_code(ev, ecat), "")),
187 __ec_(error_code(ev, ecat))
188{
189}
190
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000191system_error::~system_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000192{
193}
194
195void
196__throw_system_error(int ev, const char* what_arg)
197{
Howard Hinnantd4444702010-08-11 17:04:31 +0000198#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199 throw system_error(error_code(ev, system_category()), what_arg);
Howard Hinnantdb4d4782013-03-28 18:56:26 +0000200#else
201 (void)ev;
202 (void)what_arg;
Howard Hinnantd4444702010-08-11 17:04:31 +0000203#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204}
205
206_LIBCPP_END_NAMESPACE_STD