Browse Source

Reformatted the extension methods for classes.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/256/merge
Dimitar Dobrev 10 years ago
parent
commit
39a368894d
  1. 93
      src/AST/ClassExtensions.cs

93
src/AST/ClassExtensions.cs

@ -7,38 +7,43 @@ namespace CppSharp.AST
{ {
public static class ClassExtensions public static class ClassExtensions
{ {
public static IEnumerable<Method> FindOperator(this Class c, CXXOperatorKind kind) public static IEnumerable<Method> FindOperator(this Class @class,
CXXOperatorKind kind)
{ {
return c.Operators.Where(method => method.OperatorKind == kind); return @class.Operators.Where(method => method.OperatorKind == kind);
} }
public static IEnumerable<Method> FindMethodByOriginalName(this Class c, string name) public static IEnumerable<Method> FindMethodByOriginalName(this Class @class,
string name)
{ {
return c.Methods.Where(method => method.OriginalName == name); return @class.Methods.Where(method => method.OriginalName == name);
} }
public static IEnumerable<Variable> FindVariableByOriginalName(this Class c, string originalName) public static IEnumerable<Variable> FindVariableByOriginalName(this Class @class,
string originalName)
{ {
return c.Variables.Where(v => v.OriginalName == originalName); return @class.Variables.Where(v => v.OriginalName == originalName);
} }
public static IEnumerable<Function> GetFunctionOverloads(this Class c, Function function) public static IEnumerable<Function> GetFunctionOverloads(this Class @class,
Function function)
{ {
if (function.IsOperator) if (function.IsOperator)
return c.FindOperator(function.OperatorKind); return @class.FindOperator(function.OperatorKind);
return c.Methods.Where(method => method.Name == function.Name); return @class.Methods.Where(method => method.Name == function.Name);
} }
public static IEnumerable<T> FindHierarchy<T>(this Class c, Func<Class, IEnumerable<T>> func) public static IEnumerable<T> FindHierarchy<T>(this Class @class,
Func<Class, IEnumerable<T>> func)
where T : Declaration where T : Declaration
{ {
foreach (var elem in func(c)) foreach (var elem in func(@class))
yield return elem; yield return elem;
foreach (var @base in c.Bases) foreach (var @base in @class.Bases)
{ {
if (!@base.IsClass) continue; if (!@base.IsClass) continue;
foreach(var elem in @base.Class.FindHierarchy<T>(func)) foreach(var elem in @base.Class.FindHierarchy(func))
yield return elem; yield return elem;
} }
} }
@ -54,25 +59,25 @@ namespace CppSharp.AST
} }
} }
public static Method GetBaseMethod(this Class c, Method @override, bool onlyPrimaryBase = false, bool getTopmost = false) public static Method GetBaseMethod(this Class @class, Method @override,
bool onlyPrimaryBase = false, bool getTopmost = false)
{ {
foreach (var @base in c.Bases) var parameterTypeComparer = new ParameterTypeComparer();
foreach (var @base in @class.Bases.Where(
b => b.IsClass && b.Class.OriginalClass != @class && (!onlyPrimaryBase || !b.Class.IsInterface)))
{ {
if (!@base.IsClass || @base.Class.OriginalClass == c || (onlyPrimaryBase && @base.Class.IsInterface))
continue;
Method baseMethod; Method baseMethod;
if (!getTopmost) if (!getTopmost)
{ {
baseMethod = (@base.Class.GetBaseMethod(@override, onlyPrimaryBase)); baseMethod = @base.Class.GetBaseMethod(@override, onlyPrimaryBase);
if (baseMethod != null) if (baseMethod != null)
return baseMethod; return baseMethod;
} }
baseMethod = (from method in @base.Class.Methods baseMethod = (from method in @base.Class.Methods
where where (method.OriginalName == @override.OriginalName &&
(method.OriginalName == @override.OriginalName && method.ReturnType == @override.ReturnType && method.ReturnType == @override.ReturnType &&
method.Parameters.SequenceEqual(@override.Parameters, new ParameterTypeComparer())) || method.Parameters.SequenceEqual(@override.Parameters, parameterTypeComparer)) ||
(@override.IsDestructor && method.IsDestructor && method.IsVirtual) (@override.IsDestructor && method.IsDestructor && method.IsVirtual)
select method).FirstOrDefault(); select method).FirstOrDefault();
if (baseMethod != null) if (baseMethod != null)
@ -94,11 +99,14 @@ namespace CppSharp.AST
return baseMethod != null && !baseMethod.IsPure; return baseMethod != null && !baseMethod.IsPure;
} }
public static Property GetBaseProperty(this Class c, Property @override, bool onlyFirstBase = false, bool getTopmost = false) public static Property GetBaseProperty(this Class @class, Property @override,
bool onlyFirstBase = false, bool getTopmost = false)
{ {
foreach (var @base in c.Bases) var parameterTypeComparer = new ParameterTypeComparer();
foreach (var @base in @class.Bases)
{ {
if (!@base.IsClass || @base.Class.OriginalClass == c || (onlyFirstBase && @base.Class.IsInterface)) if (!@base.IsClass || @base.Class.OriginalClass == @class ||
(onlyFirstBase && @base.Class.IsInterface))
continue; continue;
Property baseProperty; Property baseProperty;
@ -110,9 +118,8 @@ namespace CppSharp.AST
} }
baseProperty = (from property in @base.Class.Properties baseProperty = (from property in @base.Class.Properties
where where property.OriginalName == @override.OriginalName &&
property.OriginalName == @override.OriginalName && property.Parameters.SequenceEqual(@override.Parameters, parameterTypeComparer)
property.Parameters.SequenceEqual(@override.Parameters, new ParameterTypeComparer())
select property).FirstOrDefault(); select property).FirstOrDefault();
if (baseProperty != null) if (baseProperty != null)
return baseProperty; return baseProperty;
@ -133,13 +140,13 @@ namespace CppSharp.AST
return baseProperty != null && !baseProperty.IsPure; return baseProperty != null && !baseProperty.IsPure;
} }
public static Property GetPropertyByName(this Class c, string propertyName) public static Property GetPropertyByName(this Class @class, string propertyName)
{ {
Property property = c.Properties.FirstOrDefault(m => m.Name == propertyName); Property property = @class.Properties.FirstOrDefault(m => m.Name == propertyName);
if (property != null) if (property != null)
return property; return property;
foreach (var baseClassSpecifier in c.Bases.Where( foreach (var baseClassSpecifier in @class.Bases.Where(
b => b.Type.IsClass() && b.Class.IsDeclared)) b => b.Type.IsClass() && b.Class.IsDeclared))
{ {
property = baseClassSpecifier.Class.GetPropertyByName(propertyName); property = baseClassSpecifier.Class.GetPropertyByName(propertyName);
@ -149,16 +156,16 @@ namespace CppSharp.AST
return null; return null;
} }
public static Property GetPropertyByConstituentMethod(this Class c, Method method) public static Property GetPropertyByConstituentMethod(this Class @class, Method method)
{ {
var property = c.Properties.FirstOrDefault(p => p.GetMethod == method); var property = @class.Properties.FirstOrDefault(p => p.GetMethod == method);
if (property != null) if (property != null)
return property; return property;
property = c.Properties.FirstOrDefault(p => p.SetMethod == method); property = @class.Properties.FirstOrDefault(p => p.SetMethod == method);
if (property != null) if (property != null)
return property; return property;
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsClass())) foreach (BaseClassSpecifier @base in @class.Bases.Where(b => b.Type.IsClass()))
{ {
property = @base.Class.GetPropertyByConstituentMethod(method); property = @base.Class.GetPropertyByConstituentMethod(method);
if (property != null) if (property != null)
@ -167,13 +174,13 @@ namespace CppSharp.AST
return null; return null;
} }
public static Method GetMethodByName(this Class c, string methodName) public static Method GetMethodByName(this Class @class, string methodName)
{ {
var method = c.Methods.FirstOrDefault(m => m.Name == methodName); var method = @class.Methods.FirstOrDefault(m => m.Name == methodName);
if (method != null) if (method != null)
return method; return method;
foreach (BaseClassSpecifier @base in c.Bases.Where(b => b.Type.IsClass())) foreach (var @base in @class.Bases.Where(b => b.Type.IsClass()))
{ {
method = @base.Class.GetMethodByName(methodName); method = @base.Class.GetMethodByName(methodName);
if (method != null) if (method != null)
@ -184,18 +191,18 @@ namespace CppSharp.AST
public static bool HasRefBase(this Class @class) public static bool HasRefBase(this Class @class)
{ {
Class baseClass = null; Class @base = null;
if (@class.HasBaseClass) if (@class.HasBaseClass)
baseClass = @class.Bases[0].Class; @base = @class.Bases[0].Class;
var hasRefBase = baseClass != null && baseClass.IsRefType var hasRefBase = @base != null && @base.IsRefType && @base.IsDeclared;
&& baseClass.IsDeclared;
return hasRefBase; return hasRefBase;
} }
private static bool ComputeClassPath(this Class current, Class target, IList<BaseClassSpecifier> path) private static bool ComputeClassPath(this Class current, Class target,
IList<BaseClassSpecifier> path)
{ {
if (target == current) if (target == current)
return true; return true;

Loading…
Cancel
Save