使用具有非整数值的std :: error_code [英] Using std::error_code with non-integer values

查看:136
本文介绍了使用具有非整数值的std :: error_code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个库,想在远程系统返回错误时返回错误代码。问题是这些字符串由字符串标识,例如 0A01,并且还包含一条消息,并且错误代码需要整数作为值。

I'm writing a library and want to return error codes whenever an error is returned by a remote system. The problem is that these are identified by strings, eg, "0A01" and also contain a message, and error code requires an integer as value.

什么是最好的方法实现错误代码,使用 std :: error_code 提供的所有功能,但使用字符串作为值?如何在 std :: error_code std :: error_category 中添加外部错误字符串?

What is the best way to implement an error code, with all the functionality that std::error_code provides but that uses strings as the value? How do I add an external error string to the std::error_code or std::error_category?

推荐答案

如注释中所述,您必须知道错误代码,该错误代码可以从远程服务器接收。
从远程服务器收到的 std :: string 包含您所说的两部分,

As mentioned in the comments, you must know the error codes, which could be received from the remote server. The std::string which you receive from a remote server contains 2 parts as you said,


问题在于它们由字符串标识,例如 0A01,并且还包含一条消息,并且错误代码要求整数作为值。

The problem is that these are identified by strings, eg, "0A01" and also contain a message, and error code requires an integer as value.

由于您尚未共享错误消息的格式,因此我不会添加用于吐出错误代码的代码,而是将您的字符串分成两部分,

As you haven't shared the format of the error message, I am not adding the code for spiting it, split your string into 2 parts,


  1. 错误代码

  2. 错误消息

现在您可以使用 std将类型 std :: string 的错误代码转换为 int :: stoi(error_code),所以可以说

Now you can convert Error Code of type std::string to int by using std::stoi(error_code), So lets say

int error_code_int = std::stoi(string_to_hexadecimal(error_code));

对于 std :: error_category 作为我们自定义错误消息的基类,请执行此操作

And for std::error_category which serves as base class for our custom error messages, do this,

std::string message_received = "This is the message which received from remote server.";

struct OurCustomErrCategory : std::error_category
{
  const char* name() const noexcept override;
  std::string message(int ev) const override;
};

const char* OurCustomErrCategory::name() const noexcept
{
  return "Error Category Name";
}

std::string OurCustomErrCategory::message(int error_code_int) const
{
    switch (error_code_int)
    {
    case 1:
        return message_received;

    default:
        return "(unrecognized error)";
  }
}

const OurCustomErrCategory ourCustomErrCategoryObject;

std::error_code make_error_code(int e)
{
  return {e, ourCustomErrCategoryObject};
}

int main()
{
    int error_code_int = std::stoi(string_to_hexadecimal(error_code));  // error_code = 0A01
    ourCustomErrCategoryObject.message(error_code_int);
    std::error_code ec(error_code_int , ourCustomErrCategoryObject);
    assert(ec);

    std::cout << ec << std::endl;
    std::cout << ec.message() << std::endl;
}

上述示例的输出为

Error Category Name : 0A01
This is the message which received from remote server.

您可以使用函数 string_to_hexadecimal() 这篇文章

我希望现在您可以根据需要修改上面的代码。

编辑1:

正如您所说的那样:


这是假设动态消息是全球价值。如何将
传递给 std :: error_category 对象?

您可以看到 std :: error_code :: assign 和构造函数 std :: error_code :: error_code 正在使用参数 int 来获取错误代码编号,并使用 error_category 。因此很明显, std :: error_code 无法接收动态消息。

You can see that both std::error_code::assign and constructor std::error_code::error_code are taking parameters of int for error code number and error_category. So It is obvious that std::error_code can't take the dynamic message.

但是,我说 std :: error_code error_category 用作构造函数中的参数,所以有什么办法,我们可以分配动态邮件在那里?

But wait, I said std::error_code are taking error_category as an argument in constructor, so is there any way, we can assign the dynamic message there ?

std :: error_category 声明


std :: error_category 用作特定错误
类别类型的基类。

std::error_category serves as the base class for specific error category types.

因此,这意味着我们从 std :: error_category 结构 c>在以下行

So it means that the struct we derived from std::error_category at the following line

struct OurCustomErrCategory : std::error_category

可以有一个数据成员,我们可以通过成员函数进行分配,因此我们的 struct 会变成这样,

can have a data member and we can assign it via member function, so our struct will become like that,

struct OurCustomErrCategory : std::error_category
{
    std::string message_received;
    OurCustomErrCategory(std::string m) : message_received(m) {}

    const char* name() const noexcept override;
    std::string message(int ev) const override;
};

您可以在任何需要的地方分配它,

and you can assign it like that wherever you want,

const OurCustomErrCategory ourCustomErrCategoryObject("This is the message which received from remote server.");

这篇关于使用具有非整数值的std :: error_code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