Browse Source

Add `UsePropertyDetectionHeuristics` option to `DriverOptions`.

pull/1028/head
Rokas Kupstys 8 years ago
parent
commit
804f69498c
  1. 2
      src/Generator/Options.cs
  2. 19
      src/Generator/Passes/GetterSetterToPropertyPass.cs

2
src/Generator/Options.cs

@ -162,6 +162,8 @@ namespace CppSharp @@ -162,6 +162,8 @@ namespace CppSharp
/// </summary>
public HashSet<string> ExplicitlyPatchedVirtualFunctions { get; }
public bool UsePropertyDetectionHeuristics { get; set; } = true;
#endregion
}

19
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -19,9 +19,11 @@ namespace CppSharp.Passes @@ -19,9 +19,11 @@ namespace CppSharp.Passes
private readonly List<Method> setters = new List<Method>();
private readonly List<Method> setMethods = new List<Method>();
private readonly List<Method> nonSetters = new List<Method>();
private bool useHeuristics = true;
public PropertyGenerator(Class @class)
public PropertyGenerator(Class @class, bool useHeuristics)
{
this.useHeuristics = useHeuristics;
foreach (var method in @class.Methods.Where(
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated))
DistributeMethod(method);
@ -266,16 +268,21 @@ namespace CppSharp.Passes @@ -266,16 +268,21 @@ namespace CppSharp.Passes
}
}
private static bool IsGetter(Method method)
private bool IsGetter(Method method)
{
if (method.IsDestructor ||
(method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) ||
method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType))
return false;
var firstWord = GetFirstWord(method.Name);
return (firstWord.Length < method.Name.Length &&
Match(firstWord, new[] { "get", "is", "has" })) ||
(!Match(firstWord, new[] { "to", "new" }) && !verbs.Contains(firstWord));
if (firstWord.Length < method.Name.Length && Match(firstWord, new[] {"get", "is", "has"}))
return true;
if (useHeuristics && !Match(firstWord, new[] {"to", "new"}) && !verbs.Contains(firstWord))
return true;
return false;
}
private static bool Match(string prefix, IEnumerable<string> prefixes)
@ -354,7 +361,7 @@ namespace CppSharp.Passes @@ -354,7 +361,7 @@ namespace CppSharp.Passes
public override bool VisitClassDecl(Class @class)
{
if (base.VisitClassDecl(@class))
new PropertyGenerator(@class).GenerateProperties();
new PropertyGenerator(@class, Options.UsePropertyDetectionHeuristics).GenerateProperties();
return false;
}
}

Loading…
Cancel
Save