Beispiel #1
0
 public virtual void CSharpMarshalToManaged(CSharpMarshalContext ctx)
 {
     ctx.Return.Write(ctx.ReturnVarName);
 }
Beispiel #2
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("({0} ? 1 : 0)", ctx.Parameter.Name);
 }
Beispiel #3
0
 public virtual void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     ctx.Return.Write(ctx.Parameter.Name);
 }
Beispiel #4
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("IntPtr.Zero");
 }
Beispiel #5
0
 public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("({0} != 0)", ctx.ReturnVarName);
 }
Beispiel #6
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     // pointless, put just so that the generated code compiles
     ctx.Return.Write("new QList.{0}()", Helpers.InternalStruct);
 }
Beispiel #7
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("new Std.WString()");
 }
Beispiel #8
0
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
        {
            string param = ctx.Parameter.Name;

            if (ctx.Parameter.Usage == ParameterUsage.Unknown &&
                !ctx.Parameter.Type.IsReference() &&
                !(ctx.Parameter.Type is TemplateParameterSubstitutionType) &&
                ctx.MarshalKind != MarshalKind.NativeField &&
                ctx.MarshalKind != MarshalKind.VTableReturnValue &&
                ctx.MarshalKind != MarshalKind.Variable)
            {
                ctx.Return.Write(param);
                return;
            }

            var substitution = Type as TemplateParameterSubstitutionType;

            if (substitution != null)
            {
                param = $"({substitution.Replacement}) (object) {param}";
            }

            // Allow setting native field to null via setter property.
            if (ctx.MarshalKind == MarshalKind.NativeField)
            {
                // Free memory if we're holding a pointer to unmanaged memory that we (think we)
                // allocated. We can't simply compare with IntPtr.Zero since the reference could be
                // owned by the native side.

                // TODO: Surely, we can do better than stripping out the name of the field using
                // string manipulation on the ReturnVarName, but I don't see it yet. Seems like it
                // would be really helpful to have ctx hold a Decl property representing the
                // "appropriate" Decl when we get here. When MarshalKind == NativeField, Decl would
                // be set to the Field we're operating on.
                var fieldName = ctx.ReturnVarName.Substring(ctx.ReturnVarName.LastIndexOf("->") + 2);

                ctx.Before.WriteLine($"if (__{fieldName}_OwnsNativeMemory)");
                ctx.Before.WriteLineIndent($"Marshal.FreeHGlobal({ctx.ReturnVarName});");
                ctx.Before.WriteLine($"__{fieldName}_OwnsNativeMemory = true;");
                ctx.Before.WriteLine($"if ({param} == null)");
                ctx.Before.WriteOpenBraceAndIndent();
                ctx.Before.WriteLine($"{ctx.ReturnVarName} = global::System.IntPtr.Zero;");
                ctx.Before.WriteLine("return;");
                ctx.Before.UnindentAndWriteCloseBrace();
            }

            var bytes        = $"__bytes{ctx.ParameterIndex}";
            var bytePtr      = $"__bytePtr{ctx.ParameterIndex}";
            var encodingName = GetEncoding().Name;

            switch (encodingName)
            {
            case nameof(Encoding.Unicode):
                ctx.Before.WriteLine($@"var {bytePtr} = Marshal.StringToHGlobalUni({param});");
                break;

            case nameof(Encoding.Default):
                ctx.Before.WriteLine($@"var {bytePtr} = Marshal.StringToHGlobalAnsi({param});");
                break;

            default:
            {
                var encodingBytesPerChar = GetCharWidth() / 8;
                var writeNulMethod       = encodingBytesPerChar switch
                {
                    1 => nameof(Marshal.WriteByte),
                    2 => nameof(Marshal.WriteInt16),
                    4 => nameof(Marshal.WriteInt32),
                    _ => throw new System.NotImplementedException(
                              $"Encoding bytes per char: {encodingBytesPerChar} is not implemented.")
                };

                ctx.Before.WriteLine($@"var {bytes} = global::System.Text.Encoding.{encodingName}.GetBytes({param});");
                ctx.Before.WriteLine($@"var {bytePtr} = Marshal.AllocHGlobal({bytes}.Length + {encodingBytesPerChar});");
                ctx.Before.WriteLine($"Marshal.Copy({bytes}, 0, {bytePtr}, {bytes}.Length);");
                ctx.Before.WriteLine($"Marshal.{writeNulMethod}({bytePtr} + {bytes}.Length, 0);");
            }
            break;
            }

            ctx.Return.Write($"{bytePtr}");
        }
Beispiel #9
0
 public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("Marshal.PtrToStringUni(new IntPtr(QtCore.QString.{0}({1}).Utf16))",
                      Helpers.CreateInstanceIdentifier, ctx.ReturnVarName);
 }
Beispiel #10
0
 public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
 {
     ctx.Return.Write("\"test\"");
 }
Beispiel #11
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     ctx.Return.Write(ctx.Parameter.Type.Desugar().IsAddress() ?
                      "global::System.IntPtr.Zero" : "\"test\"");
 }
Beispiel #12
0
 public virtual void CSharpMarshalToManaged(CSharpMarshalContext ctx)
 {
     throw new NotImplementedException();
 }