From d531e40e5fd8305baad88d7fdd68362d20d74e50 Mon Sep 17 00:00:00 2001 From: triton Date: Wed, 22 May 2013 16:27:22 +0100 Subject: [PATCH] Added better wrapping for out parameters. --- .../Generators/CSharp/CSharpTextTemplate.cs | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index f6bf0ed9..3fd48217 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -1146,42 +1146,52 @@ namespace CppSharp.Generators.CSharp if (param.Usage == ParameterUsage.Out) { - //var paramType = param.Type; - //if (paramType.IsReference()) - // paramType = (paramType as PointerType).Pointee; + var paramType = param.Type; - //var typePrinter = new CppTypePrinter(Driver.TypeDatabase); - //var type = paramType.Visit(typePrinter); - - //WriteLine("{0} {1};", type, argName); + Class @class; + if (paramType.Desugar().IsTagDecl(out @class) && @class.IsRefType) + { + WriteLine("{0} = new {1}();", param.Name, paramType); + } } - else + + var ctx = new CSharpMarshalContext(Driver) { - var ctx = new CSharpMarshalContext(Driver) - { - Parameter = param, - ParameterIndex = paramIndex, - ArgName = argName, - Function = function - }; + Parameter = param, + ParameterIndex = paramIndex, + ArgName = argName, + Function = function + }; - paramMarshal.Context = ctx; + paramMarshal.Context = ctx; - var marshal = new CSharpMarshalManagedToNativePrinter(ctx); - param.Visit(marshal); + var marshal = new CSharpMarshalManagedToNativePrinter(ctx); + param.Visit(marshal); - if (string.IsNullOrEmpty(marshal.Context.Return)) - throw new Exception("Cannot marshal argument of function"); + if (string.IsNullOrEmpty(marshal.Context.Return)) + throw new Exception("Cannot marshal argument of function"); - if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) - Write(marshal.Context.SupportBefore); + if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore)) + Write(marshal.Context.SupportBefore); - WriteLine("var {0} = {1};", SafeIdentifier(argName), marshal.Context.Return); - } + WriteLine("var {0} = {1};", SafeIdentifier(argName), marshal.Context.Return); return paramMarshal; } + static string GetParameterUsage(ParameterUsage usage) + { + switch (usage) + { + case ParameterUsage.Out: + return "out "; + case ParameterUsage.InOut: + return "ref"; + default: + return string.Empty; + } + } + private void GenerateMethodParameters(Method method, Class @class) { var @params = new List(); @@ -1193,7 +1203,8 @@ namespace CppSharp.Generators.CSharp if (param.Kind == ParameterKind.HiddenStructureReturn) continue; - @params.Add(string.Format("{0} {1}", param.Type, SafeIdentifier(param.Name))); + @params.Add(string.Format("{0}{1} {2}", GetParameterUsage(param.Usage), + param.Type, SafeIdentifier(param.Name))); } Write(string.Join(", ", @params));