Skip to content
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

Wrap C++ structs as C# structs where appropriate #500

Open
ddobrev opened this issue Jun 25, 2015 · 5 comments
Open

Wrap C++ structs as C# structs where appropriate #500

ddobrev opened this issue Jun 25, 2015 · 5 comments

Comments

@ddobrev
Copy link
Contributor

ddobrev commented Jun 25, 2015

Structs and classes are essentially the same in C++. However, the way they are used is almost identical as they way they are in C#. So it would be a good idea if a C++ struct is mapped to a C# struct. That is not always convenient - for example, if the struct is inherited, we won't be able to directly mirror that in C# since deriving from structs is forbidden there. Therefore in such cases mapping to a struct would cause more problems than it solves.
So the algorithm should:

  1. Only consider C++ structs, that is, types declared with the "struct" keyword;
  2. Apply a series of checks to the struct, such as searching the AST for derived types as stated above, to determine if it's beneficial to represent it as a C# struct.

Note: manual marking of types as structs must be left to give freedom to binders.

@genuinelucifer
Copy link
Contributor

@ddobrev Please review #501

@genuinelucifer
Copy link
Contributor

@ddobrev Please review #518

@genuinelucifer
Copy link
Contributor

@ddobrev
There is a corner case for this which can't be checked directly. As in NamespaceDerived and NamespaceBase Test in which both are compiled separately but the Derived one inherits from the Base.
We have practically no way of knowing that a future type (rather compiled separately) will inherit this or not. So, if we marshal NamespaceBase to struct then build will fail for NamespaceDerived.
I could think that if the user enables MarshalStructToStructWhereverPossible() Pass then he must also mark the C++ struct(s) with some macro to prohibit value type marshalling. (ex: STRICT_REF_TYPE)
What do you suggest as a workaround for this?

@genuinelucifer
Copy link
Contributor

@ddobrev Any comment?!

@tomspilman
Copy link
Member

Getting this working would be a huge benefit to many APIs cases we've run into. Currently we end up writing a dummy class to wrap the struct with getters/setters to access the fields. It is a lot of wasted effort when a C++ struct can be easily be represented in C#.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants