Browse Source

Factor VarPrefix and ArgumentPrefix helpers into base marshaler.

pull/1547/head
Joao Matos 5 years ago committed by João Matos
parent
commit
c4f96ab1b9
  1. 10
      src/Generator/Generators/C/CppMarshal.cs
  2. 4
      src/Generator/Generators/C/CppSources.cs
  3. 6
      src/Generator/Generators/CLI/CLIMarshal.cs
  4. 2
      src/Generator/Generators/CLI/CLISources.cs
  5. 4
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  6. 8
      src/Generator/Generators/Marshal.cs

10
src/Generator/Generators/C/CppMarshal.cs

@ -333,15 +333,9 @@ namespace CppSharp.Generators.Cpp @@ -333,15 +333,9 @@ namespace CppSharp.Generators.Cpp
public class CppMarshalManagedToNativePrinter : MarshalPrinter<MarshalContext>
{
public readonly TextGenerator VarPrefix;
public readonly TextGenerator ArgumentPrefix;
public CppMarshalManagedToNativePrinter(MarshalContext ctx)
: base(ctx)
{
VarPrefix = new TextGenerator();
ArgumentPrefix = new TextGenerator();
Context.MarshalToNative = this;
}
@ -414,7 +408,7 @@ namespace CppSharp.Generators.Cpp @@ -414,7 +408,7 @@ namespace CppSharp.Generators.Cpp
var isRef = Context.Parameter.Usage == ParameterUsage.Out ||
Context.Parameter.Usage == ParameterUsage.InOut;
ArgumentPrefix.Write("&");
Context.ArgumentPrefix.Write("&");
Context.Return.Write($"(::{@enum.QualifiedOriginalName}){0}{Context.Parameter.Name}",
isRef ? string.Empty : "*");
return true;
@ -586,7 +580,7 @@ namespace CppSharp.Generators.Cpp @@ -586,7 +580,7 @@ namespace CppSharp.Generators.Cpp
Context.Return.Write("*");
if (Context.Parameter.Type.IsReference())
VarPrefix.Write("&");
Context.VarPrefix.Write("&");
}
var method = Context.Function as Method;

4
src/Generator/Generators/C/CppSources.cs

@ -658,8 +658,8 @@ namespace CppSharp.Generators.Cpp @@ -658,8 +658,8 @@ namespace CppSharp.Generators.Cpp
if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
Write(marshal.Context.Before);
WriteLine($"auto {marshal.VarPrefix}{argName} = {marshal.Context.Return};");
paramMarshal.Prefix = marshal.ArgumentPrefix;
WriteLine($"auto {marshal.Context.VarPrefix}{argName} = {marshal.Context.Return};");
paramMarshal.Prefix = marshal.Context.ArgumentPrefix;
}
paramMarshal.Name = argName;

6
src/Generator/Generators/CLI/CLIMarshal.cs

@ -738,7 +738,7 @@ namespace CppSharp.Generators.CLI @@ -738,7 +738,7 @@ namespace CppSharp.Generators.CLI
public void MarshalValueClass(Class @class)
{
var marshalVar = Context.MarshalVarPrefix + "_marshal" +
var marshalVar = Context.VarPrefix + "_marshal" +
Context.ParameterIndex++;
Context.Before.WriteLine("auto {0} = ::{1}();", marshalVar,
@ -774,14 +774,12 @@ namespace CppSharp.Generators.CLI @@ -774,14 +774,12 @@ namespace CppSharp.Generators.CLI
private void MarshalValueClassProperty(Property property, string marshalVar)
{
var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
property.Name);
var fieldRef = $"{Context.Parameter.Name}.{property.Name}";
var marshalCtx = new MarshalContext(Context.Context, Context.Indentation)
{
ArgName = fieldRef,
ParameterIndex = Context.ParameterIndex++,
MarshalVarPrefix = Context.MarshalVarPrefix,
ReturnVarName = $"{marshalVar}.{property.Field.OriginalName}"
};

2
src/Generator/Generators/CLI/CLISources.cs

@ -968,9 +968,9 @@ namespace CppSharp.Generators.CLI @@ -968,9 +968,9 @@ namespace CppSharp.Generators.CLI
var param = new Parameter { Name = "(*this)" , Namespace = function.Namespace };
var ctx = new MarshalContext(Context, CurrentIndentation)
{
MarshalVarPrefix = valueMarshalName,
Parameter = param
};
ctx.VarPrefix.Write(valueMarshalName);
var marshal = new CLIMarshalManagedToNativePrinter(ctx);
marshal.MarshalValueClassProperties(@class, valueMarshalName);

4
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -12,12 +12,8 @@ namespace CppSharp.Generators.CSharp @@ -12,12 +12,8 @@ namespace CppSharp.Generators.CSharp
public CSharpMarshalContext(BindingContext context, uint indentation)
: base(context, indentation)
{
ArgumentPrefix = new TextGenerator { CurrentIndentation = indentation };
Cleanup = new TextGenerator { CurrentIndentation = indentation };
}
public TextGenerator ArgumentPrefix { get; }
public TextGenerator Cleanup { get; }
public bool HasCodeBlock { get; set; }
}

8
src/Generator/Generators/Marshal.cs

@ -9,7 +9,9 @@ namespace CppSharp.Generators @@ -9,7 +9,9 @@ namespace CppSharp.Generators
Context = context;
Before = new TextGenerator { CurrentIndentation = indentation };
Return = new TextGenerator { CurrentIndentation = indentation };
MarshalVarPrefix = string.Empty;
Cleanup = new TextGenerator { CurrentIndentation = indentation };
VarPrefix = new TextGenerator();
ArgumentPrefix = new TextGenerator();
this.Indentation = indentation;
}
@ -19,6 +21,9 @@ namespace CppSharp.Generators @@ -19,6 +21,9 @@ namespace CppSharp.Generators
public TextGenerator Before { get; }
public TextGenerator Return { get; }
public TextGenerator Cleanup { get; }
public TextGenerator VarPrefix;
public TextGenerator ArgumentPrefix;
public string ReturnVarName { get; set; }
public QualifiedType ReturnType { get; set; }
@ -27,7 +32,6 @@ namespace CppSharp.Generators @@ -27,7 +32,6 @@ namespace CppSharp.Generators
public int ParameterIndex { get; set; }
public Function Function { get; set; }
public string MarshalVarPrefix { get; set; }
public uint Indentation { get; }
}

Loading…
Cancel
Save