考虑这些C函数:
#define INDICATE_SPECIAL_CASE -1
void prepare (long *length_or_indicator);
void execute ();
prepare函数用于存储指向延迟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
}
}
如何在Vala中编写prepare ()
和INDICATE_SPECIAL_CASE
的绑定?
是否可以将变量拆分为两个?
即使在调用out
之后写入prepare ()
变量(在execute ()
中),是否可以避免使用指针?
答案 0 :(得分: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;
}
}