正确使用相同操作和不同类型的回调

Correct use of Callback for same operation and different type

我正在用 C 开发,我需要询问有关回调使用的信息。

假设我定义了 3 个回调,其中有 3 种不同类型的回调输入,例如:

typedef void (*CB_1) (const struct paramType_1 *p);
typedef void (*CB_2) (const struct paramType_2 *p);
typedef void (*CB_3) (const struct paramType_3 *p);

好的,我有 3 个回调数组,每个回调类型:

static CB_1         CB1List[10] ;
static CB_2         CB2List[10] ;
static CB_3         CB3List[10] ;

所以我定义了 3 个要调用的回调列表(在可能不同的情况下),每个列表都是特定类型的回调(CB_1、CB_2 或 CB_3)具有特定的回调参数(paramType_1、paramType_2 或 paramType_3)。

现在假设我需要为每个回调执行一个相同的操作...由于不同的特定参数,我必须复制粘贴 3 次函数...假设我需要向数组添加一个回调我需要这个;:

   static void CBAdd_1(CB_1 _cb) {   
      CB1List[i] = _cb
    }
   static void CBAdd_2(CB_2 _cb) {   
      CB2List[i] = _cb
    }
    static void CBAdd_3(CB_3 _cb) {  
      CB3List[i] = _cb
    }

使用通用函数的正确方法是什么 "void CBAdd" 不复制三次回调函数的三次?也许使用 (void*) 参数或其他?

谢谢

是的,您需要一个如下所示的 void* 参数。或者(取决于您的需要)您可以使用联合而不是结构并将所有成员存储在该单一联合类型中。这样,联合类型参数将传递给所有函数,函数定义可以决定使用哪个成员。

#include <stdio.h>

typedef struct paramType_1
{
  int p;
} paramType_1;

typedef struct paramType_2
{
  int p;
} paramType_2;

typedef struct paramType_3
{
  int p;
} paramType_3;

typedef void (*CB) (const void* p);

static CB CBList[10];

void CB_1(const void* p)
{
  const paramType_1* pt = (paramType_1*)p;
  printf("p1: %d\n", pt->p);
}

void CB_2(const void* p)
{
  const paramType_2* pt = (paramType_2*)p;
  printf("p2: %d\n", pt->p);
}

void CB_3(const void *p)
{
  const paramType_3* pt = (paramType_3*)p;
  printf("p3: %d\n", pt->p);
}

static int c = 0;

static void CBAdd(CB cb_)
{
  CBList[c++] = cb_;
}

int main()
{
  int i = 0;
  paramType_1 p1;
  paramType_2 p2;
  paramType_3 p3;

  for (i = 0; i < 3; i++)
  {
    CBAdd(CB_1);
  }

  for (i = 0; i < 3; i++)
  {
    CBAdd(CB_2);
  }

  for (i = 0; i < 3; i++)
  {
    CBAdd(CB_3);
  }

  p1.p = 1;
  p2.p = 2;
  p3.p = 3;

  for (i = 0; i < 3; i++)
  {
    (CBList[i])((void*)&p1);
  }

  for (i = 3; i < 6; i++)
  {
    (CBList[i])((void*)&p2);
  }

  for (i = 6; i < 9; i++)
  {
    (CBList[i])((void*)&p3);
  }

  return 0;
}