www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Repeated struct definitions for graph data structures and in/out

reply data pulverizer <data.pulverizer gmail.com> writes:
Dear D Gurus,

I am trying to access functionality in the glpk C library using 
extern(C). It has graph structs in its header file that are 
specified in an odd recurring manner that I cannot reproduce in D:

typedef struct glp_graph glp_graph;
typedef struct glp_vertex glp_vertex;
typedef struct glp_arc glp_arc;

struct glp_graph
{
      ...
       glp_vertex **v; /* glp_vertex *v[1+nv_max];
};

struct glp_vertex
{
       ...
       glp_arc *in;
       glp_arc *out;
};

struct glp_arc
{
       glp_vertex *tail;
       glp_vertex *head;
       glp_arc *t_prev;
       glp_arc *t_next;
       glp_arc *h_prev;
       glp_arc *h_next;
       ....
};


you may also spot that the in, and out keywords are used as 
members in the struct, which gives an error in D. These structs 
are required for functions in the library so need to be included 
in the D interface file.

How do I reproduce these structs in in D?

Thanks
Jan 03 2016
parent reply anonymous <anonymous example.com> writes:
On 03.01.2016 14:30, data pulverizer wrote:
 I am trying to access functionality in the glpk C library using
 extern(C). It has graph structs in its header file that are specified in
 an odd recurring manner that I cannot reproduce in D:
I don't see what's odd about this. What exactly are your struggling with?
 typedef struct glp_graph glp_graph;
 typedef struct glp_vertex glp_vertex;
 typedef struct glp_arc glp_arc;
You can just drop these. http://dlang.org/ctod.html#tagspace
 struct glp_graph
 {
       ...
        glp_vertex **v; /* glp_vertex *v[1+nv_max];
 };
Drop the semicolon after the struct declaration, and move the asterisks one place to the left (purely style): struct glp_graph { glp_vertex** v; }
 struct glp_vertex
 {
        ...
        glp_arc *in;
        glp_arc *out;
 };
As above, and rename in/out to something else, e.g. in_ and out_: struct glp_vertex { glp_arc* in_; glp_arc* out_; }
 struct glp_arc
 {
        glp_vertex *tail;
        glp_vertex *head;
        glp_arc *t_prev;
        glp_arc *t_next;
        glp_arc *h_prev;
        glp_arc *h_next;
        ....
 };
Nothing new here.
 you may also spot that the in, and out keywords are used as members in
 the struct, which gives an error in D. These structs are required for
 functions in the library so need to be included in the D interface file.
Just rename them to something else. In D code that uses the struct, you use the new names. C code doesn't need to be changed, as the name doesn't matter when compiled.
Jan 03 2016
parent data pulverizer <data.pulverizer gmail.com> writes:
Thanks library now compiles.

On Sunday, 3 January 2016 at 13:45:13 UTC, anonymous wrote:
 On 03.01.2016 14:30, data pulverizer wrote:
 I am trying to access functionality in the glpk C library using
 extern(C). It has graph structs in its header file that are 
 specified in
 an odd recurring manner that I cannot reproduce in D:
I don't see what's odd about this. What exactly are your struggling with?
 typedef struct glp_graph glp_graph;
 typedef struct glp_vertex glp_vertex;
 typedef struct glp_arc glp_arc;
You can just drop these. http://dlang.org/ctod.html#tagspace
 struct glp_graph
 {
       ...
        glp_vertex **v; /* glp_vertex *v[1+nv_max];
 };
Drop the semicolon after the struct declaration, and move the asterisks one place to the left (purely style): struct glp_graph { glp_vertex** v; }
 struct glp_vertex
 {
        ...
        glp_arc *in;
        glp_arc *out;
 };
As above, and rename in/out to something else, e.g. in_ and out_: struct glp_vertex { glp_arc* in_; glp_arc* out_; }
 struct glp_arc
 {
        glp_vertex *tail;
        glp_vertex *head;
        glp_arc *t_prev;
        glp_arc *t_next;
        glp_arc *h_prev;
        glp_arc *h_next;
        ....
 };
Nothing new here.
 you may also spot that the in, and out keywords are used as 
 members in
 the struct, which gives an error in D. These structs are 
 required for
 functions in the library so need to be included in the D 
 interface file.
Just rename them to something else. In D code that uses the struct, you use the new names. C code doesn't need to be changed, as the name doesn't matter when compiled.
Jan 03 2016