2014-01-18 23 views
0

考虑以下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 
    } 
} 

我想实现在瓦拉是这样的:

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的约束力?

是否有可能将变量分成两部分?

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

回答

2

使用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; 
} 
}