etc/services - sync with NetBSD-8
[minix.git] / external / bsd / libc++ / dist / libcxx / src / system_error.cpp
blob3023e200aa34d18a8a568723d6eec41a36821cc7
1 //===---------------------- system_error.cpp ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "__config"
12 #define _LIBCPP_BUILDING_SYSTEM_ERROR
13 #include "system_error"
15 #include "include/config_elast.h"
16 #include "cstring"
17 #include "string"
19 _LIBCPP_BEGIN_NAMESPACE_STD
21 // class error_category
23 error_category::error_category() _NOEXCEPT
27 error_category::~error_category() _NOEXCEPT
31 error_condition
32 error_category::default_error_condition(int ev) const _NOEXCEPT
34 return error_condition(ev, *this);
37 bool
38 error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
40 return default_error_condition(code) == condition;
43 bool
44 error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
46 return *this == code.category() && code.value() == condition;
49 string
50 __do_message::message(int ev) const
52 return string(strerror(ev));
55 class _LIBCPP_HIDDEN __generic_error_category
56 : public __do_message
58 public:
59 virtual const char* name() const _NOEXCEPT;
60 virtual string message(int ev) const;
63 const char*
64 __generic_error_category::name() const _NOEXCEPT
66 return "generic";
69 string
70 __generic_error_category::message(int ev) const
72 #ifdef _LIBCPP_ELAST
73 if (ev > _LIBCPP_ELAST)
74 return string("unspecified generic_category error");
75 #endif // _LIBCPP_ELAST
76 return __do_message::message(ev);
79 const error_category&
80 generic_category() _NOEXCEPT
82 static __generic_error_category s;
83 return s;
86 class _LIBCPP_HIDDEN __system_error_category
87 : public __do_message
89 public:
90 virtual const char* name() const _NOEXCEPT;
91 virtual string message(int ev) const;
92 virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
95 const char*
96 __system_error_category::name() const _NOEXCEPT
98 return "system";
101 string
102 __system_error_category::message(int ev) const
104 #ifdef _LIBCPP_ELAST
105 if (ev > _LIBCPP_ELAST)
106 return string("unspecified system_category error");
107 #endif // _LIBCPP_ELAST
108 return __do_message::message(ev);
111 error_condition
112 __system_error_category::default_error_condition(int ev) const _NOEXCEPT
114 #ifdef _LIBCPP_ELAST
115 if (ev > _LIBCPP_ELAST)
116 return error_condition(ev, system_category());
117 #endif // _LIBCPP_ELAST
118 return error_condition(ev, generic_category());
121 const error_category&
122 system_category() _NOEXCEPT
124 static __system_error_category s;
125 return s;
128 // error_condition
130 string
131 error_condition::message() const
133 return __cat_->message(__val_);
136 // error_code
138 string
139 error_code::message() const
141 return __cat_->message(__val_);
144 // system_error
146 string
147 system_error::__init(const error_code& ec, string what_arg)
149 if (ec)
151 if (!what_arg.empty())
152 what_arg += ": ";
153 what_arg += ec.message();
155 return what_arg;
158 system_error::system_error(error_code ec, const string& what_arg)
159 : runtime_error(__init(ec, what_arg)),
160 __ec_(ec)
164 system_error::system_error(error_code ec, const char* what_arg)
165 : runtime_error(__init(ec, what_arg)),
166 __ec_(ec)
170 system_error::system_error(error_code ec)
171 : runtime_error(__init(ec, "")),
172 __ec_(ec)
176 system_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))
182 system_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))
188 system_error::system_error(int ev, const error_category& ecat)
189 : runtime_error(__init(error_code(ev, ecat), "")),
190 __ec_(error_code(ev, ecat))
194 system_error::~system_error() _NOEXCEPT
198 void
199 __throw_system_error(int ev, const char* what_arg)
201 #ifndef _LIBCPP_NO_EXCEPTIONS
202 throw system_error(error_code(ev, system_category()), what_arg);
203 #else
204 (void)ev;
205 (void)what_arg;
206 #endif
209 _LIBCPP_END_NAMESPACE_STD