2017-05-08 131 views
4

我正在編寫一個庫,並希望在遠程系統返回錯誤時返回錯誤代碼。問題在於這些是由字符串標識的,例如「0A01」,並且還包含一條消息,而錯誤代碼需要一個整數作爲值。使用具有非整數值的std :: error_code

實現錯誤代碼的最佳方式是什麼?std::error_code提供的所有功能都使用字符串作爲值?如何將外部錯誤字符串添加到std::error_codestd::error_category

+1

異常? --- – Quentin

+0

我想使用error_codes。適用於應用程序的流程更好。 – ruipacheco

+0

如果這些字符串代碼的數量是固定的,那麼您可以將它們映射爲整數值(特別是因爲它們看起來像十六進制值)並且無論如何都使用'std :: error_code'。否則,編寫一個包含兩個字符串字段的類應該不會很困難。 – freakish

回答

3

正如在評論中提到,你必須知道的錯誤代碼,這可以從遠程服務器接收。 ,其從遠程服務器接收的std::string包含2份如你所說,

的問題是,這些是由字符串識別,例如,「0A01」,並且還包含一個消息和錯誤代碼要求的整數作爲價值。

由於您沒有共享的錯誤消息的格式,我不添加代碼吐涎它,分裂您的字符串分爲兩個部分,

  1. 錯誤代碼
  2. 錯誤消息

現在,您可以通過使用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指出

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."); 
+0

這假定動態消息是一個全局值。我如何將它傳遞給std :: error_category對象? – ruipacheco

+1

@ruipacheco:您可以將消息存儲在'OurCustomErrCategory'的成員中,並添加方法供您存儲它們,只要您知道它們到達網絡時即可。這假定從錯誤代碼到消息的映射是恆定的,即如果兩個錯誤代碼相同,消息將是相同的。 – PaulR

+0

@ruipacheco代碼中是否有任何問題,我該如何協助您? –

0

不要繼承error_code。您需要編寫自己的錯誤類別,它將整數映射到您的特定錯誤。這是一步一步的說明如何做到這一點:

http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-4.html

+0

但我沒有整數。我有兩個字符串,最終用戶會一直檢查代碼。 – ruipacheco

+0

您仍然可以將傳入的錯誤代碼字符串映射爲錯誤整數:'std :: error_code ec = make_error_code(errc_from_str(「0A01」));' – erenon

+0

找不到errc_from_str()。 – ruipacheco

相關問題