Re: [Vala] trying vapi file for ngspice



thanks a lot,
it working afther some tweaking and testing.

but i got another question:

i would like to get the vector info struct working with this function:

VAPI:

[CCode (cname = "ngGet_Vec_Info")]
                public unowned VectorInfo get_vector_info(string                                              
  vector_name);

HEADER:

pvector_info ngGet_Vec_Info(char* vecname);


VAPI:

[CCode (cname = "vector_info", has_destroy_function = false,
has_copy_function = false, has_type_id = false)]
public struct VectorInfo {
                [CCode (cname = "v_name")]
                public unowned string name;
                [CCode (cname = "v_type")]              
                public int type;
                [CCode (cname = "v_flags")]             
                public short flags;
                [CCode (cname = "v_realdata",array_length = false)]
                public double[] data;
                [CCode (cname = "v_compdata",array_length = false)]  
                public NgComplex data_complex;
                [CCode (cname = "v_length")]
                public int length;
}

HEADER:

typedef struct vector_info {
    char *v_name;               
    int v_type;                 
    short v_flags;              
    double *v_realdata;         
    ngcomplex_t *v_compdata;    
    int v_length;
} vector_info, *pvector_info;

HEADER:

but it gives my an errors on the number of parameters that is not
correct.
It seems i need to connect to v_length parameter to the data, because
this is a vector of that length.

Any clues how to fix this or where to find info?

Evan Nemerson schreef op Tue 20-05-2014 om 12:43 [-0700]:
On Tue, 2014-05-20 at 19:55 +0200, Steven Vanden Branden wrote:
hello,
seems like the mail got lost but anyway have read the answer on
mailarchive about the bool, got the vapi to compile but get an segment
fault error when i try to run the application so i would like to review
the vapi for the function that causes it:

#if __GNUC__ >= 4
    #define IMPEXP __attribute__ ((visibility ("default")))
    #define IMPEXPLOCAL  __attribute__ ((visibility ("hidden")))


IMPEXP
int  ngSpice_Init(SendChar* printfcn, SendStat* statfcn, ControlledExit*
ngexit, SendData* sdata, SendInitData* sinitdata, BGThreadRunning*
bgtrun, void* userData);

the arguments are almost all pointer to callback functions and these i
need to get in my vala program.

vapi code :

[CCode (cname = "ngSpice_Init")]
          public int init(out SendOutput* a,out SendSimulationStatus* b,out ControlledExit* c
          , out SendVectorData* d, out SendInitializationData* e, out IsBackgroundThreadRunning* f, out 
void * userData);

Do not use pointers in Vala.  They are there for some corner cases which
will not work any other way, but in general if you are using pointers in
Vala you are doing it wrong.

I don't know why you think these are out parameters—based on a quick
look at the documentation they seem like in parameters.

It's untested, but you should try something along the lines of:

        [CCode (cname = "SendChar", has_target = false, simple_generics
        = true)]
        public delegate int SendChar<T> (string str, int id, T data);
        [CCode (cname = "SendStat", has_target = false, simple_generics
        = true)]
        public delegate int SendStat<T> (string str, int id, T data);
        [CCode (cname = "ControlledExit", has_target = false,
        simple_generics = true)]
        public delegate int ControlledExit<T> (int status, bool
        immediate, bool quit, int id, T data);
        [CCode (cname = "SendData", has_target = false, simple_generics
        = true)]
        public delegate int SendData<T> (VecValuesAll[] vectors, int id,
        T data);
        [CCode (cname = "SendInitData", has_target = false,
        simple_generics = true)]
        public delegate int SendInitData<T> ([CCode (array_length =
        false)] VecInfoAll[] vecs, int id, T data);
        [CCode (cname = "BGThreadRunning", has_target = false,
        simple_generics = true)]
        public delegate int BGThreadRunning<T> (bool running, int id, T
        data);
        
        [CCode (cname = "ngSpice_Init", simple_generics = true)]
        public static int init<T> (Ngspice.SendChar<T>? send_char,
        Ngspice.SendStat<T>? send_stat, Ngspice.ControlledExit<T>
        controlled_exit, Ngspice.SendData<T>? send_data,
        Ngspice.SendInitData<T>? send_data_init,
        Ngspice.BGThreadRunning<T>? bg_thread_running, T data);

A copy (without the wrapping): http://pastebin.com/u3HbaVpF

The only weird thing about this is you can't use closures.  The C API
takes a single parameter to pass to all the callbacks, instead of one
per callback like Vala expects, so you'll have to deal with that data
param manually.  Also, make sure it stays alive as long as you need it,
otherwise you'll probably end up with a use-after-free bug.


-Evan




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