【问题标题】:Look-up table of C macrosC宏的查找表
【发布时间】:2022-12-09 10:31:45
【问题描述】:

我正在 2 个驱动程序之间制作查找表。我使用一个从索引 0 开始的枚举,它被传递到一个查找表以返回一个整数。这很好用。

// In library
#define LIB_FEATURE_1 0x10
#define LIB_FEATURE_2 0x22
#define LIB_FEATURE_3 0x44
#define LIB_FEATURE_4 0x42

// In my costum driver
enum features_s {
    my_feature_a = 0,
    my_feature_b = 1,
    my_feature_c = 2
};

const int feature_tbl[] = {
    LIB_FEATURE_2,   // Maps my_feature_a to LIB_FEATURE_2
    0xFF,            // Maps my_feature_b to an error code because this  
                     //     feature is not present in library
    LIB_FEATURE_4    // Maps my_feature_c to LIB_FEATURE_4
};

// In app
int value = feature_tbl[my_feature_a];

该库包含一些更复杂的宏(在嵌入式系统中设置寄存器):

// In library
#define LIB_FEATURE_1 do {
                    //does black magic
                } while(0)

#define LIB_FEATURE_2 do {
                    //does white magic
                } while(0)

#define LIB_FEATURE_3 do {
                    //does yellow magic
                } while(0)

#define LIB_FEATURE_4 do {
                    //does purple magic
                } while(0)

// In my costum driver
enum features_s {
    my_feature_a = 0,
    my_feature_b = 1,
    my_feature_c = 2
};

/* 
 * something missing here. I want this mapping :
 * my_feature_a executes LIB_FEATURE_2();
 * my_feature_b executes nothing
 * my_feature_c executes LIB_FEATURE_4();
 */

// In app
SOME_KIND_OF_LOOK_UP_TABLE[my_feature_a](); 

是否可以创建一个 const 表,或者一个将索引作为参数并执行正确功能的宏?

我也试过宏连接,但它似乎不起作用。

我尝试使用常量表和宏连接

【问题讨论】:

  • 宏是编译前的文本替换。它们在运行时不作为自己的实体存在。您可能会考虑使用函数指针数组。函数体将是您的宏现在的样子。另一种选择是编写一个函数(或大型宏),它有一个开关,不同的情况下调用不同的宏。

标签: c macros lookup-tables


【解决方案1】:
//function prototype
typedef void (*my_lib_feature_function)();

//implement feature functions
//we need an address, therefore real functions instead of macros
static void my_lib_feature_function_1() { /* feature magic 1 */ }
static void my_lib_feature_function_2() { /* feature magic 2 */ }
static void my_lib_feature_function_3() { /* feature magic 3 */ }
static void my_lib_feature_function_4() { /* feature magic 4 */ }

//feature table (lookup table)
//organize as you see fit, extend or shorten it, but
//take care of the right indices (enum values)
static const my_lib_feature_function function_table[] = {
    my_lib_feature_function_2, //index 0 -> my_feature_a
    NULL,                      //index 1 -> my_feature_b
    my_lib_feature_function_4  //index 2 -> my_feature_c
};

//invoker
//param feature is the index to the lookup table
static void invoke_my_lib_feature(enum features_s feature)
{
    if (function_table[feature] != NULL) //if entry exists
        function_table[feature]();       //execute it
}

//In app
/*either*/ //invoke_my_lib_feature(my_feature_a);
/*and/or*/ //invoke_my_lib_feature(my_feature_b);
/*and/or*/ //invoke_my_lib_feature(my_feature_c);

【讨论】:

    猜你喜欢
    • 2011-03-28
    • 2020-03-14
    • 2018-01-03
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多