C ++错误代码样板的模板与宏 [英] Templates Vs Macros for C++ Error Code Boilerplate

查看:48
本文介绍了C ++错误代码样板的模板与宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR-可以使用模板来实现样板生成宏吗?

TL;DR - can a boilerplate generation macro be implemented using templates?

我有一些使用C ++ std :: error_code和error_category类的代码.我发现,一旦错误代码的数量开始增加,为每个错误代码编写的样板文件的数量也会迅速增加.

I have some code which makes use of the C++ std::error_code and error_category classes. I've found that once the number of error codes started to grow the amount of boilerplate being written for each was also growing very quickly.

要解决此问题,我编写了一些宏,这些宏应根据我们实际关心的事情(包括枚举代码和附加到它们的消息)进行静态检查并生成大部分样板.这些宏将枚举和消息作为const std :: map接收.

To work around this I've written some macros which should be statically checking and generating most of the boilerplate from the things we actually care about - the enum codes and the messages attached to them. These macros take in the enum and the messages as a const std::map.

我的问题-可以用某种模板代替这种样板生成吗?现在,如果这失败了,对其他人的goto建议就是检查static_asserts",这使它有点像PITA.

My question - can this sort of boilerplate generation be replaced with a template of some sort? Right now if this fails the goto advice for others has been "check for static_asserts" which make it a bit of a PITA to use.

如果不能简单地将其替换为模板,是否可以添加代码以改善失败的编译的输出,从而使这些代码使用起来更省心?现在,失败的编译会同时输出静态断言和许多其他不需要的输出.

If it can't be simply replaced with templates, can code be added to improve the output of a failed compile so these are less painful to use? Right now a failed compile outputs both the static asserts and lot of other unneeded output.

我在下面添加了一些代码来演示宏-我删除了所有与命名空间相关的代码,因此这可能有点不正确,但应该可以很好地说明目标.

I've included some code below which demonstrates the macros - I've removed all of our namespace-ing related code so it may be a tad incorrect but should demonstrate the goal well enough.

//C++14 definition - we are using C++11
template< bool B, class T = void >
using enable_if_t = typename std::enable_if<B,T>::type;

//Generic template test for any other type
template <typename T, typename = void>
struct is_std_map : std::false_type {};

//Specialised test for a std::map type
template <typename T>
struct is_std_map<T, enable_if_t<
                    std::is_same<typename T::value_type,
                                std::pair<const typename T::key_type,
                                          typename T::mapped_type>
                    >::value>
> : std::true_type {};

#define MAKE_ERROR_CODE_CATEGORY(EC, EC_MESSAGE_MAP)               \
/* Check that we have an enum type as the first arg, and a const std::map arg for the second */                 \
static_assert(std::is_enum<EC>::value, "!");                                             \
static_assert(std::is_const<decltype(EC_MESSAGE_MAP)>::value, "!" );                \
static_assert(is_std_map<decltype(EC_MESSAGE_MAP)>::value, "!");        \
/* Validate that the non-const types for our EC and our EC_MESSAGE_MAP are as expected*/                        \
static_assert(std::is_same<                                                                                     \
                    std::remove_const<EC>::type,                                                                \
                    std::remove_const<decltype(EC_MESSAGE_MAP)::key_type                                        \
                >::type>::value,                                                                                \
              "!");                          \
static_assert(std::is_same<                                                                                     \
                    std::remove_const<std::string>::type,                                                       \
                    std::remove_const<decltype(EC_MESSAGE_MAP)::mapped_type                                     \
                >::type>::value,                                                                                \
              "!");  \
/*Generates a standardised category for the provided EC */                                                      \
struct EC## _category : std::error_category                                                                \
{                                                                                                               \
    const char* name() const noexcept override                                                                  \
    {                                                                                                           \
      return  #EC ;                                                                                             \
    }                                                                                                           \
    std::string message(int c) const override                                                                   \
    {                                                                                                           \
        EC code = static_cast<EC>(c);                                                                           \
        auto itr = EC_MESSAGE_MAP.find(code);                                                                   \
        if (itr != EC_MESSAGE_MAP.end())                                                                        \
        {                                                                                                       \
            return itr->second;                                                                                 \
        }                                                                                                       \
        else                                                                                                    \
        {                                                                                                       \
            return "(unrecognized error)";                                                                      \
        }                                                                                                       \
    }                                                                                                           \                                                                                                    \
};
namespace std                                                                                                   \
{                                                                                                               \
    template <>                                                                                                 \
        struct is_error_code_enum< EC > : true_type {};                                   \
}                                                                                                               \
/* Declare a global function returning a static instance of the custom category */                              \
const EC## _category& EC## _category_generator()                     \
{                                                                                                               \
    static EC## _category c;                                                              \
    return c;                                                                                                   \
}                                                                                                               \
/* Adds the standard error code construction call */                                                            \
inline std::error_code make_error_code(EC e)                                              \
{                                                                                                               \
    return {static_cast<int>(e), EC## _category_generator()};                                                   \
}                                 

推荐答案

需要使用您的宏来对枚举进行字符串化,并帮助避免标准中专门化的样板,但是您可以提取一些代码来创建模板:

Your macro is needed to stringify the enum, and help to avoid boilerplate for specialization in std, but you can extract some code to create templates:

// Traits to retrieve name and mapping from enum.
template <typename E>
struct enum_traits
{
    static_assert(std::is_enum<E>::value, "!");

    static const char* const name;
    static const std::map<E, std::string> mapping;
};

template <typename E>
struct ECategory_impl : std::error_category
{
    static_assert(std::is_enum<E>::value, "!");

    const char* name() const noexcept override
    {
      return enum_traits<E>::name;
    }

    std::string message(int c) const override
    {
        const auto& Map = enum_traits<E>::mapping;
        E code = static_cast<E>(c);
        auto itr = Map.find(code);
        if (itr != Map.end())
        {
            return itr->second;
        }
        else
        {
            return "(unrecognized error)";
        }
    }
};

template <typename E>
std::error_code make_error_code(E e)
{
    static_assert(std::is_enum<E>::value, "!");
    static const ECategory_impl<E> categ{};
    return {static_cast<int>(e), categ};
}

然后是MACRO(如果您认为现在要重复的内容足够低,则可以省略):

and then the MACRO (Which might be omitted if you consider that now stuff to repeat is low enough):

#define MAKE_ERROR_CODE_CATEGORY(E)                      \
/* Stringification for the name*/                        \
template <> const char* const enum_traits<E>::name = #E; \
/* Specialization in std */                              \
namespace std                                            \
{                                                        \
    template <>                                          \
    struct is_error_code_enum<E> : true_type {};         \
}                                                        \
/* Alias for custom naming */                            \
using E##_category = ECategory_impl<E>;
//                                    ^
// You might remove that final ';' for a usage `MAKE_ERROR_CODE_CATEGORY(E);`
// instead of current `MAKE_ERROR_CODE_CATEGORY(E)`

用法类似于:

enum class E {A, B};
template <>
const std::map<E, std::string> enum_traits<E>::mapping{
    {E::A, "A"},
    {E::B, "E"}
};

MAKE_ERROR_CODE_CATEGORY(E)

演示

这篇关于C ++错误代码样板的模板与宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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