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