C/C++ macro lib

Published:

Brief

Sometimes, we need to use macro to complete some miracle task. In this lib, you can use those macro to operate macro parameters.

Basic function

list

funcdesp
GET_MACRO_PARAS_TOTAL_NUM(…)get count of macro paras.
GET_MACRO_PARA_N(N, …)get macro para in order N.
GET_MACRO_PARAS_N_2_END(N, …)get macro paras from order N to end.
GET_MACRO_PARAS_START_2_N(N, …)get macro paras from order 0 to order N-1.
GET_MACRO_PARAS_N_2_M(N, M, …)get macro paras from order N to order M-1.
GET_MACRO_PARAS_N_LEN(N, LEN, …)get macro paras from order N to order N+LEN-1.
GET_MACRO_PARAS_RAM(N, …)get N macro paras in ramdom order.
MACRO_PARAS_ENUM_OPT(opt, …)provide ‘opt’ as a callback-func to every macro para.
MACRO_PARAS_ENUM_OPT_WITHOUT_LAST(opt, …)provide opt as a callback-func to every macro para except the last one.
MACRO_PARAS_NOTDEAL_0PARAS(opt, …)don’t deal when macro paras is none.

examples

GET_MACRO_PARAS_TOTAL_NUM()               // "0"
GET_MACRO_PARAS_TOTAL_NUM(a)              // "1"
GET_MACRO_PARAS_TOTAL_NUM(a, b, c, d)     // "4"

GET_MACRO_PARA_N(0, a, b, c, d)           // "a"
GET_MACRO_PARA_N(1, a, b, c, d)           // "b"
GET_MACRO_PARA_N(3, a, b, c, d)           // "d"

GET_MACRO_PARAS_N_2_END(0, a, b, c, d)    // "a, b, c, d"
GET_MACRO_PARAS_N_2_END(1, a, b, c, d)    // "b, c, d"
GET_MACRO_PARAS_N_2_END(3, a, b, c, d)    // "d"

GET_MACRO_PARAS_START_2_N(0, a, b, c, d)  // ""
GET_MACRO_PARAS_START_2_N(1, a, b, c, d)  // "a"
GET_MACRO_PARAS_START_2_N(3, a, b, c, d)  // "a, b, c"

GET_MACRO_PARAS_N_2_M(0, 0, a, b, c, d)   // ""
GET_MACRO_PARAS_N_2_M(0, 1, a, b, c, d)   // "a"
GET_MACRO_PARAS_N_2_M(1, 2, a, b, c, d)   // "b"
GET_MACRO_PARAS_N_2_M(1, 3, a, b, c, d)   // "b, c"
GET_MACRO_PARAS_N_2_M(3, 3, a, b, c, d)   // ""

GET_MACRO_PARAS_N_LEN(0, 0, a, b, c, d)   // ""
GET_MACRO_PARAS_N_LEN(0, 1, a, b, c, d)   // "a"
GET_MACRO_PARAS_N_LEN(0, 4, a, b, c, d)   // "a, b, c, d"
GET_MACRO_PARAS_N_LEN(3, 1, a, b, c, d)   // "d"
GET_MACRO_PARAS_N_LEN(4, 0, a, b, c, d)   // ""

GET_MACRO_PARAS_RAM(0, a, b, c, d)        // ""
GET_MACRO_PARAS_RAM(1, 0, a, b, c, d)     // "a"
GET_MACRO_PARAS_RAM(1, 1, a, b, c, d)     // "b"
GET_MACRO_PARAS_RAM(1, 3, a, b, c, d)     // "d"
GET_MACRO_PARAS_RAM(2, 0, 3, a, b, c, d)  // "a, d"

MACRO_PARAS_ENUM_OPT(opt)                 // ""
MACRO_PARAS_ENUM_OPT(opt, a)              // "opt(1, a)"
MACRO_PARAS_ENUM_OPT(opt, a, b, c, d)     // "opt(4, a) opt(3, b) opt(2, c) opt(1, d)"

MACRO_PARAS_ENUM_OPT_WITHOUT_LAST(opt)              // ""
MACRO_PARAS_ENUM_OPT_WITHOUT_LAST(opt, a)           // "a"
MACRO_PARAS_ENUM_OPT_WITHOUT_LAST(opt, a, b, c, d)  // "opt(4, a) opt(3, b) opt(2, c) d"

MACRO_PARAS_NOTDEAL_0PARAS(opt)                 // ""
MACRO_PARAS_NOTDEAL_0PARAS(opt, a)              // "opt(1, a)"
MACRO_PARAS_NOTDEAL_0PARAS(opt, a, b, c, d)     // "opt(4, a, b, c, d)"

Struct function

list

funcdesp
GET_STRUCT_TYPE(name)get name of the struct generated by ‘GEN_STRUCT’.
GET_STRUCT_AUTO_TYPE(name)get name of the struct generated by ‘GEN_STRUCT’.
GET_STRUCT_VAR(name)get name of the struct variate generated by ‘GEN_STRUCT’.
GET_STRUCT_AUTO_VAR()get name of the struct variate generated by ‘GEN_STRUCT’.
GEN_STRUCT(name, …)generate a struct and variate of this struct.
GEN_STRUCT_AUTO(…)generate a struct and variate of this struct.
GEN_TEMP_VARS(…)generate corresponding temp vars of macro paras.
SET_TEMP_VARS(…)set corresponding temp vars of macro paras.
GET_TEMP_VARS(…)get corresponding temp vars of macro paras.
SET_STRUCT_AUTO(…)set corresponding struct’s member of macro paras.

