我正在編寫一個庫,並希望在遠程系統返回錯誤時返回錯誤代碼。問題在於這些是由字符串標識的,例如「0A01」,並且還包含一條消息,而錯誤代碼需要一個整數作爲值。使用具有非整數值的std :: error_code
實現錯誤代碼的最佳方式是什麼?std::error_code
提供的所有功能都使用字符串作爲值?如何將外部錯誤字符串添加到std::error_code
或std::error_category
?
我正在編寫一個庫,並希望在遠程系統返回錯誤時返回錯誤代碼。問題在於這些是由字符串標識的,例如「0A01」,並且還包含一條消息,而錯誤代碼需要一個整數作爲值。使用具有非整數值的std :: error_code
實現錯誤代碼的最佳方式是什麼?std::error_code
提供的所有功能都使用字符串作爲值?如何將外部錯誤字符串添加到std::error_code
或std::error_category
?
正如在評論中提到,你必須知道的錯誤代碼,這可以從遠程服務器接收。 ,其從遠程服務器接收的std::string
包含2份如你所說,
的問題是,這些是由字符串識別,例如,「0A01」,並且還包含一個消息和錯誤代碼要求的整數作爲價值。
由於您沒有共享的錯誤消息的格式,我不添加代碼吐涎它,分裂您的字符串分爲兩個部分,
現在,您可以通過使用std::stoi(error_code)
std::string
類型的錯誤代碼轉換爲int
,因此,可以說
int error_code_int = std::stoi(string_to_hexadecimal(error_code));
而對於std::error_category
其作爲基類爲我們的自定義錯誤消息,爲此,
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()
從this post 。
我希望現在你可以根據你的需要修改上面的代碼。
編輯1:
正如你說:
這是假設的動態消息是一個全球性的價值。我如何將它 傳遞給
std::error_category
對象?
你可以看到,無論std::error_code::assign
和構造std::error_code::error_code
針對錯誤代碼編號和error_category
服用int
參數。所以很明顯,std::error_code
不能接受動態消息。
但是等等,我說std::error_code
在構造函數中是以error_category
作爲參數,那麼有什麼辦法,我們可以在那裏指定動態消息嗎?
std::error_category
用作用於特定的錯誤 類別類型的基類。
因此,這意味着該struct
我們從std::error_category
衍生於下一行
struct OurCustomErrCategory : std::error_category
可以有一個數據成員,我們可以通過成員函數分配給它,所以我們struct
會變成那樣,
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;
};
和任何你想要的,你可以這樣分配給它,
const OurCustomErrCategory ourCustomErrCategoryObject("This is the message which received from remote server.");
這假定動態消息是一個全局值。我如何將它傳遞給std :: error_category對象? – ruipacheco
@ruipacheco:您可以將消息存儲在'OurCustomErrCategory'的成員中,並添加方法供您存儲它們,只要您知道它們到達網絡時即可。這假定從錯誤代碼到消息的映射是恆定的,即如果兩個錯誤代碼相同,消息將是相同的。 – PaulR
@ruipacheco代碼中是否有任何問題,我該如何協助您? –
不要繼承error_code。您需要編寫自己的錯誤類別,它將整數映射到您的特定錯誤。這是一步一步的說明如何做到這一點:
http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-4.html
但我沒有整數。我有兩個字符串,最終用戶會一直檢查代碼。 – ruipacheco
您仍然可以將傳入的錯誤代碼字符串映射爲錯誤整數:'std :: error_code ec = make_error_code(errc_from_str(「0A01」));' – erenon
找不到errc_from_str()。 – ruipacheco
異常? --- – Quentin
我想使用error_codes。適用於應用程序的流程更好。 – ruipacheco
如果這些字符串代碼的數量是固定的,那麼您可以將它們映射爲整數值(特別是因爲它們看起來像十六進制值)並且無論如何都使用'std :: error_code'。否則,編寫一個包含兩個字符串字段的類應該不會很困難。 – freakish