簡體   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