1

ポインターによっても参照されるコールバック関数に、ポインターによって可変数の引数を渡したいと考えています。参照渡しできる引数のリストを作成する方法はありますか?

元:

typedef struct MENUELEMENT
{
  void* OK_func; void* OK_args;
} menuElement_t;

menuElement_t* curMenuElement;

menuElement_t menu[] =
{
  //First Menu Element
  (void*)menuDisplayText, (void*)("Test", (char*)&lcdBuffer[0]) //menuDisplayText is a function that takes two arguments
  //Second Menu Element
  (void*)menuDisplayVal, (void*)&value[0] //menuDisplayVal is a function that takes one argument
};

void loop() //Main Loop - just an example of how the function pointed to by curMenuElement is executed
{
  curMenuElement = &menu[0];
  if(KP_OK)
  {
    (*reinterpret_cast<void (*)(...)>(curMenuElement->OK_func))(curMenuElement->OK_args); //General template for function pointed to at OK_func with OK_args
  }
}

これまでのところ、これは 1 つの引数でうまく機能しますが、構造体変数の初期化で複数の引数のリストを渡す方法がわかりません。これは、va_list を使用するビルダー関数を使用しなくても可能ですか?

4

1 に答える 1

0

可変個引数関数は、スタック上のパラメーターのみを受け入れます。ループは、特定の関数の構造体にいくつの引数値があるかを知る必要があります。次に、アセンブラー レイヤーにドロップダウンして値を手動でスタックにプッシュし、現在の関数を呼び出し、最後に値を関数の終了後にスタックします。確かに実行可能ですが、手動で行うには多くの作業が必要であり、コンパイル時間を安全に失うことになります。

構造体自体、または少なくともそのメンバーを各関数に渡し、必要に応じて値の使用方法を決定させる方がよいでしょう。例えば:

typedef void (*menuFunc)(void** args, int numArgs);

typedef struct MENUELEMENT
{
    menuFunc OK_func;
    int OK_num_args;
    void** OK_args;
} menuElement_t;

.

void menuDisplayText(void** args, int numArgs)
{
    ...
}

void menuDisplayVal(void** args, int numArgs)
{
    ...
}

void* menuDisplayTextArgs[] =
{
    "Test"
    &lcdBuffer[0]
};

menuElement_t menu[] =
{
  {&menuDisplayText, 2, menuDisplayTextArgs},
  {&menuDisplayVal, 1, &value[0]}
};

.

void loop()
{
    menuElement_t* curMenuElement = &menu[0];
    if (KP_OK)
    {
        curMenuElement->OK_func(curMenuElement->OK_args, curMenuElement->OK_num_args);
    }
}
于 2013-04-23T23:44:17.397 に答える