1227825Stheraven//===---------------------- system_error.cpp ------------------------------===//
2227825Stheraven//
3227825Stheraven//                     The LLVM Compiler Infrastructure
4227825Stheraven//
5227825Stheraven// This file is dual licensed under the MIT and the University of Illinois Open
6227825Stheraven// Source Licenses. See LICENSE.TXT for details.
7227825Stheraven//
8227825Stheraven//===----------------------------------------------------------------------===//
9227825Stheraven
10278724Sdim#define _LIBCPP_BUILDING_SYSTEM_ERROR
11278724Sdim#include "__config"
12227825Stheraven#include "system_error"
13227825Stheraven#include "string"
14227825Stheraven#include "cstring"
15227825Stheraven
16227825Stheraven_LIBCPP_BEGIN_NAMESPACE_STD
17227825Stheraven
18227825Stheraven// class error_category
19227825Stheraven
20227825Stheravenerror_category::error_category() _NOEXCEPT
21227825Stheraven{
22227825Stheraven}
23227825Stheraven
24227825Stheravenerror_category::~error_category() _NOEXCEPT
25227825Stheraven{
26227825Stheraven}
27227825Stheraven
28227825Stheravenerror_condition
29227825Stheravenerror_category::default_error_condition(int ev) const _NOEXCEPT
30227825Stheraven{
31227825Stheraven    return error_condition(ev, *this);
32227825Stheraven}
33227825Stheraven
34227825Stheravenbool
35227825Stheravenerror_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
36227825Stheraven{
37227825Stheraven    return default_error_condition(code) == condition;
38227825Stheraven}
39227825Stheraven
40227825Stheravenbool
41227825Stheravenerror_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
42227825Stheraven{
43227825Stheraven    return *this == code.category() && code.value() == condition;
44227825Stheraven}
45227825Stheraven
46227825Stheravenstring
47227825Stheraven__do_message::message(int ev) const
48227825Stheraven{
49227825Stheraven    return string(strerror(ev));
50227825Stheraven}
51227825Stheraven
52227825Stheravenclass _LIBCPP_HIDDEN __generic_error_category
53227825Stheraven    : public __do_message
54227825Stheraven{
55227825Stheravenpublic:
56227825Stheraven    virtual const char* name() const _NOEXCEPT;
57227825Stheraven    virtual string message(int ev) const;
58227825Stheraven};
59227825Stheraven
60227825Stheravenconst char*
61227825Stheraven__generic_error_category::name() const _NOEXCEPT
62227825Stheraven{
63227825Stheraven    return "generic";
64227825Stheraven}
65227825Stheraven
66227825Stheravenstring
67227825Stheraven__generic_error_category::message(int ev) const
68227825Stheraven{
69278724Sdim#ifdef _LIBCPP_ELAST
70278724Sdim    if (ev > _LIBCPP_ELAST)
71227825Stheraven      return string("unspecified generic_category error");
72278724Sdim#endif  // _LIBCPP_ELAST
73227825Stheraven    return __do_message::message(ev);
74227825Stheraven}
75227825Stheraven
76227825Stheravenconst error_category&
77227825Stheravengeneric_category() _NOEXCEPT
78227825Stheraven{
79227825Stheraven    static __generic_error_category s;
80227825Stheraven    return s;
81227825Stheraven}
82227825Stheraven
83227825Stheravenclass _LIBCPP_HIDDEN __system_error_category
84227825Stheraven    : public __do_message
85227825Stheraven{
86227825Stheravenpublic:
87227825Stheraven    virtual const char* name() const _NOEXCEPT;
88227825Stheraven    virtual string message(int ev) const;
89227825Stheraven    virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
90227825Stheraven};
91227825Stheraven
92227825Stheravenconst char*
93227825Stheraven__system_error_category::name() const _NOEXCEPT
94227825Stheraven{
95227825Stheraven    return "system";
96227825Stheraven}
97227825Stheraven
98227825Stheravenstring
99227825Stheraven__system_error_category::message(int ev) const
100227825Stheraven{
101278724Sdim#ifdef _LIBCPP_ELAST
102278724Sdim    if (ev > _LIBCPP_ELAST)
103227825Stheraven      return string("unspecified system_category error");
104278724Sdim#endif  // _LIBCPP_ELAST
105227825Stheraven    return __do_message::message(ev);
106227825Stheraven}
107227825Stheraven
108227825Stheravenerror_condition
109227825Stheraven__system_error_category::default_error_condition(int ev) const _NOEXCEPT
110227825Stheraven{
111278724Sdim#ifdef _LIBCPP_ELAST
112278724Sdim    if (ev > _LIBCPP_ELAST)
113227825Stheraven      return error_condition(ev, system_category());
114278724Sdim#endif  // _LIBCPP_ELAST
115227825Stheraven    return error_condition(ev, generic_category());
116227825Stheraven}
117227825Stheraven
118227825Stheravenconst error_category&
119227825Stheravensystem_category() _NOEXCEPT
120227825Stheraven{
121227825Stheraven    static __system_error_category s;
122227825Stheraven    return s;
123227825Stheraven}
124227825Stheraven
125227825Stheraven// error_condition
126227825Stheraven
127227825Stheravenstring
128227825Stheravenerror_condition::message() const
129227825Stheraven{
130227825Stheraven    return __cat_->message(__val_);
131227825Stheraven}
132227825Stheraven
133227825Stheraven// error_code
134227825Stheraven
135227825Stheravenstring
136227825Stheravenerror_code::message() const
137227825Stheraven{
138227825Stheraven    return __cat_->message(__val_);
139227825Stheraven}
140227825Stheraven
141227825Stheraven// system_error
142227825Stheraven
143227825Stheravenstring
144227825Stheravensystem_error::__init(const error_code& ec, string what_arg)
145227825Stheraven{
146227825Stheraven    if (ec)
147227825Stheraven    {
148227825Stheraven        if (!what_arg.empty())
149227825Stheraven            what_arg += ": ";
150227825Stheraven        what_arg += ec.message();
151227825Stheraven    }
152227825Stheraven    return _VSTD::move(what_arg);
153227825Stheraven}
154227825Stheraven
155227825Stheravensystem_error::system_error(error_code ec, const string& what_arg)
156227825Stheraven    : runtime_error(__init(ec, what_arg)),
157227825Stheraven      __ec_(ec)
158227825Stheraven{
159227825Stheraven}
160227825Stheraven
161227825Stheravensystem_error::system_error(error_code ec, const char* what_arg)
162227825Stheraven    : runtime_error(__init(ec, what_arg)),
163227825Stheraven      __ec_(ec)
164227825Stheraven{
165227825Stheraven}
166227825Stheraven
167227825Stheravensystem_error::system_error(error_code ec)
168227825Stheraven    : runtime_error(__init(ec, "")),
169227825Stheraven      __ec_(ec)
170227825Stheraven{
171227825Stheraven}
172227825Stheraven
173227825Stheravensystem_error::system_error(int ev, const error_category& ecat, const string& what_arg)
174227825Stheraven    : runtime_error(__init(error_code(ev, ecat), what_arg)),
175227825Stheraven      __ec_(error_code(ev, ecat))
176227825Stheraven{
177227825Stheraven}
178227825Stheraven
179227825Stheravensystem_error::system_error(int ev, const error_category& ecat, const char* what_arg)
180227825Stheraven    : runtime_error(__init(error_code(ev, ecat), what_arg)),
181227825Stheraven      __ec_(error_code(ev, ecat))
182227825Stheraven{
183227825Stheraven}
184227825Stheraven
185227825Stheravensystem_error::system_error(int ev, const error_category& ecat)
186227825Stheraven    : runtime_error(__init(error_code(ev, ecat), "")),
187227825Stheraven      __ec_(error_code(ev, ecat))
188227825Stheraven{
189227825Stheraven}
190227825Stheraven
191227825Stheravensystem_error::~system_error() _NOEXCEPT
192227825Stheraven{
193227825Stheraven}
194227825Stheraven
195227825Stheravenvoid
196227825Stheraven__throw_system_error(int ev, const char* what_arg)
197227825Stheraven{
198227825Stheraven#ifndef _LIBCPP_NO_EXCEPTIONS
199227825Stheraven    throw system_error(error_code(ev, system_category()), what_arg);
200249998Sdim#else
201249998Sdim    (void)ev;
202249998Sdim    (void)what_arg;
203227825Stheraven#endif
204227825Stheraven}
205227825Stheraven
206227825Stheraven_LIBCPP_END_NAMESPACE_STD
207