Browse Source

Added a new pass converting all parameter types to their respective interfaces, if any.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/68/head
Dimitar Dobrev 12 years ago
parent
commit
3a9eef1e80
  1. 3
      src/Generator/Driver.cs
  2. 6
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  3. 2
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 12
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  5. 11
      src/Generator/Passes/MultipleInheritancePass.cs
  6. 29
      src/Generator/Passes/ParamTypeToInterfacePass.cs
  7. 1
      tests/CSharpTemp/CSharpTemp.Tests.cs
  8. 7
      tests/CSharpTemp/CSharpTemp.cpp
  9. 5
      tests/CSharpTemp/CSharpTemp.h

3
src/Generator/Driver.cs

@ -154,7 +154,10 @@ namespace CppSharp @@ -154,7 +154,10 @@ namespace CppSharp
if (Options.GenerateAbstractImpls)
TranslationUnitPasses.AddPass(new GenerateAbstractImplementationsPass());
if (Options.GenerateInterfacesForMultipleInheritance)
{
TranslationUnitPasses.AddPass(new MultipleInheritancePass());
TranslationUnitPasses.AddPass(new ParamTypeToInterfacePass());
}
}
public void ProcessCode()

6
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -541,9 +541,11 @@ namespace CppSharp.Generators.CSharp @@ -541,9 +541,11 @@ namespace CppSharp.Generators.CSharp
return;
}
Context.Return.Write("*({0}.Internal*){1}.{2}", CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(@class),
var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(@class);
if (@class.IsInterface)
qualifiedIdentifier = qualifiedIdentifier.Substring(1);
Context.Return.Write("*({0}.Internal*){1}.{2}", qualifiedIdentifier,
Helpers.SafeIdentifier(Context.Parameter.Name), Helpers.InstanceIdentifier);
}
private void MarshalValueClass(Class @class)

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

@ -2067,7 +2067,7 @@ namespace CppSharp.Generators.CSharp @@ -2067,7 +2067,7 @@ namespace CppSharp.Generators.CSharp
return string.Join(", ",
from param in @params
where param.Kind != ParameterKind.IndirectReturnType
let typeName = param.CSharpType(this.TypePrinter)
let typeName = param.CSharpType(TypePrinter)
select string.Format("{0}{1} {2}", GetParameterUsage(param.Usage),
typeName, SafeIdentifier(param.Name)));
}

12
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -349,10 +349,14 @@ namespace CppSharp.Generators.CSharp @@ -349,10 +349,14 @@ namespace CppSharp.Generators.CSharp
public CSharpTypePrinterResult VisitClassDecl(Class @class)
{
var nestedName = GetNestedQualifiedName(@class);
if (ContextKind == CSharpTypePrinterContextKind.Native)
return string.Format("{0}.Internal", nestedName);
var nestedName = GetNestedQualifiedName(@class);
if (ContextKind == CSharpTypePrinterContextKind.Native)
{
if (@class.IsInterface)
nestedName = nestedName.Substring(1);
return string.Format("{0}.Internal", nestedName);
}
return nestedName;
}

11
src/Generator/Passes/MultipleInheritancePass.cs

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators.CSharp;
namespace CppSharp.Passes
{
@ -61,6 +62,14 @@ namespace CppSharp.Passes @@ -61,6 +62,14 @@ namespace CppSharp.Passes
@interface.Methods.AddRange(@base.Methods.Where(
m => !m.IsConstructor && !m.IsDestructor && !m.IsStatic && !m.Ignore));
@interface.Properties.AddRange(@base.Properties.Where(p => !p.Ignore));
if (@interface.Bases.Count == 0)
{
Property instance = new Property();
instance.Name = Helpers.InstanceIdentifier;
instance.QualifiedType = new QualifiedType(new BuiltinType(PrimitiveType.IntPtr));
instance.GetMethod = new Method();
@interface.Properties.Add(instance);
}
@interface.Events.AddRange(@base.Events);
if (addMembers)
{
@ -94,7 +103,7 @@ namespace CppSharp.Passes @@ -94,7 +103,7 @@ namespace CppSharp.Passes
private static void ImplementInterfaceProperties(Class @class, Class @interface)
{
foreach (var property in @interface.Properties)
foreach (var property in @interface.Properties.Where(p => p.Name != Helpers.InstanceIdentifier))
{
var impl = new Property(property) { Namespace = @class };
var rootBaseProperty = @class.GetRootBaseProperty(property, true);

29
src/Generator/Passes/ParamTypeToInterfacePass.cs

@ -0,0 +1,29 @@ @@ -0,0 +1,29 @@
using CppSharp.AST;
namespace CppSharp.Passes
{
public class ParamTypeToInterfacePass : TranslationUnitPass
{
public override bool VisitParameterDecl(Parameter parameter)
{
var tagType = parameter.QualifiedType.Type as TagType;
if (tagType == null)
{
var pointerType = parameter.QualifiedType.Type as PointerType;
if (pointerType != null)
tagType = pointerType.Pointee as TagType;
}
if (tagType != null)
{
var @class = tagType.Declaration as Class;
if (@class != null)
{
var @interface = @class.Namespace.FindClass("I" + @class.Name);
if (@interface != null)
parameter.QualifiedType = new QualifiedType(new TagType(@interface));
}
}
return base.VisitParameterDecl(parameter);
}
}
}

1
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -42,5 +42,6 @@ public class CSharpTempTests @@ -42,5 +42,6 @@ public class CSharpTempTests
bar[0] = new Foo { A = 1000 };
Assert.That(bar[0].A, Is.EqualTo(1000));
Assert.That(baz.farAwayFunc(), Is.EqualTo(20));
Assert.That(baz.takesQux(baz), Is.EqualTo(20));
}
}

7
tests/CSharpTemp/CSharpTemp.cpp

@ -31,7 +31,7 @@ const Foo& Bar::operator[](int i) const @@ -31,7 +31,7 @@ const Foo& Bar::operator[](int i) const
return m_foo;
}
int Qux::farAwayFunc()
int Qux::farAwayFunc() const
{
return 20;
}
@ -45,3 +45,8 @@ Foo& Bar::operator[](int i) @@ -45,3 +45,8 @@ Foo& Bar::operator[](int i)
{
return m_foo;
}
int Baz::takesQux(const Qux& qux)
{
return qux.farAwayFunc();
}

5
tests/CSharpTemp/CSharpTemp.h

@ -21,7 +21,7 @@ protected: @@ -21,7 +21,7 @@ protected:
class DLL_API Qux
{
public:
int farAwayFunc();
int farAwayFunc() const;
};
class DLL_API Bar : public Qux
@ -37,5 +37,6 @@ private: @@ -37,5 +37,6 @@ private:
class DLL_API Baz : public Foo, public Bar
{
public:
int takesQux(const Qux& qux);
};

Loading…
Cancel
Save