xref: /minix3/external/bsd/libc++/dist/libcxx/src/system_error.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
14684ddb6SLionel Sambuc //===---------------------- system_error.cpp ------------------------------===//
24684ddb6SLionel Sambuc //
34684ddb6SLionel Sambuc //                     The LLVM Compiler Infrastructure
44684ddb6SLionel Sambuc //
54684ddb6SLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
64684ddb6SLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
74684ddb6SLionel Sambuc //
84684ddb6SLionel Sambuc //===----------------------------------------------------------------------===//
94684ddb6SLionel Sambuc 
10*0a6a1f1dSLionel Sambuc #include "__config"
11*0a6a1f1dSLionel Sambuc 
124684ddb6SLionel Sambuc #define _LIBCPP_BUILDING_SYSTEM_ERROR
134684ddb6SLionel Sambuc #include "system_error"
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include "include/config_elast.h"
164684ddb6SLionel Sambuc #include "cstring"
17*0a6a1f1dSLionel Sambuc #include "string"
184684ddb6SLionel Sambuc 
194684ddb6SLionel Sambuc _LIBCPP_BEGIN_NAMESPACE_STD
204684ddb6SLionel Sambuc 
214684ddb6SLionel Sambuc // class error_category
224684ddb6SLionel Sambuc 
error_category()234684ddb6SLionel Sambuc error_category::error_category() _NOEXCEPT
244684ddb6SLionel Sambuc {
254684ddb6SLionel Sambuc }
264684ddb6SLionel Sambuc 
~error_category()274684ddb6SLionel Sambuc error_category::~error_category() _NOEXCEPT
284684ddb6SLionel Sambuc {
294684ddb6SLionel Sambuc }
304684ddb6SLionel Sambuc 
314684ddb6SLionel Sambuc error_condition
default_error_condition(int ev) const324684ddb6SLionel Sambuc error_category::default_error_condition(int ev) const _NOEXCEPT
334684ddb6SLionel Sambuc {
344684ddb6SLionel Sambuc     return error_condition(ev, *this);
354684ddb6SLionel Sambuc }
364684ddb6SLionel Sambuc 
374684ddb6SLionel Sambuc bool
equivalent(int code,const error_condition & condition) const384684ddb6SLionel Sambuc error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT
394684ddb6SLionel Sambuc {
404684ddb6SLionel Sambuc     return default_error_condition(code) == condition;
414684ddb6SLionel Sambuc }
424684ddb6SLionel Sambuc 
434684ddb6SLionel Sambuc bool
equivalent(const error_code & code,int condition) const444684ddb6SLionel Sambuc error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT
454684ddb6SLionel Sambuc {
464684ddb6SLionel Sambuc     return *this == code.category() && code.value() == condition;
474684ddb6SLionel Sambuc }
484684ddb6SLionel Sambuc 
494684ddb6SLionel Sambuc string
message(int ev) const504684ddb6SLionel Sambuc __do_message::message(int ev) const
514684ddb6SLionel Sambuc {
524684ddb6SLionel Sambuc     return string(strerror(ev));
534684ddb6SLionel Sambuc }
544684ddb6SLionel Sambuc 
554684ddb6SLionel Sambuc class _LIBCPP_HIDDEN __generic_error_category
564684ddb6SLionel Sambuc     : public __do_message
574684ddb6SLionel Sambuc {
584684ddb6SLionel Sambuc public:
594684ddb6SLionel Sambuc     virtual const char* name() const _NOEXCEPT;
604684ddb6SLionel Sambuc     virtual string message(int ev) const;
614684ddb6SLionel Sambuc };
624684ddb6SLionel Sambuc 
634684ddb6SLionel Sambuc const char*
name() const644684ddb6SLionel Sambuc __generic_error_category::name() const _NOEXCEPT
654684ddb6SLionel Sambuc {
664684ddb6SLionel Sambuc     return "generic";
674684ddb6SLionel Sambuc }
684684ddb6SLionel Sambuc 
694684ddb6SLionel Sambuc string
message(int ev) const704684ddb6SLionel Sambuc __generic_error_category::message(int ev) const
714684ddb6SLionel Sambuc {
72*0a6a1f1dSLionel Sambuc #ifdef _LIBCPP_ELAST
73*0a6a1f1dSLionel Sambuc     if (ev > _LIBCPP_ELAST)
744684ddb6SLionel Sambuc       return string("unspecified generic_category error");
75*0a6a1f1dSLionel Sambuc #endif  // _LIBCPP_ELAST
764684ddb6SLionel Sambuc     return __do_message::message(ev);
774684ddb6SLionel Sambuc }
784684ddb6SLionel Sambuc 
794684ddb6SLionel Sambuc const error_category&
generic_category()804684ddb6SLionel Sambuc generic_category() _NOEXCEPT
814684ddb6SLionel Sambuc {
824684ddb6SLionel Sambuc     static __generic_error_category s;
834684ddb6SLionel Sambuc     return s;
844684ddb6SLionel Sambuc }
854684ddb6SLionel Sambuc 
864684ddb6SLionel Sambuc class _LIBCPP_HIDDEN __system_error_category
874684ddb6SLionel Sambuc     : public __do_message
884684ddb6SLionel Sambuc {
894684ddb6SLionel Sambuc public:
904684ddb6SLionel Sambuc     virtual const char* name() const _NOEXCEPT;
914684ddb6SLionel Sambuc     virtual string message(int ev) const;
924684ddb6SLionel Sambuc     virtual error_condition default_error_condition(int ev) const _NOEXCEPT;
934684ddb6SLionel Sambuc };
944684ddb6SLionel Sambuc 
954684ddb6SLionel Sambuc const char*
name() const964684ddb6SLionel Sambuc __system_error_category::name() const _NOEXCEPT
974684ddb6SLionel Sambuc {
984684ddb6SLionel Sambuc     return "system";
994684ddb6SLionel Sambuc }
1004684ddb6SLionel Sambuc 
1014684ddb6SLionel Sambuc string
message(int ev) const1024684ddb6SLionel Sambuc __system_error_category::message(int ev) const
1034684ddb6SLionel Sambuc {
104*0a6a1f1dSLionel Sambuc #ifdef _LIBCPP_ELAST
105*0a6a1f1dSLionel Sambuc     if (ev > _LIBCPP_ELAST)
1064684ddb6SLionel Sambuc       return string("unspecified system_category error");
107*0a6a1f1dSLionel Sambuc #endif  // _LIBCPP_ELAST
1084684ddb6SLionel Sambuc     return __do_message::message(ev);
1094684ddb6SLionel Sambuc }
1104684ddb6SLionel Sambuc 
1114684ddb6SLionel Sambuc error_condition
default_error_condition(int ev) const1124684ddb6SLionel Sambuc __system_error_category::default_error_condition(int ev) const _NOEXCEPT
1134684ddb6SLionel Sambuc {
114*0a6a1f1dSLionel Sambuc #ifdef _LIBCPP_ELAST
115*0a6a1f1dSLionel Sambuc     if (ev > _LIBCPP_ELAST)
1164684ddb6SLionel Sambuc       return error_condition(ev, system_category());
117*0a6a1f1dSLionel Sambuc #endif  // _LIBCPP_ELAST
1184684ddb6SLionel Sambuc     return error_condition(ev, generic_category());
1194684ddb6SLionel Sambuc }
1204684ddb6SLionel Sambuc 
1214684ddb6SLionel Sambuc const error_category&
system_category()1224684ddb6SLionel Sambuc system_category() _NOEXCEPT
1234684ddb6SLionel Sambuc {
1244684ddb6SLionel Sambuc     static __system_error_category s;
1254684ddb6SLionel Sambuc     return s;
1264684ddb6SLionel Sambuc }
1274684ddb6SLionel Sambuc 
1284684ddb6SLionel Sambuc // error_condition
1294684ddb6SLionel Sambuc 
1304684ddb6SLionel Sambuc string
message() const1314684ddb6SLionel Sambuc error_condition::message() const
1324684ddb6SLionel Sambuc {
1334684ddb6SLionel Sambuc     return __cat_->message(__val_);
1344684ddb6SLionel Sambuc }
1354684ddb6SLionel Sambuc 
1364684ddb6SLionel Sambuc // error_code
1374684ddb6SLionel Sambuc 
1384684ddb6SLionel Sambuc string
message() const1394684ddb6SLionel Sambuc error_code::message() const
1404684ddb6SLionel Sambuc {
1414684ddb6SLionel Sambuc     return __cat_->message(__val_);
1424684ddb6SLionel Sambuc }
1434684ddb6SLionel Sambuc 
1444684ddb6SLionel Sambuc // system_error
1454684ddb6SLionel Sambuc 
1464684ddb6SLionel Sambuc string
__init(const error_code & ec,string what_arg)1474684ddb6SLionel Sambuc system_error::__init(const error_code& ec, string what_arg)
1484684ddb6SLionel Sambuc {
1494684ddb6SLionel Sambuc     if (ec)
1504684ddb6SLionel Sambuc     {
1514684ddb6SLionel Sambuc         if (!what_arg.empty())
1524684ddb6SLionel Sambuc             what_arg += ": ";
1534684ddb6SLionel Sambuc         what_arg += ec.message();
1544684ddb6SLionel Sambuc     }
155*0a6a1f1dSLionel Sambuc     return what_arg;
1564684ddb6SLionel Sambuc }
1574684ddb6SLionel Sambuc 
system_error(error_code ec,const string & what_arg)1584684ddb6SLionel Sambuc system_error::system_error(error_code ec, const string& what_arg)
1594684ddb6SLionel Sambuc     : runtime_error(__init(ec, what_arg)),
1604684ddb6SLionel Sambuc       __ec_(ec)
1614684ddb6SLionel Sambuc {
1624684ddb6SLionel Sambuc }
1634684ddb6SLionel Sambuc 
system_error(error_code ec,const char * what_arg)1644684ddb6SLionel Sambuc system_error::system_error(error_code ec, const char* what_arg)
1654684ddb6SLionel Sambuc     : runtime_error(__init(ec, what_arg)),
1664684ddb6SLionel Sambuc       __ec_(ec)
1674684ddb6SLionel Sambuc {
1684684ddb6SLionel Sambuc }
1694684ddb6SLionel Sambuc 
system_error(error_code ec)1704684ddb6SLionel Sambuc system_error::system_error(error_code ec)
1714684ddb6SLionel Sambuc     : runtime_error(__init(ec, "")),
1724684ddb6SLionel Sambuc       __ec_(ec)
1734684ddb6SLionel Sambuc {
1744684ddb6SLionel Sambuc }
1754684ddb6SLionel Sambuc 
system_error(int ev,const error_category & ecat,const string & what_arg)1764684ddb6SLionel Sambuc system_error::system_error(int ev, const error_category& ecat, const string& what_arg)
1774684ddb6SLionel Sambuc     : runtime_error(__init(error_code(ev, ecat), what_arg)),
1784684ddb6SLionel Sambuc       __ec_(error_code(ev, ecat))
1794684ddb6SLionel Sambuc {
1804684ddb6SLionel Sambuc }
1814684ddb6SLionel Sambuc 
system_error(int ev,const error_category & ecat,const char * what_arg)1824684ddb6SLionel Sambuc system_error::system_error(int ev, const error_category& ecat, const char* what_arg)
1834684ddb6SLionel Sambuc     : runtime_error(__init(error_code(ev, ecat), what_arg)),
1844684ddb6SLionel Sambuc       __ec_(error_code(ev, ecat))
1854684ddb6SLionel Sambuc {
1864684ddb6SLionel Sambuc }
1874684ddb6SLionel Sambuc 
system_error(int ev,const error_category & ecat)1884684ddb6SLionel Sambuc system_error::system_error(int ev, const error_category& ecat)
1894684ddb6SLionel Sambuc     : runtime_error(__init(error_code(ev, ecat), "")),
1904684ddb6SLionel Sambuc       __ec_(error_code(ev, ecat))
1914684ddb6SLionel Sambuc {
1924684ddb6SLionel Sambuc }
1934684ddb6SLionel Sambuc 
~system_error()1944684ddb6SLionel Sambuc system_error::~system_error() _NOEXCEPT
1954684ddb6SLionel Sambuc {
1964684ddb6SLionel Sambuc }
1974684ddb6SLionel Sambuc 
1984684ddb6SLionel Sambuc void
__throw_system_error(int ev,const char * what_arg)1994684ddb6SLionel Sambuc __throw_system_error(int ev, const char* what_arg)
2004684ddb6SLionel Sambuc {
2014684ddb6SLionel Sambuc #ifndef _LIBCPP_NO_EXCEPTIONS
2024684ddb6SLionel Sambuc     throw system_error(error_code(ev, system_category()), what_arg);
2034684ddb6SLionel Sambuc #else
2044684ddb6SLionel Sambuc     (void)ev;
2054684ddb6SLionel Sambuc     (void)what_arg;
2064684ddb6SLionel Sambuc #endif
2074684ddb6SLionel Sambuc }
2084684ddb6SLionel Sambuc 
2094684ddb6SLionel Sambuc _LIBCPP_END_NAMESPACE_STD
210