www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C bindings: typedef struct conflicts with method

reply "yawniek" <dlang srtnwz.com> writes:
i tried to automagically create bindings for librdkafka 
(https://github.com/edenhill/librdkafka)
with dstep.

now the code contains typedefs structs with the same name as 
methods:

```
typedef struct rd_kafka_metadata {
         int         broker_cnt;     /* Number of brokers in 
'brokers' */
         struct rd_kafka_metadata_broker *brokers;  /* Brokers */

         int         topic_cnt;      /* Number of topics in 
'topics' */
         struct rd_kafka_metadata_topic *topics;    /* Topics */

         int32_t     orig_broker_id; /* Broker originating this 
metadata */
         char       *orig_broker_name; /* Name of originating 
broker */
} rd_kafka_metadata_t;


rd_kafka_metadata (rd_kafka_t *rk, int all_topics,
                    rd_kafka_topic_t *only_rkt,
                    const struct rd_kafka_metadata **metadatap,
                    int timeout_ms);
```

what the correct way to bind these?
Jul 20 2015
next sibling parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 21/07/2015 5:53 p.m., yawniek wrote:
 i tried to automagically create bindings for librdkafka
 (https://github.com/edenhill/librdkafka)
 with dstep.

 now the code contains typedefs structs with the same name as methods:

 ```
 typedef struct rd_kafka_metadata {
          int         broker_cnt;     /* Number of brokers in 'brokers' */
          struct rd_kafka_metadata_broker *brokers;  /* Brokers */

          int         topic_cnt;      /* Number of topics in 'topics' */
          struct rd_kafka_metadata_topic *topics;    /* Topics */

          int32_t     orig_broker_id; /* Broker originating this metadata */
          char       *orig_broker_name; /* Name of originating broker */
 } rd_kafka_metadata_t;


 rd_kafka_metadata (rd_kafka_t *rk, int all_topics,
                     rd_kafka_topic_t *only_rkt,
                     const struct rd_kafka_metadata **metadatap,
                     int timeout_ms);
 ```

 what the correct way to bind these?
The c function matters. The struct's two names do not. Just use the struct's _t name.
Jul 20 2015
prev sibling next sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2015-07-21 07:53, yawniek wrote:
 i tried to automagically create bindings for librdkafka
 (https://github.com/edenhill/librdkafka)
 with dstep.

 now the code contains typedefs structs with the same name as methods:

 ```
 typedef struct rd_kafka_metadata {
          int         broker_cnt;     /* Number of brokers in 'brokers' */
          struct rd_kafka_metadata_broker *brokers;  /* Brokers */

          int         topic_cnt;      /* Number of topics in 'topics' */
          struct rd_kafka_metadata_topic *topics;    /* Topics */

          int32_t     orig_broker_id; /* Broker originating this metadata */
          char       *orig_broker_name; /* Name of originating broker */
 } rd_kafka_metadata_t;


 rd_kafka_metadata (rd_kafka_t *rk, int all_topics,
                     rd_kafka_topic_t *only_rkt,
                     const struct rd_kafka_metadata **metadatap,
                     int timeout_ms);
 ```

 what the correct way to bind these?
Please report an issue for this. In this case "rd_kafka_metadata_t" should be used for the struct name. -- /Jacob Carlborg
Jul 20 2015
parent reply "yawniek" <dlang srtnwz.com> writes:
On Tuesday, 21 July 2015 at 06:12:53 UTC, Jacob Carlborg wrote:
 what the correct way to bind these?
Please report an issue for this. In this case "rd_kafka_metadata_t" should be used for the struct name.
done, https://github.com/jacob-carlborg/dstep/issues/40 i was under the impression that there is already a ticked as https://github.com/jacob-carlborg/dstep/issues/8 looks very similar (but was closed). thanks rikki, thats what i ended up doing, compiles so far.
Jul 21 2015
parent Jacob Carlborg <doob me.com> writes:
On 2015-07-21 14:24, yawniek wrote:

 done, https://github.com/jacob-carlborg/dstep/issues/40
 i was under the impression that there is already a ticked as
 https://github.com/jacob-carlborg/dstep/issues/8
 looks very similar (but was closed).
Yeah, looks very similar. Issue 8 i still open and has never been closed. -- /Jacob Carlborg
Jul 21 2015
prev sibling parent reply "Mike Parker" <aldacron gmail.com> writes:
On Tuesday, 21 July 2015 at 05:53:26 UTC, yawniek wrote:
 i tried to automagically create bindings for librdkafka 
 (https://github.com/edenhill/librdkafka)
 with dstep.

 now the code contains typedefs structs with the same name as 
 methods:

 ```
 typedef struct rd_kafka_metadata {
         int         broker_cnt;     /* Number of brokers in 
 'brokers' */
         struct rd_kafka_metadata_broker *brokers;  /* Brokers */

         int         topic_cnt;      /* Number of topics in 
 'topics' */
         struct rd_kafka_metadata_topic *topics;    /* Topics */

         int32_t     orig_broker_id; /* Broker originating this 
 metadata */
         char       *orig_broker_name; /* Name of originating 
 broker */
 } rd_kafka_metadata_t;


 rd_kafka_metadata (rd_kafka_t *rk, int all_topics,
                    rd_kafka_topic_t *only_rkt,
                    const struct rd_kafka_metadata **metadatap,
                    int timeout_ms);
 ```

 what the correct way to bind these?
Just FYI, the D side knows absolutely nothing about what the struct is called on the C side and vice versa. Only functions (and any global variables if you need them) are actually bound. Types are translated. So you can call the struct anything you want on the D side. That said, it's usually a good idea to use the the same name as the C code uses for consistency. It means existing C code can be ported to D, or with custom libraries you can switch back and forth, without needing to remember that a struct is call bloob_t in C and Blarg in D. In your case, rd_kafka_metadata is the name of the struct, but in C instances would need to be declared like so: struct rd_kafka_metadata instance; The typedef makes it so that instances can be declared in C without the struct keyword: rd_kafka_metadata_t instance; That makes the latter a better choice to use on the D side.
Jul 22 2015
parent reply Jacob Carlborg <doob me.com> writes:
On 2015-07-23 03:57, Mike Parker wrote:

 In your case, rd_kafka_metadata is the name of the struct, but in C
 instances would need to be declared like so:

 struct rd_kafka_metadata instance;
Since the struct is declared directly in the typedef, is the struct name actually available? -- /Jacob Carlborg
Jul 22 2015
parent "Mike Parker" <aldacron gmail.com> writes:
On Thursday, 23 July 2015 at 06:26:28 UTC, Jacob Carlborg wrote:
 On 2015-07-23 03:57, Mike Parker wrote:

 In your case, rd_kafka_metadata is the name of the struct, but 
 in C
 instances would need to be declared like so:

 struct rd_kafka_metadata instance;
Since the struct is declared directly in the typedef, is the struct name actually available?
Yes. It's just short hand for this: typedef struct foo foo_t; struct foo {}
Jul 23 2015