Browse Source

Merge pull request #203 from ddobrev/master

Wrapped copy constructors
pull/204/head
João Matos 12 years ago
parent
commit
d0ff0b18d5
  1. 2
      src/Generator/AST/Utils.cs
  2. 4
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  3. 4
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  4. 15
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  5. 8
      src/Generator/Passes/CheckStaticClass.cs
  6. 14
      tests/Basic/Basic.Tests.cs
  7. 10
      tests/Basic/Basic.cpp
  8. 9
      tests/Basic/Basic.h
  9. 13
      tests/CSharpTemp/CSharpTemp.Tests.cs
  10. 12
      tests/CSharpTemp/CSharpTemp.cpp
  11. 5
      tests/CSharpTemp/CSharpTemp.cs
  12. 9
      tests/CSharpTemp/CSharpTemp.h

2
src/Generator/AST/Utils.cs

@ -25,7 +25,7 @@ namespace CppSharp.AST
if (@class != null && @class.IsValueType && isEmptyCtor) if (@class != null && @class.IsValueType && isEmptyCtor)
return true; return true;
if (method.IsCopyConstructor || method.IsMoveConstructor) if (method.IsMoveConstructor)
return true; return true;
if (method.IsDestructor) if (method.IsDestructor)

4
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -374,6 +374,10 @@ namespace CppSharp.Generators.CLI
if (ASTUtils.CheckIgnoreMethod(ctor)) if (ASTUtils.CheckIgnoreMethod(ctor))
continue; continue;
// C++/CLI does not allow special member funtions for value types.
if (@class.IsValueType && ctor.IsCopyConstructor)
continue;
GenerateMethod(ctor); GenerateMethod(ctor);
} }

4
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -142,6 +142,10 @@ namespace CppSharp.Generators.CLI
if (ASTUtils.CheckIgnoreMethod(method)) if (ASTUtils.CheckIgnoreMethod(method))
continue; continue;
// C++/CLI does not allow special member funtions for value types.
if (@class.IsValueType && method.IsCopyConstructor)
continue;
GenerateMethod(method, @class); GenerateMethod(method, @class);
} }

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

@ -2159,8 +2159,19 @@ namespace CppSharp.Generators.CSharp
WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier, WriteLine("{0} = Marshal.AllocHGlobal({1});", Helpers.InstanceIdentifier,
@class.Layout.Size); @class.Layout.Size);
if (!method.IsDefaultConstructor || @class.HasNonTrivialDefaultConstructor) if (method.IsCopyConstructor)
GenerateInternalFunctionCall(method); {
if (@class.HasNonTrivialCopyConstructor)
GenerateInternalFunctionCall(method);
else
WriteLine("*(({0}.Internal*) __Instance) = *(({0}.Internal*) {1}.__Instance);",
@class.Name, method.Parameters[0].Name);
}
else
{
if (!method.IsDefaultConstructor || @class.HasNonTrivialDefaultConstructor)
GenerateInternalFunctionCall(method);
}
GenerateVTableClassSetupCall(@class); GenerateVTableClassSetupCall(@class);
} }

8
src/Generator/Passes/CheckStaticClass.cs

@ -59,6 +59,14 @@ namespace CppSharp.Passes
// If all the above constraints hold, then we assume the class can be // If all the above constraints hold, then we assume the class can be
// static. // static.
@class.IsStatic = true; @class.IsStatic = true;
// Ignore the special methods for static classes.
foreach (var ctor in @class.Constructors)
ctor.IsGenerated = false;
foreach (var dtor in @class.Destructors)
dtor.IsGenerated = false;
return true; return true;
} }
} }

14
tests/Basic/Basic.Tests.cs

@ -193,5 +193,19 @@ public class BasicTests : GeneratorTestFixture
public void TestChar16() public void TestChar16()
{ {
} }
[Test]
public void TestCopyConstructor()
{
Foo foo = new Foo { A = 5, B = 5.5f };
var copyFoo = new Foo(foo);
Assert.That(foo.A, Is.EqualTo(copyFoo.A));
Assert.That(foo.B, Is.EqualTo(copyFoo.B));
var testCopyConstructorRef = new TestCopyConstructorRef { A = 10, B = 5 };
var copyBar = new TestCopyConstructorRef(testCopyConstructorRef);
Assert.That(testCopyConstructorRef.A, Is.EqualTo(copyBar.A));
Assert.That(testCopyConstructorRef.B, Is.EqualTo(copyBar.B));
}
} }

