Browse Source

Added support for read-only [] operators.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/61/head
Dimitar Dobrev 12 years ago
parent
commit
54c93f4d91
  1. 12
      src/AST/Property.cs
  2. 37
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 32
      src/Generator/Passes/CheckOperatorsOverloads.cs

12
src/AST/Property.cs

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
using System.Collections.Generic;
namespace CppSharp.AST
{
/// <summary>
@ -36,6 +38,16 @@ namespace CppSharp.AST @@ -36,6 +38,16 @@ namespace CppSharp.AST
// The field that should be get and set by this property
public Field Field { get; set; }
private readonly List<Parameter> parameters = new List<Parameter>();
/// <summary>
/// Only applicable to index ([]) properties.
/// </summary>
public List<Parameter> Parameters
{
get { return parameters; }
}
public override T Visit<T>(IDeclVisitor<T> visitor)
{
return visitor.VisitProperty(this);

37
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -893,14 +893,12 @@ namespace CppSharp.Generators.CSharp @@ -893,14 +893,12 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassProperties(Class @class)
{
foreach (var prop in @class.Properties)
foreach (var prop in @class.Properties.Where(p => !p.Ignore))
{
if (prop.Ignore) continue;
PushBlock(CSharpBlockKind.Property);
WriteLine("{0} {1} {2}",
prop.Access == AccessSpecifier.Public ? "public" : "protected",
prop.Type, SafeIdentifier(prop.Name));
prop.Type, GetPropertyName(prop));
WriteStartBraceIndent();
if (prop.Field != null)
@ -925,6 +923,12 @@ namespace CppSharp.Generators.CSharp @@ -925,6 +923,12 @@ namespace CppSharp.Generators.CSharp
}
}
private string GetPropertyName(Property prop)
{
return prop.Parameters.Count == 0 ? SafeIdentifier(prop.Name)
: string.Format("this[{0}]", FormatMethodParameters(prop.Parameters));
}
private void GenerateVariable(Class @class, Type type, Variable variable)
{
PushBlock(CSharpBlockKind.Variable);
@ -1469,7 +1473,7 @@ namespace CppSharp.Generators.CSharp @@ -1469,7 +1473,7 @@ namespace CppSharp.Generators.CSharp
var functionName = GetFunctionIdentifier(function);
Write("public static {0} {1}(", function.OriginalReturnType, functionName);
GenerateMethodParameters(function);
Write(FormatMethodParameters(function.Parameters));
WriteLine(")");
WriteStartBraceIndent();
@ -1517,7 +1521,7 @@ namespace CppSharp.Generators.CSharp @@ -1517,7 +1521,7 @@ namespace CppSharp.Generators.CSharp
else
Write("{0} {1}(", method.OriginalReturnType, functionName);
GenerateMethodParameters(method);
Write(FormatMethodParameters(method.Parameters));
Write(")");
@ -1938,25 +1942,16 @@ namespace CppSharp.Generators.CSharp @@ -1938,25 +1942,16 @@ namespace CppSharp.Generators.CSharp
}
}
private void GenerateMethodParameters(Function function)
{
var @params = new List<string>();
for (var i = 0; i < function.Parameters.Count; ++i)
private string FormatMethodParameters(IEnumerable<Parameter> @params)
{
var param = function.Parameters[i];
if (param.Kind == ParameterKind.IndirectReturnType)
continue;
var typeName = param.CSharpType(TypePrinter);
@params.Add(string.Format("{0}{1} {2}", GetParameterUsage(param.Usage),
return string.Join(", ",
from param in @params
where param.Kind != ParameterKind.IndirectReturnType
let typeName = param.CSharpType(this.TypePrinter)
select string.Format("{0}{1} {2}", GetParameterUsage(param.Usage),
typeName, SafeIdentifier(param.Name)));
}
Write(string.Join(", ", @params));
}
#endregion
public bool GenerateTypedef(TypedefDecl typedef)

32
src/Generator/Passes/CheckOperatorsOverloads.cs

@ -57,6 +57,12 @@ namespace CppSharp.Passes @@ -57,6 +57,12 @@ namespace CppSharp.Passes
if (@operator.SynthKind == FunctionSynthKind.NonMemberOperator)
continue;
if (@operator.OperatorKind == CXXOperatorKind.Subscript)
{
CreateIndexer(@class, @operator);
}
else
{
// Handle missing operator parameters
if (@operator.IsStatic)
@operator.Parameters = @operator.Parameters.Skip(1).ToList();
@ -75,6 +81,26 @@ namespace CppSharp.Passes @@ -75,6 +81,26 @@ namespace CppSharp.Passes
});
}
}
}
private static void CreateIndexer(Class @class, Method @operator)
{
if (@class.Properties.All(p => p.Parameters.Count == 0 ||
p.Parameters[0].QualifiedType != @operator.Parameters[0].QualifiedType))
{
Property property = new Property
{
Name = "Item",
QualifiedType = @operator.ReturnType,
Access = @operator.Access,
Namespace = @class
};
property.GetMethod = @operator;
property.Parameters.AddRange(@operator.Parameters);
@class.Properties.Add(property);
@operator.IsGenerated = false;
}
}
static void HandleMissingOperatorOverloadPair(Class @class, CXXOperatorKind op1,
CXXOperatorKind op2)
@ -153,6 +179,9 @@ namespace CppSharp.Passes @@ -153,6 +179,9 @@ namespace CppSharp.Passes
case CXXOperatorKind.Pipe:
case CXXOperatorKind.Caret:
// The array indexing operator can be overloaded
case CXXOperatorKind.Subscript:
// The comparison operators can be overloaded
case CXXOperatorKind.EqualEqual:
case CXXOperatorKind.ExclaimEqual:
@ -180,9 +209,6 @@ namespace CppSharp.Passes @@ -180,9 +209,6 @@ namespace CppSharp.Passes
case CXXOperatorKind.LessLessEqual:
case CXXOperatorKind.GreaterGreaterEqual:
// The array indexing operator cannot be overloaded
case CXXOperatorKind.Subscript:
// The conditional logical operators cannot be overloaded
case CXXOperatorKind.AmpAmp:
case CXXOperatorKind.PipePipe:

Loading…
Cancel
Save