22

我想尽可能用标准 C++ 中的等价物替换外部库(如 boost),如果它们存在并且可能的话,以最小化依赖关系,因此我想知道是否存在一种安全的方法来转换boost::system::error_codestd::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);

最重要的不是完全相同的错误,只是尽可能接近,最后是否是错误。有什么聪明的解决方案吗?

提前致谢!

4

3 回答 3

12

我有这个完全相同的问题,因为我想使用std::error_code但也在使用其他使用的 boost 库boost::system::error_code(例如 boost ASIO)。接受的答案适用于由 处理的错误代码std::generic_category(),因为它们是 boost 的通用错误代码的简单转换,但它不适用于您想要处理自定义错误类别的一般情况。

因此,我创建了以下代码作为通用boost::system::error_code转换std::error_code器。它通过std::error_category为每个 动态创建一个 shim 来工作boost::system::error_category,将调用转发到底层的 Boost 错误类别。由于错误类别需要是单例的(或至少像本例中那样的单例),我不认为会有太多的内存爆炸。

我也只是将boost::system::generic_category()对象转换为使用std::generic_category(),因为它们的行为应该相同。我本来想为 做同样的事情system_category(),但是在 VC++10 上进行测试时,它打印出了错误的消息(我认为它应该打印出你从中得到的信息FormatMessage,但它似乎使用strerror,BoostFormatMessage按预期使用)。

要使用它,只需调用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()) );
}
于 2012-05-12T00:08:29.383 回答
9

由于 C++-11 (std::errc),boost/system/error_code.hpp将相同的错误代码映射到std::errc,这是在系统头文件中定义的system_error

您可以比较这两个枚举,它们在功能上应该是等效的,因为它们似乎都基于 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 
于 2012-04-16T16:20:21.663 回答
2

我将上述解决方案改编为更短的解决方案。使用 thread_local std::map 将类别名称映射到其实例,因此不需要锁定。

限制是您不能在线程之间传递错误代码,因为类别指针会有所不同。(如果您不想使用 thread_local 存储,将其转换为锁定函数很简单)

我也喂它更紧凑。

#include <iostream>
#include <map>
#include <boost/system/system_error.hpp>

namespace std
{

error_code make_error_code(boost::system::error_code error)
{
    struct CategoryAdapter : public error_category
    {
        CategoryAdapter(const boost::system::error_category& category)
            : m_category(category)
        {
        }

        const char* name() const noexcept
        {
            return m_category.name();
        }

        std::string message(int ev) const
        {
            return m_category.message(ev);
        }

    private:
        const boost::system::error_category& m_category;
    };

    static thread_local map<std::string, CategoryAdapter> nameToCategory;
    auto result = nameToCategory.emplace(error.category().name(), error.category());
    auto& category = result.first->second;
    return error_code(error.value(), category);
}

};

int main() {
    auto a = boost::system::errc::make_error_code(boost::system::errc::address_family_not_supported);
    auto b = std::make_error_code(a);
    std::cout << b.message() << std::endl;
}
于 2018-08-29T11:12:38.927 回答