2012-04-16 25 views
14

मैं मानक सी ++ में अपने समकक्षों के साथ जितना संभव हो सके बाहरी पुस्तकालयों (जैसे बूस्ट) को प्रतिस्थापित करना चाहता हूं, यदि वे मौजूद हैं और निर्भरता को कम करने के लिए यह संभव है, इसलिए मुझे आश्चर्य है कि boost::system::error_code से std::error_code को परिवर्तित करने का एक सुरक्षित तरीका मौजूद है। छद्म कोड उदाहरण:क्या कोई बूस्ट :: सिस्टम :: error_code को std: error_code में परिवर्तित करना संभव है?

void func(const std::error_code & err) 
{ 
    if(err) { 
     //error 
    } else { 
     //success 
    } 
} 

boost::system::error_code boost_err = foo(); //foo() returns a boost::system::error_code 
std::error_code std_err = magic_code_here; //convert boost_err to std::error_code here 
func(std_err); 

सबसे महत्वपूर्ण यह नहीं बिल्कुल एक ही त्रुटि, बस इतनी के रूप में संभव के करीब है और पिछले पर है अगर गलती से हुआ है या नहीं। क्या कोई स्मार्ट समाधान है?

अग्रिम धन्यवाद!

+0

क्या आप दोनों एक साथ उपयोग करना चाहते हैं? यदि नहीं, तो इंटरफेस इतने समान नहीं हैं कि एक साधारण "खोज/प्रतिस्थापन" यह करेगा? – ereOn

+1

यह संभव नहीं है। दोनों std :: error_code और boost :: system :: error_code का उपयोग किया जाता है, लेकिन मैंने उपयोगकर्ता के लिए boost :: system :: error_code को दूर करने के लिए प्रबंधन किया है, इसलिए यह कभी "इसे देखता नहीं" है, इसलिए भविष्य में जब अंतिम निर्भरता इसे हटा देता है तो मैं कर सकता हूँ – Fredrik

+0

मुझे आपको एपीआई के बारे में पर्याप्त जानकारी नहीं है जो आपको magic_code देने के लिए है, लेकिन मैं कह सकता हूं कि आगे बढ़ने का सबसे अच्छा तरीका 'tyifef boost :: system :: के साथ संयोजन में' #ifdef USE_BOOST' का उपयोग करना होगा। error_code ErrorCodeType; 'और' ty elseef std :: error_code errorCodeType के साथ '# else';'। फिर आप अपने कोड बेस में प्रगतिशील परिवर्तन कर सकते हैं ताकि दोनों एक ही इंटरफ़ेस कॉल का उपयोग करके समर्थित हों, और फिर जब यह सभी 'USE_BOOST' के साथ काम नहीं कर रहा है, तो आप स्विच को स्थायी बना सकते हैं। अन्यथा आप एक साइड स्ट्रीम पर काम करना बंद कर देंगे जो अंततः भुला दिया जाएगा। – Dennis

उत्तर

8

चूंकि सी ++ - 11 (std :: errc), boost/system/error_code.hpp उसी त्रुटि कोड को std::errc पर नक्शा करता है, जिसे सिस्टम हेडर system_error में परिभाषित किया गया है।

आप दोनों enums की तुलना कर सकते हैं और वे कार्यात्मक रूप से समकक्ष होना चाहिए क्योंकि वे दोनों POSIX मानक पर आधारित प्रतीत होते हैं। एक कास्ट की आवश्यकता हो सकती है।

उदाहरण के लिए,

namespace posix_error 
    { 
     enum posix_errno 
     { 
     success = 0, 
     address_family_not_supported = EAFNOSUPPORT, 
     address_in_use = EADDRINUSE, 
     address_not_available = EADDRNOTAVAIL, 
     already_connected = EISCONN, 
     argument_list_too_long = E2BIG, 
     argument_out_of_domain = EDOM, 
     bad_address = EFAULT, 
     bad_file_descriptor = EBADF, 
     bad_message = EBADMSG, 
     .... 
     } 
    } 

और std::errc

address_family_not_supported error condition corresponding to POSIX code EAFNOSUPPORT 

address_in_use error condition corresponding to POSIX code EADDRINUSE 

address_not_available error condition corresponding to POSIX code EADDRNOTAVAIL 

already_connected error condition corresponding to POSIX code EISCONN 

argument_list_too_long error condition corresponding to POSIX code E2BIG 

argument_out_of_domain error condition corresponding to POSIX code EDOM 

bad_address error condition corresponding to POSIX code EFAULT 
+3

