Browse Source

Merge pull request #275 from ddobrev/master

Improved the pass for generating properties
pull/277/head
João Matos 11 years ago
parent
commit
4146736222
  1. 104
      src/Generator/Passes/GetterSetterToPropertyAdvancedPass.cs

104
src/Generator/Passes/GetterSetterToPropertyAdvancedPass.cs

@ -13,66 +13,18 @@ namespace CppSharp.Passes
{ {
public class GetterSetterToPropertyAdvancedPass : TranslationUnitPass public class GetterSetterToPropertyAdvancedPass : TranslationUnitPass
{ {
// collect all types of methods first to be able to match pairs and detect virtuals and overrides; private class PropertyGenerator
// (a property needs to) be virtual or an override if either of its constituent methods are such) {
private readonly List<Method> getters = new List<Method>();
private readonly List<Method> setters = new List<Method>(); private readonly List<Method> setters = new List<Method>();
private readonly List<Method> setMethods = new List<Method>(); private readonly List<Method> setMethods = new List<Method>();
private readonly List<Method> nonSetters = new List<Method>(); private readonly List<Method> nonSetters = new List<Method>();
private readonly HashSet<Method> getters = new HashSet<Method>();
private static readonly HashSet<string> verbs = new HashSet<string>();
static GetterSetterToPropertyAdvancedPass()
{
LoadVerbs();
}
static Stream GetResourceStream (Assembly assembly) public PropertyGenerator(Class @class)
{ {
var stream = assembly.GetManifestResourceStream("CppSharp.Generator.Passes.verbs.txt"); foreach (var method in @class.Methods.Where(
if (stream != null) m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && !m.Ignore))
return stream;
stream = assembly.GetManifestResourceStream("verbs.txt");
return stream;
}
private static void LoadVerbs()
{
var assembly = Assembly.GetExecutingAssembly();
using (var resourceStream = GetResourceStream(assembly))
{
using (var streamReader = new StreamReader(resourceStream))
while (!streamReader.EndOfStream)
verbs.Add(streamReader.ReadLine());
}
}
public GetterSetterToPropertyAdvancedPass()
{
Options.VisitClassFields = false;
Options.VisitClassProperties = false;
Options.VisitNamespaceEnums = false;
Options.VisitNamespaceTemplates = false;
Options.VisitNamespaceTypedefs = false;
Options.VisitNamespaceEvents = false;
Options.VisitNamespaceVariables = false;
Options.VisitFunctionParameters = false;
Options.VisitTemplateArguments = false;
}
public override bool VisitTranslationUnit(TranslationUnit unit)
{
bool result = base.VisitTranslationUnit(unit);
GenerateProperties();
return result;
}
public override bool VisitMethodDecl(Method method)
{
if (!method.IsConstructor && !method.IsDestructor && !method.IsOperator &&
method.IsGenerated && !method.IsSynthetized)
DistributeMethod(method); DistributeMethod(method);
return base.VisitMethodDecl(method);
} }
public void GenerateProperties() public void GenerateProperties()
@ -93,9 +45,7 @@ namespace CppSharp.Passes
private void GenerateProperties(IEnumerable<Method> settersToUse, bool readOnly) private void GenerateProperties(IEnumerable<Method> settersToUse, bool readOnly)
{ {
foreach (var group in settersToUse.GroupBy(m => m.Namespace)) foreach (var setter in settersToUse)
{
foreach (var setter in group)
{ {
Class type = (Class) setter.Namespace; Class type = (Class) setter.Namespace;
StringBuilder nameBuilder = new StringBuilder(setter.Name.Substring(3)); StringBuilder nameBuilder = new StringBuilder(setter.Name.Substring(3));
@ -125,7 +75,6 @@ namespace CppSharp.Passes
next: next:
; ;
} }
}
foreach (Method nonSetter in nonSetters) foreach (Method nonSetter in nonSetters)
{ {
Class type = (Class) nonSetter.Namespace; Class type = (Class) nonSetter.Namespace;
@ -205,9 +154,9 @@ namespace CppSharp.Passes
property.Comment = comment; property.Comment = comment;
} }
type.Properties.Add(property); type.Properties.Add(property);
getter.GenerationKind = GenerationKind.Internal; getter.GenerationKind = GenerationKind.None;
if (setter != null) if (setter != null)
setter.GenerationKind = GenerationKind.Internal; setter.GenerationKind = GenerationKind.None;
} }
} }
@ -250,7 +199,7 @@ namespace CppSharp.Passes
} }
} }
private bool IsGetter(Method method) private static bool IsGetter(Method method)
{ {
if (method.IsDestructor || if (method.IsDestructor ||
(method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) || (method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void)) ||
@ -273,4 +222,37 @@ namespace CppSharp.Passes
return new string(firstVerb.ToArray()); return new string(firstVerb.ToArray());
} }
} }
private static readonly HashSet<string> verbs = new HashSet<string>();
static GetterSetterToPropertyAdvancedPass()
{
LoadVerbs();
}
private static void LoadVerbs()
{
using (var resourceStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("CppSharp.Generator.Passes.verbs.txt"))
{
using (StreamReader streamReader = new StreamReader(resourceStream))
while (!streamReader.EndOfStream)
verbs.Add(streamReader.ReadLine());
}
}
public GetterSetterToPropertyAdvancedPass()
{
Options.VisitClassProperties = false;
}
public override bool VisitClassDecl(Class @class)
{
bool result = base.VisitClassDecl(@class);
new PropertyGenerator(@class).GenerateProperties();
return result;
}
}
} }

Loading…
Cancel
Save