Skip to content

Added support for wchar_t and std::wstring for C##983

Draft
zillemarco wants to merge 1 commit intomono:mainfrom
zillemarco:master
Draft

Added support for wchar_t and std::wstring for C##983
zillemarco wants to merge 1 commit intomono:mainfrom
zillemarco:master

Conversation

@zillemarco
Copy link
Contributor

With the help of @tritao:

  • added support for wchar_t
  • added support for std::wstring
  • unified the way wide character strings are handled between C# and
    C++/CLI
  • changed the way strings are handled, using 'IntPtr' instead of 'string'
    with marshalling attributes on the P/Invokes

return encoding.GetString(buffer);
}

public static IntPtr StringToHGlobalMultiByteUni(string str)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't used anywhere.

// Deleting destructors (default in v-table) accept an i32 bitfield as a
// second parameter in MS ABI.
if (method != null && method.IsDestructor && Context.ParserOptions.IsMicrosoftAbi)
var @class = method != null ? method.Namespace as Class : null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needlessly complicated - if the method is not null, then its name-space is always a class, just cast it.

var type = ctx.Parameter.Type.Desugar();
ClassTemplateSpecialization basicString = GetBasicString(type);

if(basicString.Arguments[0].Type.Type.IsPrimitiveType(PrimitiveType.Char))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A space after the conditional operator.

// Struct added to help map a the wchar_t C++ type to C#
// The data is stored as a 64 bit value so it could represent
// an UTF-32 character but C# only represents UTF-16 characters
// so beware of possible data loss when using it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this means, is it that our binding for wstring does not always work properly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that C# char (UTF-16) type cannot represent the entirety of Unicode (UTF-32).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should then think about throwing an exception if out of range.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get this comment. wchar_t on Windows is represented as UTF16-LE. On Linux it's UTF-32. Char in C# is always UTF16-LE afaik. There is one more type to consider: char16_t is always UTF16-LE, regardless of the platform that's being used. (Since C++17). C++20 will also define UTF8 type char8_t.

@ddobrev ddobrev force-pushed the master branch 3 times, most recently from d0c8e15 to 5ca9345 Compare October 25, 2017 22:08
@ddobrev ddobrev force-pushed the master branch 4 times, most recently from 4dfd9cc to e53b253 Compare November 21, 2017 01:07
@ddobrev ddobrev force-pushed the master branch 12 times, most recently from 0464938 to 637018f Compare October 15, 2018 05:15
@ddobrev ddobrev force-pushed the master branch 4 times, most recently from 91e219c to 32da859 Compare January 4, 2019 21:27
@tritao tritao force-pushed the master branch 2 times, most recently from a0169c2 to 3ea7e97 Compare March 11, 2019 00:30
@phraemer
Copy link

phraemer commented Sep 4, 2020

Has this been abandoned? Seems like it would be a nice improvement!

@zillemarco
Copy link
Contributor Author

Frankly I have to really apologize for leaving this behind. I had other priorities at work so I couldn't give it the final push it needed.

I think I could be able to get back at this in my free time since not far from now I'll also need this at work.
If @tritao or @ddobrev could give me a little refresh on what was missing and/or what should be done I can give it a try.
Sorry but it's been almost 3 years and things most likely could have changed that impacted the work I did in this PR too.

@tritao
Copy link
Collaborator

tritao commented Sep 4, 2020

I don't recall anymore either, but think it was pretty much good to go.

Please rebase it on top of latest master, make sure all tests still pass and lets take it from there.

With the help of @tritao:
- added support for wchar_t
- added support for std::wstring
- unified the way wide character strings are handled between C# and
  C++/CLI
- changed the way strings are handled, using 'IntPtr' instead of 'string'
  with marshalling attributes on the P/Invokes
@dnfadmin
Copy link

dnfadmin commented Sep 5, 2020

CLA assistant check
All CLA requirements met.

@tritao
Copy link
Collaborator

tritao commented Sep 5, 2020

It's not compiling anymore:

Passes\IgnoreSystemDeclarationsPass.cs(60,49): error CS0103: The name 'GetCharSpecializations' does not exist in the current context [C:\projects\cppsharp\src\Generator\CppSharp.Generator.csproj]

Guess some code moved around or was renamed, and needs to be updated in this PR.

@zillemarco
Copy link
Contributor Author

Yeah I know, during the rebase I had quite a bit of conflicts. I was thinking about drafting the PR until I get it back to work.

@zillemarco zillemarco marked this pull request as draft September 14, 2020 15:16
@ddobrev ddobrev force-pushed the master branch 2 times, most recently from c930b78 to c38556a Compare January 2, 2021 20:41
Base automatically changed from master to main March 12, 2021 10:52
@ddobrev ddobrev force-pushed the main branch 7 times, most recently from 4c1e9b8 to 2fdd082 Compare August 30, 2021 11:25
@ddobrev ddobrev force-pushed the main branch 3 times, most recently from bcf41e4 to 851ec5e Compare October 2, 2021 15:17
@ddobrev ddobrev force-pushed the main branch 2 times, most recently from 8f64166 to 12f456e Compare October 27, 2021 22:32
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

Successfully merging this pull request may close these issues.

6 participants