美文网首页
c/c++一些宏定义的使用小技巧

c/c++一些宏定义的使用小技巧

作者: 三分钟热度的陈恤猿 | 来源:发表于2018-12-26 16:49 被阅读0次

    引言

           在代码书写的过程中,为了对函数进行封装,一些简单的诸如“等于”,“打印log”的代码都会用宏定义来书写,在提高抽象层次的基础上,也简化了代码的书写。接下来是一些比较常用的宏的调用,记录下以供下次来用此方法来进行代码的优化。

    正文

           试验代码如下

    #define IE_ELEMENT_PRESENT(ie, element) (ie->tOptFlags.element##Present)
    #define IE_ELEMENT(ie, element) (ie->element)
    #define IE_ELEMENT_POINTER(ie, element) (&(ie->element))
    #define IE_UNION_T(ie) (ie->t)
    #define IE_UNION_ELEMENT(ie, element) (ie->u.element)
    #define IE_UNION_ELEMENT_POINTER(ie, element) (&(ie->u.element))
    
    #define DECLARE_SAVE_FUNC(ie_element, ie_element_type, ie_saved_type)\
        VOID Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            *ie_element##_saved = *ie_element;\
        }
    
    #define DECLARE_SAVE_FUNC_WITH_CHECK(ie_element, ie_max_value, ie_element_type, ie_saved_type)\
        VOID Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            CHECK_ARRAY_BOUND_RETURN(*ie_element, ie_max_value, #ie_element, Save_##ie_element);\
            *ie_element##_saved = *ie_element;\
        }
    
    #define DECLARE_SAVE_FUNC_ARRAY_INDEX_WITH_CHECK(ie_element, ie_array, ie_max_value, ie_element_type, ie_saved_type)\
        VOID Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            CHECK_ARRAY_BOUND_RETURN(*ie_element, ie_max_value, #ie_array, Save_##ie_element);\
            *ie_element##_saved = ie_array[*ie_element];\
        }
    
    #define DECLARE_SAVE_FUNC_WITH_CHECK_RETURN_FAILURE(ie_element, ie_max_value, ie_element_type, ie_saved_type)\
        WORD32 Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            CHECK_ARRAY_BOUND(*ie_element, ie_max_value, #ie_element, Save_##ie_element);\
            *ie_element##_saved = *ie_element;\
            return SUCCESS;\
        }
    
    #define DECLARE_SAVE_FUNC_ARRAY_INDEX_WITH_CHECK_RETURN_FAILURE(ie_element, ie_array, ie_max_value, ie_element_type, ie_saved_type)\
        WORD32 Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            CHECK_ARRAY_BOUND(*ie_element, ie_max_value, #ie_array, Save_##ie_element);\
            *ie_element##_saved = ie_array[*ie_element];\
            return SUCCESS;\
        }
    
    #define DECLARE_SAVE_FUNC_WITH_CHECK_RANGE_RETURN_FAILURE(ie_element, ie_max_value, ie_min_value, ie_element_type, ie_saved_type)\
        WORD32 Save_##ie_element(const ie_element_type * ie_element, ie_saved_type * ie_element##_saved)\
        {\
            CHECK_VALUE_OUT_RANGE_RETURN_FAILURE(*ie_element, ie_max_value, ie_min_value, Save_##ie_element);\
            *ie_element##_saved = *ie_element;\
            return SUCCESS;\
        }
    
    #define SAVE_IE_SIMPLE(ie, ie_element, ie_saved)\
        do{\
            if (IE_PRESENT == IE_ELEMENT_PRESENT(ie, ie_element))\
            {\
                Save_##ie_element(IE_ELEMENT_POINTER(ie, ie_element), ie_saved);\
            }\
        } while(0);
    
    #define SAVE_IE_DEFAULT(ie, ie_element, ie_saved, defaultVal)\
        do{\
            if (IE_PRESENT == IE_ELEMENT_PRESENT(ie, ie_element))\
            {\
                Save_##ie_element(IE_ELEMENT_POINTER(ie, ie_element), ie_saved);\
            }\
            else\
            {\
                *ie_saved = defaultVal;\
            }\
        } while(0);
    
    #define CHECK_SAVE_IE_RESULT(ie_element, save_func)\
        do{\
            if(SUCCESS != save_func)\
            {\
                UE_LOG_ERR(g_dwUlMacLogId, "%s:save %s failure!", #save_func, #ie_element);\
            }\
        }while(0);
    
    #define CHECK_FUNCTION_RESULT(func)\
        do{\
            if(SUCCESS != func)\
            {\
                UE_LOG_ERR(g_dwUlMacLogId, "%s: %s failure!", __FUNCTION__, #func);\
            }\
        }while(0);
    
    #define CHECK_FUNCTION_RESULT_RETURN_FAILURE(func)\
        do{\
            if(SUCCESS != func)\
            {\
                UE_LOG_ERR(g_dwUlMacLogId, "%s: %s failure!", __FUNCTION__, #func);\
                return FAILURE;\
            }\
        }while(0);
    
    #define CHECK_FUNCTION_RESULT_RETURN_FAILURE_STAT(func, statVar)\
        do{\
            if(SUCCESS != func)\
            {\
                (statVar)++;\
                UE_LOG_ERR(g_dwUlMacLogId, "%s: %s failure!", __FUNCTION__, #func);\
                return FAILURE;\
            }\
        }while(0);
    
    #define SAVE_IE(ie, ie_element, save_func, ...)\
        do{\
            if ( IE_PRESENT == IE_ELEMENT_PRESENT(ie, ie_element) )\
            {\
                save_func(IE_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__);\
            }\
        } while(0);
    
    #define SAVE_IE_WITH_CHECK(ie, ie_element, save_func, ...)\
        do{\
            if ( IE_PRESENT == IE_ELEMENT_PRESENT(ie, ie_element) )\
            {\
                CHECK_SAVE_IE_RESULT(ie_element, (save_func(IE_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__)));\
            }\
        }while(0);
    
    #define CHECK_SAVE_IE_RESULT_RETURN_FAILURE(ie_element, save_func)\
        do{\
            if(SUCCESS != save_func)\
            {\
                UE_LOG_ERR(g_dwUlMacLogId, "%s:save %s failure!", #save_func, #ie_element);\
                return FAILURE;\
            }\
        }while(0);
    
    #define SAVE_IE_WITH_CHECK_RETURN_FAILURE(ie, ie_element, save_func, ...)\
        do{\
            if ( IE_PRESENT == IE_ELEMENT_PRESENT(ie, ie_element) )\
            {\
                CHECK_SAVE_IE_RESULT_RETURN_FAILURE(ie_element, (save_func(IE_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__)));\
            }\
        }while(0);
    
    #define CHECK_IE_UNION_T_OUT_RANGE_RETURN(ie, ie_union_t_max, ie_union_t_min)\
        do{\
            CHECK_VALUE_OUT_RANGE_RETURN(IE_UNION_T(ie), ie_union_t_max, ie_union_t_min, __FUNCTION__);\
        } while(0);
    
    #define CHECK_IE_UNION_T_OUT_RANGE_RETURN_FAILURE(ie, ie_union_t_max, ie_union_t_min)\
        do{\
            CHECK_VALUE_OUT_RANGE_RETURN_FAILURE(IE_UNION_T(ie), ie_union_t_max, ie_union_t_min, __FUNCTION__);\
        } while(0);
    
    #define SAVE_IE_UNION_SIMPLE(ie, ie_union_t, ie_element, ie_saved)\
        do{\
            if (ie_union_t == IE_UNION_T(ie))\
            {\
                Save_##ie_element(IE_UNION_ELEMENT_POINTER(ie, ie_element), ie_saved);\
            }\
        } while(0);
    
    #define SAVE_IE_UNION(ie, ie_union_t, ie_element, save_func, ...)\
        do{\
            if ( ie_union_t == IE_UNION_T(ie) )\
            {\
                save_func(IE_UNION_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__);\
            }\
        } while(0);
    
    #define SAVE_IE_UNION_WITH_CHECK(ie, ie_union_t, ie_element, save_func, ...)\
        do{\
            if ( ie_union_t == IE_UNION_T(ie) )\
            {\
                CHECK_SAVE_IE_RESULT(ie_element, (save_func(IE_UNION_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__)));\
            }\
        } while(0);
    
    #define SAVE_IE_UNION_WITH_CHECK_RETURN_FAILURE(ie, ie_union_t, ie_element, save_func, ...)\
        do{\
            if ( ie_union_t == IE_UNION_T(ie) )\
            {\
                CHECK_SAVE_IE_RESULT_RETURN_FAILURE(ie_element, (save_func(IE_UNION_ELEMENT_POINTER(ie, ie_element), ##__VA_ARGS__)));\
            }\
        } while(0);
    
    #define SAVE_IE_UNION_SIMPLE_WITH_CHECK_RETURN_FAILURE(ie, ie_union_t, ie_element, ie_saved)\
        do{\
            if ( ie_union_t == IE_UNION_T(ie) )\
            {\
                CHECK_SAVE_IE_RESULT_RETURN_FAILURE(ie_element, (Save_##ie_element(IE_UNION_ELEMENT_POINTER(ie, ie_element), ie_saved)));\
            }\
        } while(0);

    相关文章

      网友评论

          本文标题:c/c++一些宏定义的使用小技巧

          本文链接:https://www.haomeiwen.com/subject/gszhlqtx.html