Question

Consider these C functions:

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

The prepare function is used to store a pointer to a delayed long * output variable.

It can be used in C like this:

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
    }
}

I am trying to achieve something like this in 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
    }
}

How to write the binding for prepare () and INDICATE_SPECIAL_CASE in Vala?

Is it possible to split the variable into two?

Is it possible to avoid using pointers even though the out variable is written to after the call to prepare () (in execute ())?

Was it helpful?

Solution

The problem with using out is that Vala is going to generate lots of temporary variables along the way, which will make the reference wrong. What you probably want to do is create a method in your VAPI that hides all this:

[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;
 }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top