std::error_category
std::error_category是错误类别的基类,实现了并且提供了如下4个常见错误类别,每个特定类别类定义 error_code - error_condition 映射,并保有所有 error_condition 的解释字符串。错误类别类的对象被当做单例,按引用传递
std::generic_category(通用错误类别,它用于鉴别对应 POSIX errno 码的错误条件)
std::system_category(操作系统错误类别,用于操作系统提供的错误码,linux和generic_category基本无差异)
iostream_category(iostream 错误类别,用于鉴别提供于 std::ios_base::failure 类型异常的 error_code)
future_category(future 错误类别,用于鉴别 std::future_error 类型异常中提供的 error_code)
stl提供了如下图所示
std::error_category提供的接口
class error_category
{
public:
error_category();
virtual ~error_category() noexcept;
/**
返回指向指定错误类别名称的空终止字节字符串的指针
*/
virtual const char *name() const noexcept = 0;
/**
返回错误描述
*/
virtual string message(int _Errval) const = 0;
/**
映射 error_code 到 error_condition
等价于 std::error_condition(code, *this)
*/
virtual error_condition default_error_condition(int _Errval) const noexcept;
/**
检查对于 *this 所表示的 error_category , error_code 是否等价于 error_condition
等价于 default_error_condition(code) == condition
*/
virtual bool equivalent(int _Errval, const error_condition& _Cond) const noexcept;
/**
等价于 *this == code.category() && code.value() == condition
*/
virtual bool equivalent(const error_code& _Code, int _Errval) const noexcept;
/**
比较两个error_category
*/
bool operator==(const error_category& _Right) const noexcept;
bool operator!=(const error_category& _Right) const noexcept;
bool operator<(const error_category& _Right) const noexcept;
error_category(const error_category&) = delete;
error_category& operator=(const error_category&) = delete;
};
示例
#include <iostream> // iostream_category<ios>
#include <future> //future_category
#include <system_error> //system_category,generic_category
using namespace std;
class _database_category : public std::error_category
{
public:
virtual const char *name() const noexcept
{
return ("database");
}
virtual string message(int _Errval) const
{
if (*(database_errc + _Errval))
{
return std::string(database_errc[_Errval]);
}
return "database-unkown error";
}
private:
static const char* database_errc[];
};
_database_category& database_category()
{
//return (_Immortalize<_database_category>()); // 库提供的单例
static _database_category dc;//直接使用局部静态变量
return dc;
}
const char* _database_category::database_errc[] =
{
"connect db fail",//在实际中为了兼容error_code一般设置0的错误码是无错的情况
"exesql error",
NULL
};
int main()
{
cout << "cate-name-->" << std::system_category().name() << endl // windows可以使用 errlook.exe 1校验
<< "error 1-->" << std::system_category().message(1).c_str() << endl;
cout << "cate-name-->" << std::generic_category().name() << endl
<< "error 1-->" << std::generic_category().message(1).c_str() << endl;
cout << "cate-name-->" << std::iostream_category().name() << endl
<< "error 1-->" << std::iostream_category().message(1).c_str() << endl;
cout << "cate-name-->" << std::future_category().name() << endl
<< "error 1-->" << std::future_category().message(1).c_str() << endl;
cout << "cate-name-->" << database_category().name() << endl
<< "error 1-->" << database_category().message(1).c_str() << endl;
cout << "---------------" << endl;
std::error_condition ed = database_category().default_error_condition(0);
cout << ed.message().c_str() << endl;
cout << ed.value() << endl;
cout <<ed.category().name()<<endl;
return 0;
}
示例结果
cate-name-->system
error 1-->函数不正确。
cate-name-->generic
error 1-->operation not permitted
cate-name-->iostream
error 1-->iostream stream error
cate-name-->future
error 1-->broken promise
cate-name-->database
error 1-->exesql error
---------------
connect db fail
0
database