Skip to content

Commit a03a0ea

Browse files
committed
Add IgnoreConversionToProperty(pattern) and ForceConversionToProperty(pattern).
1 parent 804f694 commit a03a0ea

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

src/AST/Method.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public Method(Method method)
105105
SynthKind = method.SynthKind;
106106
AdjustedOffset = method.AdjustedOffset;
107107
OverriddenMethods.AddRange(method.OverriddenMethods);
108+
ConvertToProperty = method.ConvertToProperty;
108109
}
109110

110111
public Method(Function function)
@@ -172,6 +173,8 @@ public bool IsDestructor
172173

173174
public List<Method> OverriddenMethods { get; } = new List<Method>();
174175

176+
public bool ConvertToProperty { get; set; }
177+
175178
public Method GetRootBaseMethod()
176179
{
177180
return BaseMethod == null || BaseMethod.BaseMethod == null ?
@@ -185,4 +188,4 @@ public override T Visit<T>(IDeclVisitor<T> visitor)
185188

186189
private bool? isOverride;
187190
}
188-
}
191+
}

src/Generator/Library.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text.RegularExpressions;
66
using CppSharp.AST;
7+
using CppSharp.Passes;
78

89
namespace CppSharp
910
{
@@ -351,6 +352,53 @@ public static void IgnoreClassField(this ASTContext context, string name, string
351352
}
352353
}
353354

355+
private static IEnumerable<Class> GetClasses(DeclarationContext decl)
356+
{
357+
foreach (var @class in decl.Classes)
358+
{
359+
yield return @class;
360+
361+
foreach (var class2 in GetClasses(@class))
362+
yield return class2;
363+
}
364+
365+
foreach (var ns in decl.Namespaces)
366+
{
367+
foreach (var @class in GetClasses(ns))
368+
yield return @class;
369+
}
370+
}
371+
372+
public static void IgnoreConversionToProperty(this ASTContext context, string pattern)
373+
{
374+
foreach (var unit in context.TranslationUnits)
375+
{
376+
foreach (var @class in GetClasses(unit))
377+
{
378+
foreach (var method in @class.Methods)
379+
{
380+
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
381+
method.ExcludeFromPasses.Add(typeof(GetterSetterToPropertyPass));
382+
}
383+
}
384+
}
385+
}
386+
387+
public static void ForceConversionToProperty(this ASTContext context, string pattern)
388+
{
389+
foreach (var unit in context.TranslationUnits)
390+
{
391+
foreach (var @class in GetClasses(unit))
392+
{
393+
foreach (var method in @class.Methods)
394+
{
395+
if (Regex.Match(method.QualifiedLogicalOriginalName, pattern).Success)
396+
method.ConvertToProperty = true;
397+
}
398+
}
399+
}
400+
}
401+
354402
#endregion
355403

356404
#region Module Helpers
@@ -379,4 +427,4 @@ public static void IgnoreHeadersWithName(this ASTContext context, string pattern
379427

380428
#endregion
381429
}
382-
}
430+
}

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public PropertyGenerator(Class @class, bool useHeuristics)
2525
{
2626
this.useHeuristics = useHeuristics;
2727
foreach (var method in @class.Methods.Where(
28-
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated))
28+
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
29+
!m.ExcludeFromPasses.Contains(typeof(GetterSetterToPropertyPass))))
2930
DistributeMethod(method);
3031
}
3132

@@ -261,7 +262,7 @@ private void DistributeMethod(Method method)
261262
}
262263
else
263264
{
264-
if (IsGetter(method))
265+
if (method.ConvertToProperty || IsGetter(method))
265266
getters.Add(method);
266267
if (method.Parameters.All(p => p.Kind == ParameterKind.IndirectReturnType))
267268
nonSetters.Add(method);

0 commit comments

Comments
 (0)