धन्यवाद, मुझे निम्न कोड का उपयोग करके काम करने के लिए मिला: std :: make_error_code (static_cast (err.value())) - गलती :: इंस्टॉलेशन का एक उदाहरण/संदर्भ है ::एरर कोड। – Fredrik

7

मैं इस में ठीक उसी सवाल था के बाद से मैं std::error_code उपयोग करना चाहता था, लेकिन यह भी है कि उपयोग करने वाले अन्य बढ़ावा पुस्तकालयों उपयोग कर रहा था boost::system::error_code (जैसे को बढ़ावा देने ASIO) । स्वीकार्य उत्तर std::generic_category() द्वारा प्रबंधित त्रुटि कोड के लिए काम करता है, क्योंकि वे बूस्ट के जेनेरिक त्रुटि कोड से एक साधारण कलाकार हैं, लेकिन यह सामान्य मामले के लिए काम नहीं करता है जहां आप कस्टम त्रुटि श्रेणियों को भी संभालना चाहते हैं।

इसलिए मैंने सामान्य उद्देश्य boost::system::error_code -to- std::error_code कनवर्टर के रूप में निम्न कोड बनाया है। यह प्रत्येक boost::system::error_category के लिए गतिशील रूप से std::error_category शिम बनाकर काम करता है, अंतर्निहित बूस्ट त्रुटि श्रेणी में कॉल अग्रेषित करता है। चूंकि त्रुटि श्रेणियों को सिंगलेट्स (या कम से कम सिंगलटन-जैसे इस मामले में) होने की आवश्यकता है, इसलिए मुझे उम्मीद नहीं है कि वहां मेमोरी विस्फोट होगा।

मैं boost::system::generic_category() ऑब्जेक्ट को std::generic_category() का उपयोग करने के लिए भी परिवर्तित करता हूं क्योंकि उन्हें वही व्यवहार करना चाहिए। मैं system_category() के लिए ऐसा करना चाहता था, हालांकि वीसी ++ 10 पर परीक्षण में यह गलत संदेशों को मुद्रित करता था (मुझे लगता है कि आपको FormatMessage से जो मिलता है उसे प्रिंट करना चाहिए, लेकिन ऐसा लगता है कि strerror का उपयोग करना है, बूस्ट FormatMessage का उपयोग अपेक्षित है) ।

इसका उपयोग करने के लिए बस नीचे परिभाषित BoostToErrorCode() पर कॉल करें।

बस एक चेतावनी, मैंने अभी इसे आज लिखा है, इसलिए इसमें केवल मूल परीक्षण था। आप इसे किसी भी तरह से इस्तेमाल कर सकते हैं, लेकिन आप अपने जोखिम पर ऐसा करते हैं।

//================================================================================================== 
// These classes implement a shim for converting a boost::system::error_code to a std::error_code. 
// Unfortunately this isn't straightforward since it the error_code classes use a number of 
// incompatible singletons. 
// 
// To accomplish this we dynamically create a shim for every boost error category that passes 
// the std::error_category calls on to the appropriate boost::system::error_category calls. 
//================================================================================================== 
#include <boost/system/error_code.hpp> 
#include <boost/thread/mutex.hpp> 
#include <boost/thread/once.hpp> 
#include <boost/thread/locks.hpp> 

#include <system_error> 
namespace 
{ 
    // This class passes the std::error_category functions through to the 
    // boost::system::error_category object. 
    class BoostErrorCategoryShim : public std::error_category 
    { 
    public: 
     BoostErrorCategoryShim(const boost::system::error_category& in_boostErrorCategory) 
      :m_boostErrorCategory(in_boostErrorCategory), m_name(std::string("boost.") + in_boostErrorCategory.name()) {} 

     virtual const char *name() const; 
     virtual std::string message(value_type in_errorValue) const; 
     virtual std::error_condition default_error_condition(value_type in_errorValue) const; 

    private: 
     // The target boost error category. 
     const boost::system::error_category& m_boostErrorCategory; 

     // The modified name of the error category. 
     const std::string m_name; 
    }; 

    // A converter class that maintains a mapping between a boost::system::error_category and a 
    // std::error_category. 
    class BoostErrorCodeConverter 
    { 
    public: 
     const std::error_category& GetErrorCategory(const boost::system::error_category& in_boostErrorCategory) 
     { 
      boost::lock_guard<boost::mutex> lock(m_mutex); 

      // Check if we already have an entry for this error category, if so we return it directly. 
      ConversionMapType::iterator stdErrorCategoryIt = m_conversionMap.find(&in_boostErrorCategory); 
      if(stdErrorCategoryIt != m_conversionMap.end()) 
       return *stdErrorCategoryIt->second; 

      // We don't have an entry for this error category, create one and add it to the map.     
      const std::pair<ConversionMapType::iterator, bool> insertResult = m_conversionMap.insert(
       ConversionMapType::value_type(
        &in_boostErrorCategory, 
        std::unique_ptr<const BoostErrorCategoryShim>(new BoostErrorCategoryShim(in_boostErrorCategory)))); 

      // Return the newly created category. 
      return *insertResult.first->second; 
     } 

