【问题标题】:How to bind an argument with a dual function?如何将参数与对偶函数绑定?
【发布时间】:2014-01-18 13:22:00
【问题描述】:

考虑这些 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 编写绑定?

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

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

【问题讨论】:

    标签: pointers vala vapi


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2013-10-18
      • 1970-01-01
      相关资源
      最近更新 更多