Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

On Friday, 4 May 2018 at 15:37:28 UTC, ag0aep6g wrote:

On Friday, 4 May 2018 at 13:02:08 UTC, NewUser wrote:

How can I use the following c structure from d.

struct Item
{
  int id;
};

struct Group
{
  int i;
  int item_count;
  struct Item items[];
};

tried defining items[] as both "Item[] items" and "Item* 
items" in d, it compiles okay but gives an error when trying 
to access it.


Here is the error.
object.Error@(0): Access Violation


In the C code, the elements of `items` are directly part of the 
struct. There is no indirection. D doesn't have dedicated 
syntax for this, but you can hint at it with a zero-sized array:


struct Group
{
int i;
int item_count;
Item[0] items;
}

Then access an item with `group.items.ptr[index]`.


Hi ag0aep6g,

Thanks a lot for that.

I should have thought of that (i would still have missed the .ptr 
part), the old c syntax for the same thing used to be "Item 
items[0]".


Thanks,
NewUser





Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

On Friday, 4 May 2018 at 14:55:18 UTC, Timoses wrote:

On Friday, 4 May 2018 at 13:27:03 UTC, NewUser wrote:

Hi Timoses,

The structure is being returned from c and I'd like use it 
from d.


What you have work perfectly when assigning from d.


Regards,
NewUser


Then you probably need some `extern(C)` statement introducing 
the C function and the struct type, right?


You could also try dstep to "translate" the C header file to D:
https://github.com/jacob-carlborg/dstep


Hi Timoses,

Thanks for the suggestion, i'll try it out.

Regards,
NewUser


Re: using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

On Friday, 4 May 2018 at 13:21:53 UTC, Timoses wrote:

On Friday, 4 May 2018 at 13:02:08 UTC, NewUser wrote:
tried defining items[] as both "Item[] items" and "Item* 
items" in d, it compiles okay but gives an error when trying 
to access it.


You were on the right track. D array notation is:

[] ;

For me this works:

```
struct Item
{
  int id;
};

struct Group
{
  int i;
  int item_count;
  Item[] items;
};

void main()
{
auto g = Group();
g.items ~= Item(3);
assert(g.items[0].id == 3);
}
```


Hi Timoses,

The structure is being returned from c and I'd like use it from d.

What you have work perfectly when assigning from d.


Regards,
NewUser



using Unsized Arrays in Structures from d?

2018-05-04 Thread NewUser via Digitalmars-d-learn

Hi,

How can I use the following c structure from d.

struct Item
{
  int id;
};

struct Group
{
  int i;
  int item_count;
  struct Item items[];
};

tried defining items[] as both "Item[] items" and "Item* items" 
in d, it compiles okay but gives an error when trying to access 
it.


Here is the error.
object.Error@(0): Access Violation

0x00BCA9D1
0x00BC104C
0x00BD01EB
0x00BD0169
0x00BD
0x00BCA827
0x74118654 in BaseThreadInitThunk
0x77534B17 in RtlGetAppContainerNamedObjectPath
0x77534AE7 in RtlGetAppContainerNamedObjectPath


Regards,
NewUser




Re: Passing to c++ std::string and vector

2018-04-30 Thread NewUser via Digitalmars-d-learn

On Monday, 30 April 2018 at 10:48:40 UTC, Jonathan M Davis wrote:
On Monday, April 30, 2018 01:07:35 NewUser via 
Digitalmars-d-learn wrote:

Hi,

How do I pass a d string to a c++ std::string?


The most straightforward way would be to create a C or C++ 
function which accepts const char* and size_t and then creates 
the std::string, in which case you pass it arr.ptr (or [0]) 
and arr.length from D (and you could create a D helper function 
that takes a dynamic array to then call the C/C++ function if 
you don't want to use the array properties directly every time).


Essentially, you'll probably end up creating wrapper functions 
for any C++ functions that you want to call that take 
std::string, since there is not currently a straightforward way 
to construct a std::string from D (there's Calypso, but using 
it means that you're tied to ldc). Solutions are likely 
forthcoming, but there's nothing production-ready at this 
point. Do interacts reasonably well when it's operatoring on 
pointers to C++ classes, but as soon as it has to deal with 
construction or destruction, things get more complicated (which 
means that C++ classes on the stack definitely get more 
complicated). You can still get things to work, but it's 
frequently not straightforward in the way that calling C 
functions is.


- Jonathan M Davis


Thanks.


Passing to c++ std::string and vector

2018-04-29 Thread NewUser via Digitalmars-d-learn

Hi,

How do I pass a d string to a c++ std::string?

NewUser