-
-
Notifications
You must be signed in to change notification settings - Fork 657
[WIP] add extern (C++) struct Darray(T) and Dstring to simplify interop with C++ #7893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /+ | ||
| Example usage: | ||
| extern (C++) Dstring funLib(Dstring input) | ||
| { | ||
| import std.string : toUpper; | ||
| import std.conv : to; | ||
| return input.toNative.toUpper.to!Dstring; | ||
| } | ||
| +/ | ||
|
|
||
| extern (C++) struct Darray(T) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're going to create a wrapper type like this you'll probably need to do it in the same way that I've done in You may also need to support implicit conversion to/from native D arrays, but implicit conversion between multiple types requires multiple alias this. Take a look at |
||
| { | ||
| size_t length; | ||
| T* ptr; | ||
|
|
||
| extern (D) this(T[] a) | ||
| { | ||
| this.length = a.length; | ||
| this.ptr = a.ptr; | ||
| } | ||
|
|
||
| extern (D) T[] toNative() | ||
| { | ||
| // TODO: could even use a raw cast since ABI is same | ||
| return ptr[0 .. length]; | ||
| } | ||
| } | ||
|
|
||
| alias Dstring = Darray!char; | ||
| alias Dcstring = Darray!(const(char)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (resolved privately on slack https://dlang.slack.com/archives/D8ZMKRGUV/p1521139433000456) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The slack conversation is private, so the link isn't helpful to other people. in short: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not only that but |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve been thinking how to implement this. The idea I had was to add a new method to the
Typebase class calledtypeForMangling, which takes a mangling as its argument and returns the type that should be used for that mangling. This allows a type to pose as another type during mangling. TheT[]type would need to construct aDSlice!Ttype during the semantic phase. In this case,typeForManglingwould be called when manglingT[]and use the mangling of the new type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[ this PR is now doing two things at once, or is this a cherry pick that is about to be merged? ]