blob: 18f668f071cdf7b3d240c4c5878bcb301575263f [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
Jonathan Roelofsb9420932014-09-02 20:34:23 +000010#include "__config"
Dan Albert656850f2015-01-06 17:34:51 +000011
12#define _LIBCPP_BUILDING_SYSTEM_ERROR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000013#include "system_error"
Dan Albert656850f2015-01-06 17:34:51 +000014
Dan Albert1d4a1ed2016-05-25 22:36:09 -070015#include "config_elast.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000016#include "cstring"
Dan Albert656850f2015-01-06 17:34:51 +000017#include "string"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000018
19_LIBCPP_BEGIN_NAMESPACE_STD
20
21// class error_category
22
Howard Hinnant1e15fd12011-05-26 19:48:01 +000023error_category::error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024{
25}
26
Howard Hinnant1e15fd12011-05-26 19:48:01 +000027error_category::~error_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028{
29}
30
31error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +000032error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033{
34 return error_condition(ev, *this);
35}
36
37bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000038error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039{
40 return default_error_condition(code) == condition;
41}
42
43bool
Howard Hinnant1e15fd12011-05-26 19:48:01 +000044error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045{
46 return *this == code.category() && code.value() == condition;
47}
48
49string
50__do_message::message(int ev) const
51{
52 return string(strerror(ev));
53}
54
55class _LIBCPP_HIDDEN __generic_error_category
56 : public __do_message
57{
58public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000059 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000060 virtual string message(int ev) const;
61};
62
63const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000064__generic_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065{
66 return "generic";
67}
68
69string
70__generic_error_category::message(int ev) const
71{
Jonathan Roelofsb9420932014-09-02 20:34:23 +000072#ifdef _LIBCPP_ELAST
73 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +000074 return string("unspecified generic_category error");
Jonathan Roelofsb9420932014-09-02 20:34:23 +000075#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +000076 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000077}
78
79const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +000080generic_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000081{
82 static __generic_error_category s;
83 return s;
84}
85
86class _LIBCPP_HIDDEN __system_error_category
87 : public __do_message
88{
89public:
Howard Hinnant1e15fd12011-05-26 19:48:01 +000090 virtual const char* name() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000091 virtual string message(int ev) const;
Howard Hinnant1e15fd12011-05-26 19:48:01 +000092 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093};
94
95const char*
Howard Hinnant1e15fd12011-05-26 19:48:01 +000096__system_error_category::name() const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097{
98 return "system";
99}
100
101string
102__system_error_category::message(int ev) const
103{
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000104#ifdef _LIBCPP_ELAST
105 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +0000106 return string("unspecified system_category error");
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000107#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000108 return __do_message::message(ev);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109}
110
111error_condition
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000112__system_error_category::default_error_condition(int ev) const _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113{
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000114#ifdef _LIBCPP_ELAST
115 if (ev > _LIBCPP_ELAST)
Howard Hinnantadff4892010-05-24 17:49:41 +0000116 return error_condition(ev, system_category());
Jonathan Roelofsb9420932014-09-02 20:34:23 +0000117#endif // _LIBCPP_ELAST
Howard Hinnantadff4892010-05-24 17:49:41 +0000118 return error_condition(ev, generic_category());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119}
120
121const error_category&
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000122system_category() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
124 static __system_error_category s;
125 return s;
126}
127
128// error_condition
129
130string
131error_condition::message() const
132{
133 return __cat_->message(__val_);
134}
135
136// error_code
137
138string
139error_code::message() const
140{
141 return __cat_->message(__val_);
142}
143
144// system_error
145
146string
147system_error::__init(const error_code& ec, string what_arg)
148{
149 if (ec)
150 {
151 if (!what_arg.empty())
152 what_arg += ": ";
153 what_arg += ec.message();
154 }
Richard Trieu14dbb252015-04-30 21:47:28 +0000155 return what_arg;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000156}
157
158system_error::system_error(error_code ec, const string& what_arg)
159 : runtime_error(__init(ec, what_arg)),
160 __ec_(ec)
161{
162}
163
164system_error::system_error(error_code ec, const char* what_arg)
165 : runtime_error(__init(ec, what_arg)),
166 __ec_(ec)
167{
168}
169
170system_error::system_error(error_code ec)
171 : runtime_error(__init(ec, "")),
172 __ec_(ec)
173{
174}
175
176system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
177 : runtime_error(__init(error_code(ev, ecat), what_arg)),
178 __ec_(error_code(ev, ecat))
179{
180}
181
182system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
183 : runtime_error(__init(error_code(ev, ecat), what_arg)),
184 __ec_(error_code(ev, ecat))
185{
186}
187
188system_error::system_error(int ev, const error_category& ecat)
189 : runtime_error(__init(error_code(ev, ecat), "")),
190 __ec_(error_code(ev, ecat))
191{
192}
193
Howard Hinnant1e15fd12011-05-26 19:48:01 +0000194system_error::~system_error() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000195{
196}
197
198void
199__throw_system_error(int ev, const char* what_arg)
200{
Howard Hinnantd4444702010-08-11 17:04:31 +0000201#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202 throw system_error(error_code(ev, system_category()), what_arg);
Howard Hinnantdb4d4782013-03-28 18:56:26 +0000203#else
204 (void)ev;
205 (void)what_arg;
Howard Hinnantd4444702010-08-11 17:04:31 +0000206#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000207}
208
209_LIBCPP_END_NAMESPACE_STD