system_error.cpp revision 262801
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#define	_LIBCPP_BUILDING_SYSTEM_ERROR
11#include "system_error"
12#include "string"
13#include "cstring"
14
15_LIBCPP_BEGIN_NAMESPACE_STD
16
17// class error_category
18
19error_category::error_category() _NOEXCEPT
20{
21}
22
23error_category::~error_category() _NOEXCEPT
24{
25}
26
27error_condition
28error_category::default_error_condition(int ev) const _NOEXCEPT
29{
30    return error_condition(ev, *this);
31}
32
33bool
34error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
35{
36    return default_error_condition(code) == condition;
37}
38
39bool
40error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
41{
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:
55    virtual const char* name() const _NOEXCEPT;
56    virtual string message(int ev) const;
57};
58
59const char*
60__generic_error_category::name() const _NOEXCEPT
61{
62    return "generic";
63}
64
65string
66__generic_error_category::message(int ev) const
67{
68#ifdef ELAST
69    if (ev > ELAST)
70      return string("unspecified generic_category error");
71#endif  // ELAST
72    return __do_message::message(ev);
73}
74
75const error_category&
76generic_category() _NOEXCEPT
77{
78    static __generic_error_category s;
79    return s;
80}
81
82class _LIBCPP_HIDDEN __system_error_category
83    : public __do_message
84{
85public:
86    virtual const char* name() const _NOEXCEPT;
87    virtual string message(int ev) const;
88    virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
89};
90
91const char*
92__system_error_category::name() const _NOEXCEPT
93{
94    return "system";
95}
96
97string
98__system_error_category::message(int ev) const
99{
100#ifdef ELAST
101    if (ev > ELAST)
102      return string("unspecified system_category error");
103#endif  // ELAST
104    return __do_message::message(ev);
105}
106
107error_condition
108__system_error_category::default_error_condition(int ev) const _NOEXCEPT
109{
110#ifdef ELAST
111    if (ev > ELAST)
112      return error_condition(ev, system_category());
113#endif  // ELAST
114    return error_condition(ev, generic_category());
115}
116
117const error_category&
118system_category() _NOEXCEPT
119{
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    }
151    return _VSTD::move(what_arg);
152}
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
190system_error::~system_error() _NOEXCEPT
191{
192}
193
194void
195__throw_system_error(int ev, const char* what_arg)
196{
197#ifndef _LIBCPP_NO_EXCEPTIONS
198    throw system_error(error_code(ev, system_category()), what_arg);
199#else
200    (void)ev;
201    (void)what_arg;
202#endif
203}
204
205_LIBCPP_END_NAMESPACE_STD
206