[Vala] How to deal with "target" attribute in VAPI for C struct?



Hi,

I have a part of C code that I need to include in my Vala project.

It contains code +/- like that:


typedef void (*computing_finished_cb) (void *self);

typedef struct computer {
  computing_finished_cb on_finished;
  void* parent;
} computer;

size_t computer_compute(computer *c);




I've created VAPI file like that

namespace MyLib {
  [CCode (cname="computer", cheader_filename="computer.h")]
  public struct Computer {
    ComputingFinishedCallback on_finished;
    void* parent;

    [CCode (cname="computer_compute")]
    public compute();
  }
  [CCode (cname="computing_finished_cb", has_target=false)]
  public delegate void ComputingFinishedCallback();
}


Now I can use it like that:

public class Example {
  public MyLib.Computer computer;

  public Example() {
    computer = MyLib.Computer();
    computer.parent = this; // code in compute() knows that it has to
pass this pointer to the callback when computing is finished
    computer.on_finished = on_finished;
  }

  public void compute() {
    computer.compute();
  }

  static void on_finished(void* sender) {
    MyLib.Computer computer = sender as MyLib.Computer;

    computer.do_something_else();
  }

  public void do_something_else() {
    //...
  }
}


It works but it breaks encapsulation as if I want to access anything
from on_finished() I have to mark it public.

Could you give me any advise how to modify that to achieve Vala-style
code? Like that...?


public class Example {
  public MyLib.Computer computer;

  public Example() {
    computer = MyLib.Computer(this);
    computer.on_finished = on_finished;
  }

  public void compute() {
    computer.compute();
  }

  private void on_finished() {
    do_something_else();
  }

  private void do_something_else() {
    //...
  }
}


The problem that I've encountered is that when I remove
"has_target=false" from VAPI, vala tries to initialize *_target and
*_target_destroy_notify functions per each callback member of the
struct, do a lot of ref & unrefs etc... Is there any clean way to do
that? Preferably without changing struct to full-flavored GObject?

Thank you in advance,

m.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]