在if else打印块中避免代码重复

时间:2016-02-27 07:35:30

标签: c++ macros duplicates

我讨厌这里的代码重复。如何使它更简洁?重复文本的另一个宏是一个好主意吗? 我有一种奇怪的感觉,我错过了一些明显的东西。

      #define CHECK(para_1, para_2, para_3, epic_fail) { \
      if (para_2 != para_3) { \
        size_t __bytes; \
        if(#para_1 == "special") { \
          __bytes = snprintf( \
            NULL, 0, \
            #para_1 \
              " this_thing '%s'(%p) doesn't go well with special this_thing '%s'(%p)", \
            para_2, para_2, para_3, para_3); \
        } else { \
          __bytes = snprintf( \
            NULL, 0, \
            #para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
            para_2, para_2, para_3, para_3); \
        } \
        char * message = \
          reinterpret_cast<char *>(rmw_allocate(__bytes + 1)); \
        if(#para_1 == "special") { \
          snprintf( \
            message, __bytes + 1, \
            #para_1 \
              " this_thing '%s'(%p) doesn't go well with special this_thing '%s'(%p)", \
            para_2, para_2, para_3, para_3); \
        } else{ \
          snprintf( \
            message, __bytes + 1, \
            #para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
            para_2, para_2, para_3, para_3); \
        } \
        SET_THE_ERROR(message); \
        free_the_mem(message); \
        epic_fail; \
      } \
    }

3 个答案:

答案 0 :(得分:1)

在宏中使用宏:

#define CHECK_PRINT(n) snprintf( \
    message, __bytes + n, \
    #para_1 " this_thing '%s'(%p) doesn't go well with unique_goldfish this_thing '%s'(%p)", \
    para_2, para_2, para_3, para_3); 

#define CHECK(para_1, para_2, para_3, epic_fail) { \
    if (para_2 != para_3) { \
        size_t __bytes; \
        if(#para_1 == "special") { \
          __bytes = CHECK_PRINT(n); \
        } else { \
          __bytes = CHECK_PRINT(n); \
        } \
       ...

答案 1 :(得分:0)

您可以添加一个功能来检查para条件。 希望这能解释你的问题

答案 2 :(得分:0)

如果您正在进行重大重构,可能需要考虑以下内容:

static
void check_impl( char const* p1, char const* p2, char const* p3)
{
    char const* special_or_unique = (strcmp(p1,"special") == 0) ? "special" : "unique_goldfish";
    char const* fmt = "%s this_thing '%s'(%p) doesn't go well with %s this_thing '%s'(%p)";
    char* message = NULL;

    size_t bytes = snprintf(NULL, 0, fmt,
                            p1, 
                            p2, (void*) p2, 
                            special_or_unique, 
                            p3, (void*) p3);

    message = (char*) malloc(bytes+1);
    if (message) {
        snprintf(message, bytes+ 1, fmt,
                    p1, 
                    p2, (void*) p2, 
                    special_or_unique, 
                    p3, (void*) p3);

        SET_THE_ERROR(message);
        free(message);
    }
    else {
        // some error handler?
    }
}



#define CHECK(para_1, para_2, para_3, epic_fail) \
    do { \
        /*should this really be: if (strcmp((para_2), (para_3)) != 0) { */ \
        if ((para_2) != (para_3)) {  \
            check_impl( #para_1, (para_2), (para_3)); \
            epic_fail;  \
        }   \
    } while (0)
相关问题