1

考虑这些 C 函数:

#define INDICATE_SPECIAL_CASE -1
void prepare (long *length_or_indicator);
void execute ();

准备函数用于存储指向延迟long *输出变量的指针。

它可以像这样在C中使用:

int main (void) {
    long length_or_indicator;
    prepare (&length_or_indicator);
    execute ();
    if (length_or_indicator == INDICATE_SPECIAL_CASE) {
        // do something to handle special case
    }
    else {
        long length = lengh_or_indicator;
        // do something to handle the normal case which has a length
    }
}

我试图在 Vala 中实现这样的目标:

int main (void) {
    long length;
    long indicator;
    prepare (out length, out indicator);
    execute ();
    if (indicator == INDICATE_SPECIAL_CASE) {
        // do something to handle special case
    }
    else {
        // do something to handle the normal case which has a length
    }
}

如何为prepare ()INDICATE_SPECIAL_CASE在 Vala 中编写绑定?

是否可以将变量一分为二?

即使在调用(in )out之后写入变量,是否可以避免使用指针?prepare ()execute ()

4

1 回答 1

3

使用的问题out是 Vala 会在此过程中生成大量临时变量,这会导致引用错误。您可能想要做的是在您的 VAPI 中创建一个隐藏所有这些的方法:

[CCode(cname = "prepare")]
private void _prepare (long *length_or_indicator);
[CCode(cname = "execute")]
private void _execute ();
[CCode(cname = "prepare_and_exec")]
public bool execute(out long length) {
  long length_or_indicator = 0;
  prepare (&length_or_indicator);
  execute ();
  if (length_or_indicator == INDICATE_SPECIAL_CASE) {
     length = 0;
     return false;
  } else {
     length = lengh_or_indicator;
     return true;
 }
}
于 2014-01-18T16:11:17.207 回答