• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //===----------------------------------------------------------------------===//
9 
10 #include "__config"
11 
12 #define _LIBCPP_BUILDING_SYSTEM_ERROR
13 #include "system_error"
14 
15 #include "config_elast.h"
16 #include "cstring"
17 #include "string"
18 
19 _LIBCPP_BEGIN_NAMESPACE_STD
20 
21 // class error_category
22 
error_category()23 error_category::error_category() _NOEXCEPT
24 {
25 }
26 
~error_category()27 error_category::~error_category() _NOEXCEPT
28 {
29 }
30 
31 error_condition
default_error_condition(int ev) const32 error_category::default_error_condition(int ev) const _NOEXCEPT
33 {
34     return error_condition(ev, *this);
35 }
36 
37 bool
equivalent(int code,const error_condition & condition) const38 error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
39 {
40     return default_error_condition(code) == condition;
41 }
42 
43 bool
equivalent(const error_code & code,int condition) const44 error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
45 {
46     return *this == code.category() && code.value() == condition;
47 }
48 
49 string
message(int ev) const50 __do_message::message(int ev) const
51 {
52     return string(strerror(ev));
53 }
54 
55 class _LIBCPP_HIDDEN __generic_error_category
56     : public __do_message
57 {
58 public:
59     virtual const char* name() const _NOEXCEPT;
60     virtual string message(int ev) const;
61 };
62 
63 const char*
name() const64 __generic_error_category::name() const _NOEXCEPT
65 {
66     return "generic";
67 }
68 
69 string
message(int ev) const70 __generic_error_category::message(int ev) const
71 {
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);
77 }
78 
79 const error_category&
generic_category()80 generic_category() _NOEXCEPT
81 {
82     static __generic_error_category s;
83     return s;
84 }
85 
86 class _LIBCPP_HIDDEN __system_error_category
87     : public __do_message
88 {
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;
93 };
94 
95 const char*
name() const96 __system_error_category::name() const _NOEXCEPT
97 {
98     return "system";
99 }
100 
101 string
message(int ev) const102 __system_error_category::message(int ev) const
103 {
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);
109 }
110 
111 error_condition
default_error_condition(int ev) const112 __system_error_category::default_error_condition(int ev) const _NOEXCEPT
113 {
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());
119 }
120 
121 const error_category&
system_category()122 system_category() _NOEXCEPT
123 {
124     static __system_error_category s;
125     return s;
126 }
127 
128 // error_condition
129 
130 string
message() const131 error_condition::message() const
132 {
133     return __cat_->message(__val_);
134 }
135 
136 // error_code
137 
138 string
message() const139 error_code::message() const
140 {
141     return __cat_->message(__val_);
142 }
143 
144 // system_error
145 
146 string
__init(const error_code & ec,string what_arg)147 system_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     }
155     return what_arg;
156 }
157 
system_error(error_code ec,const string & what_arg)158 system_error::system_error(error_code ec, const string& what_arg)
159     : runtime_error(__init(ec, what_arg)),
160       __ec_(ec)
161 {
162 }
163 
system_error(error_code ec,const char * what_arg)164 system_error::system_error(error_code ec, const char* what_arg)
165     : runtime_error(__init(ec, what_arg)),
166       __ec_(ec)
167 {
168 }
169 
system_error(error_code ec)170 system_error::system_error(error_code ec)
171     : runtime_error(__init(ec, "")),
172       __ec_(ec)
173 {
174 }
175 
system_error(int ev,const error_category & ecat,const string & what_arg)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))
179 {
180 }
181 
system_error(int ev,const error_category & ecat,const char * what_arg)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))
185 {
186 }
187 
system_error(int ev,const error_category & 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))
191 {
192 }
193 
~system_error()194 system_error::~system_error() _NOEXCEPT
195 {
196 }
197 
198 void
__throw_system_error(int ev,const char * what_arg)199 __throw_system_error(int ev, const char* what_arg)
200 {
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
207 }
208 
209 _LIBCPP_END_NAMESPACE_STD
210