10
tests/Basic/Basic.cpp

@ -217,3 +217,13 @@ Bar::Item operator |(Bar::Item left, Bar::Item right)
{ {
return left | right; return left | right;
} }
TestCopyConstructorRef::TestCopyConstructorRef()
{
}
TestCopyConstructorRef::TestCopyConstructorRef(const TestCopyConstructorRef& other)
{
A = other.A;
B = other.B;
}

9
tests/Basic/Basic.h

@ -278,3 +278,12 @@ class DependentTypeWithNestedIndependent
long l; long l;
}; };
}; };
class DLL_API TestCopyConstructorRef
{
public:
TestCopyConstructorRef();
TestCopyConstructorRef(const TestCopyConstructorRef& other);
int A;
float B;
};

13
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -34,7 +34,7 @@ public class CSharpTempTests : GeneratorTestFixture
[Test] [Test]
public void TestFixedArrays() public void TestFixedArrays()
{ {
Qux qux = new Qux(null); Qux qux = new Qux((Foo) null);
var array = new[] { 1, 2, 3 }; var array = new[] { 1, 2, 3 };
qux.Array = array; qux.Array = array;
for (int i = 0; i < qux.Array.Length; i++) for (int i = 0; i < qux.Array.Length; i++)
@ -64,7 +64,7 @@ public class CSharpTempTests : GeneratorTestFixture
Assert.That(proprietor.Value, Is.EqualTo(20)); Assert.That(proprietor.Value, Is.EqualTo(20));
proprietor.Prop = 50; proprietor.Prop = 50;
Assert.That(proprietor.Prop, Is.EqualTo(50)); Assert.That(proprietor.Prop, Is.EqualTo(50));
var p = new P(null); var p = new P((IQux) null);
p.Value = 20; p.Value = 20;
Assert.That(p.Value, Is.EqualTo(30)); Assert.That(p.Value, Is.EqualTo(30));
p.Prop = 50; p.Prop = 50;
@ -107,4 +107,13 @@ public class CSharpTempTests : GeneratorTestFixture
bar.ArrayOfPrimitivePointers = array; bar.ArrayOfPrimitivePointers = array;
Assert.That(i, Is.EqualTo(*(int*) bar.ArrayOfPrimitivePointers[0])); Assert.That(i, Is.EqualTo(*(int*) bar.ArrayOfPrimitivePointers[0]));
} }
[Test]
public void TestCopyConstructorValue()
{
var testCopyConstructorVal = new TestCopyConstructorVal { A = 10, B = 5 };
var copyBar = new TestCopyConstructorVal(testCopyConstructorVal);
Assert.That(testCopyConstructorVal.A, Is.EqualTo(copyBar.A));
Assert.That(testCopyConstructorVal.B, Is.EqualTo(copyBar.B));
}
} }

12
tests/CSharpTemp/CSharpTemp.cpp

@ -190,4 +190,14 @@ void P::setIsBool(bool value)
} }
int TestDestructors::Marker = 0; int TestDestructors::Marker = 0;
TestCopyConstructorVal::TestCopyConstructorVal()
{
}
TestCopyConstructorVal::TestCopyConstructorVal(const TestCopyConstructorVal& other)
{
A = other.A;
B = other.B;
}

5
tests/CSharpTemp/CSharpTemp.cs

@ -64,6 +64,11 @@ namespace CppSharp.Tests
driver.TranslationUnitPasses.AddPass(new TestAttributesPass()); driver.TranslationUnitPasses.AddPass(new TestAttributesPass());
} }
public override void Preprocess(Driver driver, ASTContext ctx)
{
ctx.SetClassAsValueType("TestCopyConstructorVal");
}
public override void Postprocess(Driver driver, ASTContext lib) public override void Postprocess(Driver driver, ASTContext lib)
{ {
new CaseRenamePass( new CaseRenamePass(

9
tests/CSharpTemp/CSharpTemp.h

@ -138,3 +138,12 @@ struct DLL_API TestDestructors
TestDestructors() { Marker = 0xf00d; } TestDestructors() { Marker = 0xf00d; }
~TestDestructors() { Marker = 0xcafe; } ~TestDestructors() { Marker = 0xcafe; }
}; };
class DLL_API TestCopyConstructorVal
{
public:
TestCopyConstructorVal();
TestCopyConstructorVal(const TestCopyConstructorVal& other);
int A;
float B;
};

Loading…
Cancel
Save