    private: 
     // We keep a mapping of boost::system::error_category to our error category shims. The 
     // error categories are implemented as singletons so there should be relatively few of 
     // these. 
     typedef std::unordered_map<const boost::system::error_category*, std::unique_ptr<const BoostErrorCategoryShim>> ConversionMapType; 
     ConversionMapType m_conversionMap; 

     // This is accessed globally so we must manage access. 
     boost::mutex m_mutex; 
    }; 


    namespace Private 
    { 
     // The init flag. 
     boost::once_flag g_onceFlag = BOOST_ONCE_INIT; 

     // The pointer to the converter, set in CreateOnce. 
     BoostErrorCodeConverter* g_converter = nullptr; 

     // Create the log target manager. 
     void CreateBoostErrorCodeConverterOnce() 
     { 
      static BoostErrorCodeConverter converter; 
      g_converter = &converter; 
     } 
    } 

    // Get the log target manager. 
    BoostErrorCodeConverter& GetBoostErrorCodeConverter() 
    { 
     boost::call_once(Private::g_onceFlag, &Private::CreateBoostErrorCodeConverterOnce); 

     return *Private::g_converter; 
    } 

    const std::error_category& GetConvertedErrorCategory(const boost::system::error_category& in_errorCategory) 
    { 
     // If we're accessing boost::system::generic_category() or boost::system::system_category() 
     // then just convert to the std::error_code versions. 
     if(in_errorCategory == boost::system::generic_category()) 
      return std::generic_category(); 

     // I thought this should work, but at least in VC++10 std::error_category interprets the 
     // errors as generic instead of system errors. This means an error returned by 
     // GetLastError() like 5 (access denied) gets interpreted incorrectly as IO error. 
     //if(in_errorCategory == boost::system::system_category()) 
     // return std::system_category(); 

     // The error_category was not one of the standard boost error categories, use a converter. 
     return GetBoostErrorCodeConverter().GetErrorCategory(in_errorCategory); 
    } 


    // BoostErrorCategoryShim implementation. 
    const char* BoostErrorCategoryShim::name() const 
    { 
     return m_name.c_str(); 
    } 

    std::string BoostErrorCategoryShim::message(value_type in_errorValue) const 
    { 
     return m_boostErrorCategory.message(in_errorValue); 
    } 

    std::error_condition BoostErrorCategoryShim::default_error_condition(value_type in_errorValue) const 
    { 
     const boost::system::error_condition boostErrorCondition = m_boostErrorCategory.default_error_condition(in_errorValue); 

     // We have to convert the error category here since it may not have the same category as 
     // in_errorValue. 
     return std::error_condition(boostErrorCondition.value(), GetConvertedErrorCategory(boostErrorCondition.category())); 
    } 
} 

std::error_code BoostToErrorCode(boost::system::error_code in_errorCode) 
{ 
    return std::error_code(in_errorCode.value(), GetConvertedErrorCategory(in_errorCode.category())); 
} 
+0

क्या आप अभी भी इस कोड का उपयोग करते हैं? क्या यह संभवतः बढ़ावा देने में योगदान देने के लिए पर्याप्त उपयोगी है? – sehe

+0

@sehe AFAIK यह अभी भी उपयोग में है। मैं इसे बढ़ावा देने के लिए एक उपयोगी जोड़ देख सकता हूं, अवधारणात्मक रूप से, त्रुटि कोड के बूस्ट और std संस्करण एक ही चीज करते हैं और टाइप सिस्टम के कारण केवल असंगत हैं। उस स्थिति में हालांकि यह शायद बूस्ट त्रुटि श्रेणी कक्षाओं में सीधे लागू किया गया है। इससे म्यूटेक्स और मानचित्र की आवश्यकता को हटा दिया जाएगा और प्रति श्रेणी कुछ और बाइट्स की कीमत पर रूपांतरण अस्वीकार कर दिया जाएगा। या शायद यह सीधे std से प्राप्त हो सकता है क्योंकि आप std-> boost से जाने में भी सक्षम होना चाहते हैं? – Screndib