examples

#define GEN_EXAMPLE(...)                              \
    do                                                \
    {                                                 \
        GEN_TEMP_VARS(__VA_ARGS__);                   \
        SET_TEMP_VARS(__VA_ARGS__);                   \
        GEN_STRUCT_AUTO(GET_TEMP_VARS(__VA_ARGS__));  \
        SET_STRUCT_AUTO(GET_TEMP_VARS(__VA_ARGS__));  \
        GET_STRUCT_AUTO_VAR_MEMBER_N(0, __VA_ARGS__); \
    } while (0)

GEN_EXAMPLE(1, 2, 3)
GEN_TEMP_VARS(__VA_ARGS__);                     // typeof(1) temp_52_3, typeof(2) temp_52_2, typeof(3) temp_52_1;
SET_TEMP_VARS(__VA_ARGS__);                     // temp_52_3 = 1; temp_52_2 = 2; temp_52_1 = 3; ;
GET_TEMP_VARS(__VA_ARGS__);                     // temp_52_3, temp_52_2, temp_52_1;
GEN_STRUCT_AUTO(GET_TEMP_VARS(__VA_ARGS__));    // struct gen_auto52_stru
                                                // {
                                                //     typeof(temp_52_3) temp_52_3;
                                                //     typeof(temp_52_2) temp_52_2;
                                                //     typeof(temp_52_1) temp_52_1;
                                                // } __attribute__((__packed__)) gen_auto52;
SET_STRUCT_AUTO(GET_TEMP_VARS(__VA_ARGS__));    // gen_auto52.temp_52_3 = temp_52_3;
                                                // gen_auto52.temp_52_2 = temp_52_2;
                                                // gen_auto52.temp_52_1 = temp_52_1;
                                                // ;
GET_STRUCT_AUTO_VAR_MEMBER_N(0, __VA_ARGS__);   // gen_auto52.temp_52_0;

Type function

list

funcdesp
GET_MACRO_PARAS_SIZE(…)return paras size as array.
GET_MACRO_PARAS_SIZE_FIX(…)return paras size as fixed length(10) array
GET_MACRO_PARAS_TYPE(…)return paras type code as array.
GET_MACRO_PARAS_TYPE_FIX(…)return paras type code as fixed length(10) array.

examples

GET_MACRO_PARAS_SIZE()                // ""
GET_MACRO_PARAS_SIZE(a)               // "sizeof(a)"
GET_MACRO_PARAS_SIZE(a, b, c, d)      // "sizeof(a), sizeof(b), sizeof(c), sizeof(d)"

GET_MACRO_PARAS_SIZE_FIX()            // "0, 0, 0, 0, 0, 0, 0, 0, 0, 0"
GET_MACRO_PARAS_SIZE_FIX(a)           // "sizeof(a), 0, 0, 0, 0, 0, 0, 0, 0, 0"
GET_MACRO_PARAS_SIZE_FIX(a, b, c, d)  // "sizeof(a), sizeof(b), sizeof(c), sizeof(d), 0, 0, 0, 0, 0, 0"

GET_MACRO_PARAS_TYPE()                // ""
GET_MACRO_PARAS_TYPE(a)               // "(__builtin_types_compatible_p(typeof(a), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(a), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(a), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(a), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(a), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(a), typeof(const char *)) ? 9 : 0)))))))))"
GET_MACRO_PARAS_TYPE(a, b, c, d)      // "(__builtin_types_compatible_p(typeof(a), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(a), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(a), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(a), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(a), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(a), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(b), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(b), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(b), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(b), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(b), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(b), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(c), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(c), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(c), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(c), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(c), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(c), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(d), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(d), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(d), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(d), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(d), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(d), typeof(const char *)) ? 9 : 0)))))))))"

GET_MACRO_PARAS_TYPE_FIX()            // "0, 0, 0, 0, 0, 0, 0, 0, 0, 0"
GET_MACRO_PARAS_TYPE_FIX(a)           // "(__builtin_types_compatible_p(typeof(a), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(a), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(a), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(a), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(a), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(a), typeof(const char *)) ? 9 : 0))))))))), 0, 0, 0, 0, 0, 0, 0, 0, 0"
GET_MACRO_PARAS_TYPE_FIX(a, b, c, d)  // "(__builtin_types_compatible_p(typeof(a), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(a), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(a), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(a), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(a), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(a), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(a), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(b), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(b), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(b), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(b), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(b), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(b), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(b), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(c), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(c), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(c), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(c), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(c), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(c), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(c), typeof(const char *)) ? 9 : 0))))))))), (__builtin_types_compatible_p(typeof(d), char) ? 1 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned char) ? 2 : 
                                      // (__builtin_types_compatible_p(typeof(d), short) ? 3 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned short) ? 4 : 
                                      // (__builtin_types_compatible_p(typeof(d), int) ? 5 : 
                                      // (__builtin_types_compatible_p(typeof(d), unsigned int) ? 6 : 
                                      // (__builtin_types_compatible_p(typeof(d), float) ? 7 : 
                                      // (__builtin_types_compatible_p(typeof(d), double) ? 8 : 
                                      // (__builtin_types_compatible_p(typeof(d), typeof(const char *)) ? 9 : 0))))))))), 0, 0, 0, 0, 0, 0"

Repository

Git repository:
github.com/novumdun/macro_func_collection