Browse Source

Added optional getter/setter pair creation for fields in FieldToProperty pass.

pull/1303/head
João Matos 6 years ago committed by João Matos
parent
commit
04247b6baf
  1. 57
      src/Generator/Passes/FieldToPropertyPass.cs

57
src/Generator/Passes/FieldToPropertyPass.cs

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
@ -34,11 +35,11 @@ namespace CppSharp.Passes @@ -34,11 +35,11 @@ namespace CppSharp.Passes
if (!VisitDeclaration(field))
return false;
var @class = field.Namespace as Class;
if (@class == null)
if (ASTUtils.CheckIgnoreField(field))
return false;
if (ASTUtils.CheckIgnoreField(field))
var @class = field.Namespace as Class;
if (@class == null)
return false;
// Check if we already have a synthetized property.
@ -54,9 +55,13 @@ namespace CppSharp.Passes @@ -54,9 +55,13 @@ namespace CppSharp.Passes
Namespace = field.Namespace,
QualifiedType = field.QualifiedType,
Access = field.Access,
Field = field
Field = field,
AssociatedDeclaration = field
};
if (Options.GeneratorKind == GeneratorKind.CPlusPlus)
GenerateAcessorMethods(field, prop);
// do not rename value-class fields because they would be
// generated as fields later on even though they are wrapped by properties;
// that is, in turn, because it's cleaner to write
@ -70,5 +75,47 @@ namespace CppSharp.Passes @@ -70,5 +75,47 @@ namespace CppSharp.Passes
return false;
}
private void GenerateAcessorMethods(Field field, Property property)
{
var @class = field.Namespace as Class;
var getter = new Method
{
Name = $"get_{field.Name}",
Namespace = @class,
ReturnType = field.QualifiedType,
Access = field.Access,
AssociatedDeclaration = property,
IsStatic = field.IsStatic,
SynthKind = FunctionSynthKind.FieldAcessor
};
var setter = new Method
{
Name = $"set_{field.Name}",
Namespace = @class,
ReturnType = new QualifiedType(new BuiltinType(PrimitiveType.Void)),
Access = field.Access,
AssociatedDeclaration = property,
IsStatic = field.IsStatic,
SynthKind = FunctionSynthKind.FieldAcessor
};
var param = new Parameter
{
Name = "value",
QualifiedType = field.QualifiedType,
Namespace = setter
};
setter.Parameters.Add(param);
property.GetMethod = getter;
property.SetMethod = setter;
@class.Methods.Add(getter);
@class.Methods.Add(setter);
}
}
}

Loading…
Cancel
Save