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
23error_category::error_category() _NOEXCEPT
24{
25}
26
27error_category::~error_category() _NOEXCEPT
28{
29}
30
31error_condition
32error_category::default_error_condition(int ev) const _NOEXCEPT
33{
34    return error_condition(ev, *this);
35}
36
37bool
38error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
39{
40    return default_error_condition(code) == condition;
41}
42
43bool
44error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
45{
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:
59    virtual const char* name() const _NOEXCEPT;
60    virtual string message(int ev) const;
61};
62
63const char*
64__generic_error_category::name() const _NOEXCEPT
65{
66    return "generic";
67}
68
69string
70__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
79const error_category&
80generic_category() _NOEXCEPT
81{
82    static __generic_error_category s;
83    return s;
84}
85
86class _LIBCPP_HIDDEN __system_error_category
87    : public __do_message
88{
89public:
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
95const char*
96__system_error_category::name() const _NOEXCEPT
97{
98    return "system";
99}
100
101string
102__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
111error_condition
112__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
121const error_category&
122system_category() _NOEXCEPT
123{
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    }
155    return _VSTD::move(what_arg);
156}
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
194system_error::~system_error() _NOEXCEPT
195{
196}
197
198void
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