繁体   English   中英

C Function 在声明/编译时带有参数的指针

[英]C Function pointers with parameter at declaration/ compile time

假设我们有一个事件(int)和动作(函数指针)列表,如下所示:

typedef struct action_type {
    int event;
    int (*func)();
} action_type;

action_type actions[] = {
    { my_event1, my_action1 },
    { my_event2, my_action2 },
    { my_event3, my_action3 },
    { my_event4, my_action1 }
}

我想添加条件以作为表格的一部分实际运行操作,例如

  • 更好的可读性和
  • 动作函数的简化。
action_type actions[] = {
    { my_event1, exceeds(&temperature, 100)   , my_action1 },
    { my_event1, between(&temperature, 50, 80), my_action2 },
    ...
}

有没有办法得到这样的符号?

或者我需要创建类似的东西:

action_type actions[] = {
    { my_event1, $temperature, exceeds, 100, my_action1 },
    ...
}

但是,这种方法只允许具有固定类型的固定数量的参数。 当我正在编写一个库并且条件几乎可以是任何东西时,我正在寻找一种方法来允许 condition() function 的不同变量类型和不同参数计数。


编辑:添加了有关可能具有不同数量和类型参数的条件的附加信息。

我会使用 X_MACRO,即:像这样:

#define MY_EVENT_1 1
#define MY_EVENT_2 2
#define MY_EVENT_3 3

static int my_action1(void) { printf("Over 100!\n"); return 0; }
static int my_action2(void) { printf("Over 120!\n"); return 0; }
static int my_action3(void) { printf("Over 160!\n"); return 0; }

static int exceeds(int *val, int temp) { return *val > temp; }

typedef struct action_type {
   int event;
   int (*func)();
} action_type;

#define X_ACTIONS \
    X(MY_EVENT_1, exceeds(&temperature, 100), my_action1) \
    X(MY_EVENT_2, exceeds(&temperature, 120), my_action2) \
    X(MY_EVENT_3, exceeds(&temperature, 160), my_action3)

static action_type actions[] = {
#define X(type, cond, cb)  { type, cb },
    X_ACTIONS
#undef X
};

int main() {
    int temperature = 130;
#define X(type, cond, cb) if (cond) cb();
    X_ACTIONS
#undef X
    return 0;
}

你可以做这样的事情

#define __NO__FUNC  NULL
typedef unsigned char (*func)(void);
typedef unsigned char (*con)(unsigned char param1, unsigned char param2, unsigned char param3);

typedef struct {
  int event                      
  func   custom_func;
  con    condition;                             
} action_type;

然后像这样使用

action_type action[] = {
   { 1,SomeFunc, SomeCondition  },
   { 2,__NO__FUNC,__NO__FUNC   },
};

某些函数必须重新调整 unsigned char 并且输入类型为 void,例如

unsigned char SomeFunc(void)
{
  // some logic
  return 1;
}

 unsigned char SomeCondition(unsigned char param1, unsigned char param2, unsigned char param3)
 {
   // logic
   return 1;
 }

并像这样使用它

 if (action[0].condition(1,2,3)){
    action[0].custom_func();
 }

您还可以使用以下示例检查是否设置了 function:

 if (action[0].condition(1,2,3)){
    if(action[0].custom_func)
      action[0].custom_func();
 }

示例用法一起

  unsigned char SomeFunc(void)
  {
      // some logic
      return 1;
  }

 unsigned char SomeCondition(unsigned char param1, unsigned char param2, unsigned char param3)
 {
   // logic
   return 1;
 }


#define __NO__FUNC  NULL
typedef unsigned char (*func)(void);
typedef unsigned char (*con)(unsigned char param1, unsigned char param2, unsigned char param3);

typedef struct {
  int event                      
  func   custom_func;
  con    condition;                             
} action_type;

action_type action[] = {
   { 1,SomeFunc, SomeCondition  },
   { 2,__NO__FUNC,__NO__FUNC   },
};



void main(void)
{
  unsigned char temperature = 0;
  unsigned char val = 30;
  unsigned char another_val = 50;

  if (action[0].condition(temperature ,val,another_val )){
    action[0].custom_func();
  }

}

更新:我添加了第二个 function 参数,称为条件,可用于在执行 custom_func() 之前检查 output

更新 2:添加了条件 function 以接受变量作为输入

更新 3:我添加了如何使用它的示例代码,但是我想指出,在您的问题中,您不能在这些函数的声明中包含可变数量的 arguments。 C 语法不允许这样做。

在“ typedef unsigned char (*con)(unsigned char param1, unsigned char param2, unsigned char param3); ”类型的结构内创建一个元素

这意味着这些元素接受一个 function ,它返回 unsigned char 并且输入为 (unsigned char param1, unsigned char param2, unsigned char param3);

您不能添加具有不同返回类型或不同参数的函数。

希望能帮助到你! 此致

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM