Skip to content

Commit 4f5a911

Browse files
committed
Completed the type map of QString <-> string.
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 948ab98 commit 4f5a911

12 files changed

+412
-9
lines changed

CppSharp Qt plan.odt

659 Bytes
Binary file not shown.

QtSharp/Documentation.cs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Web.Util;
88
using CppSharp;
99
using CppSharp.AST;
10+
using CppSharp.Generators.CSharp;
1011
using CppSharp.Types;
1112
using Mono.Data.Sqlite;
1213
using zlib;
@@ -173,9 +174,13 @@ public void DocumentEnum(Enumeration @enum)
173174
}
174175
}
175176
}
177+
foreach (Enumeration.Item item in @enum.Items)
178+
{
179+
this.DocumentEnumItem(@enum, item);
180+
}
176181
}
177182

178-
public void DocumentEnumItem(Enumeration @enum, Enumeration.Item enumItem)
183+
private void DocumentEnumItem(Enumeration @enum, Enumeration.Item enumItem)
179184
{
180185
string file = GetFileForDeclarationContext(@enum.Namespace);
181186
if (this.documentation.ContainsKey(file))
@@ -521,7 +526,8 @@ private static void FillMissingParameterNames(Function function, string signatur
521526
string name = argNames[i];
522527
if (!string.IsNullOrEmpty(name))
523528
{
524-
parameter.Name = name + (index > 0 ? oldArgName.Substring(index) : string.Empty);
529+
name += (index > 0 ? oldArgName.Substring(index) : string.Empty);
530+
parameter.Name = Helpers.SafeIdentifier(name);
525531
}
526532
}
527533
}

QtSharp/DynamicQObject.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public override unsafe int Qt_metacall(QMetaObject.Call _1, int _2, void* _3)
5757
}
5858
else
5959
{
60-
value = Activator.CreateInstance(parameter.ParameterType, arg);
60+
if (parameter.ParameterType.IsAssignableFrom(typeof(string)))
61+
{
62+
// TODO: must properly handle QString here
63+
value = Marshal.PtrToStringUni(arg);
64+
}
65+
else
66+
{
67+
value = Activator.CreateInstance(parameter.ParameterType, arg);
68+
}
6169
}
6270
parameters[i] = value;
6371
}

QtSharp/GenerateSignalEventsPass.cs

+4
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ private static string GetSignalEventSuffix(Event signalToUse)
202202
else
203203
{
204204
StringBuilder lastParamBuilder = new StringBuilder(suffix);
205+
while (!char.IsLetter(lastParamBuilder[0]))
206+
{
207+
lastParamBuilder.Remove(0, 1);
208+
}
205209
lastParamBuilder[0] = char.ToUpper(lastParamBuilder[0]);
206210
suffix = lastParamBuilder.ToString();
207211
}

QtSharp/GetCommentsFromQtDocsPass.cs

-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ public override bool VisitEnumDecl(Enumeration @enum)
2828
if (@enum.Comment == null)
2929
{
3030
this.documentation.DocumentEnum(@enum);
31-
foreach (Enumeration.Item item in @enum.Items)
32-
{
33-
this.documentation.DocumentEnumItem(@enum, item);
34-
}
3531
}
3632
return base.VisitEnumDecl(@enum);
3733
}

QtSharp/MarshalQString.cs

+316
Large diffs are not rendered by default.

QtSharp/QList.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,22 @@ public override void CSharpMarshalToNative(MarshalContext ctx)
8585
}
8686
supportBefore.WriteLine("for (int i = 0; i < {0}.Count; i++)", ctx.Parameter.Name);
8787
supportBefore.WriteStartBraceIndent();
88-
supportBefore.WriteLine("{0}->array[i] = (void*) {1}[i]{2};", qListDataData, ctx.Parameter.Name, instance);
88+
Type desugared = ctx.Parameter.Type.Desugar();
89+
TemplateSpecializationType templateSpecializationType = desugared as TemplateSpecializationType;
90+
if (templateSpecializationType == null)
91+
{
92+
Type paramType;
93+
desugared.IsPointerTo(out paramType);
94+
templateSpecializationType = (TemplateSpecializationType) paramType.Desugar();
95+
}
96+
if (templateSpecializationType.Arguments[0].Type.ToString() == "string")
97+
{
98+
supportBefore.WriteLine("{0}->array[i] = Marshal.StringToHGlobalUni({1}[i]).ToPointer();", qListDataData, ctx.Parameter.Name, instance);
99+
}
100+
else
101+
{
102+
supportBefore.WriteLine("{0}->array[i] = (void*) {1}[i]{2};", qListDataData, ctx.Parameter.Name, instance);
103+
}
89104
supportBefore.WriteCloseBraceIndent();
90105
ctx.Return.Write(qList);
91106
}

QtSharp/QString.cs

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using CppSharp.AST;
2+
using CppSharp.Generators;
3+
using CppSharp.Generators.CSharp;
4+
using CppSharp.Types;
5+
6+
namespace QtSharp
7+
{
8+
[TypeMap("QString")]
9+
public class QString : TypeMap
10+
{
11+
public override string CSharpSignature(CSharpTypePrinterContext ctx)
12+
{
13+
if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native)
14+
{
15+
if (ctx.Type.IsPointer())
16+
{
17+
return "QString.Internal*";
18+
}
19+
return "QString.Internal";
20+
}
21+
return "string";
22+
}
23+
24+
public override void CSharpMarshalToNative(MarshalContext ctx)
25+
{
26+
ctx.SupportBefore.WriteLine("var __qstring{0} = QString.FromUtf16((ushort*) Marshal.StringToHGlobalUni({1}).ToPointer(), {1}.Length);",
27+
ctx.ParameterIndex, ctx.Parameter.Name);
28+
Type type = ctx.Parameter.Type.Desugar();
29+
if (type.IsAddress())
30+
{
31+
ctx.Return.Write("ReferenceEquals({0}, null) ? global::System.IntPtr.Zero : __qstring{1}.{2}",
32+
ctx.Parameter.Name, ctx.ParameterIndex, Helpers.InstanceIdentifier);
33+
return;
34+
}
35+
Class @class;
36+
type.IsTagDecl(out @class);
37+
if (@class == null)
38+
{
39+
Type.IsTagDecl(out @class);
40+
}
41+
var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(@class.OriginalClass ?? @class);
42+
ctx.Return.Write("ReferenceEquals(__qstring{0}, null) ? new {1}.Internal() : *({1}.Internal*) (__qstring{0}.{2})",
43+
ctx.ParameterIndex, qualifiedIdentifier, Helpers.InstanceIdentifier);
44+
}
45+
46+
public override void CSharpMarshalToManaged(MarshalContext ctx)
47+
{
48+
ctx.Return.Write("Marshal.PtrToStringUni(new IntPtr(new QString({0}).Utf16))", ctx.ReturnVarName);
49+
}
50+
}
51+
}

QtSharp/QtSharp.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void Postprocess(Driver driver, ASTContext lib)
126126
Class decl = null;
127127
Class qList = lib.FindClass("QList").First(c => !c.IsIncomplete && c.IsDependent);
128128
Field qListData = (from field in qList.Fields
129-
where field.Type.IsTagDecl(out decl) && string.IsNullOrEmpty(decl.Name)
129+
where field.Type.IsTagDecl(out decl) && decl.Name.All(c => !char.IsLetter(c))
130130
select field).First();
131131
foreach (Field field in decl.Fields.ToList())
132132
{
@@ -170,6 +170,7 @@ public void Setup(Driver driver)
170170
driver.Options.CodeFiles.Add(Path.Combine(dir, "QEventHandler.cs"));
171171
driver.Options.CodeFiles.Add(Path.Combine(dir, "DynamicQObject.cs"));
172172
driver.Options.CodeFiles.Add(Path.Combine(dir, "MarshalQList.cs"));
173+
driver.Options.CodeFiles.Add(Path.Combine(dir, "MarshalQString.cs"));
173174
}
174175
}
175176

QtSharp/QtSharp.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<Compile Include="GenerateEventEventsPass.cs" />
6666
<Compile Include="GenerateSignalEventsPass.cs" />
6767
<Compile Include="GetCommentsFromQtDocsPass.cs" />
68+
<None Include="MarshalQString.cs">
69+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
70+
</None>
6871
<Compile Include="Properties\AssemblyInfo.cs" />
6972
<Compile Include="ProcessHelper.cs" />
7073
<Compile Include="QBitArray.cs" />
@@ -77,6 +80,9 @@
7780
<Compile Include="QList.cs">
7881
<SubType>Code</SubType>
7982
</Compile>
83+
<Compile Include="QString.cs">
84+
<SubType>Code</SubType>
85+
</Compile>
8086
<Compile Include="QtSharp.cs" />
8187
<Compile Include="RemoveStaticsFromDerivedTypesPass.cs" />
8288
<None Include="QEventArgs.cs">

References/CppSharp.AST.dll

0 Bytes
Binary file not shown.

References/CppSharp.Generator.dll

1.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)