Browse Source

Fixed the wrapping of operators for non-equality to handle null.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/585/merge
Dimitar Dobrev 10 years ago
parent
commit
f7b4d6ce83
  1. 15
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  2. 9
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 20
      tests/Common/Common.Tests.cs
  4. 11
      tests/Common/Common.cpp
  5. 4
      tests/Common/Common.h

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

@ -943,6 +943,21 @@ namespace CppSharp.Generators.CLI
{ {
CheckArgumentRange(function); CheckArgumentRange(function);
if (function.OperatorKind == CXXOperatorKind.EqualEqual ||
function.OperatorKind == CXXOperatorKind.ExclaimEqual)
{
WriteLine("bool {0}Null = ReferenceEquals({0}, nullptr);",
function.Parameters[0].Name);
WriteLine("bool {0}Null = ReferenceEquals({0}, nullptr);",
function.Parameters[1].Name);
WriteLine("if ({0}Null || {1}Null)",
function.Parameters[0].Name, function.Parameters[1].Name);
WriteLineIndent("return {0}{1}Null && {2}Null{3};",
function.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : "!(",
function.Parameters[0].Name, function.Parameters[1].Name,
function.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : ")");
}
var retType = function.ReturnType; var retType = function.ReturnType;
if (publicRetType == null) if (publicRetType == null)
publicRetType = retType.Type; publicRetType = retType.Type;

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

@ -2407,7 +2407,8 @@ namespace CppSharp.Generators.CSharp
return; return;
} }
if (method.OperatorKind == CXXOperatorKind.EqualEqual) if (method.OperatorKind == CXXOperatorKind.EqualEqual ||
method.OperatorKind == CXXOperatorKind.ExclaimEqual)
{ {
WriteLine("bool {0}Null = ReferenceEquals({0}, null);", WriteLine("bool {0}Null = ReferenceEquals({0}, null);",
method.Parameters[0].Name); method.Parameters[0].Name);
@ -2415,8 +2416,10 @@ namespace CppSharp.Generators.CSharp
method.Parameters[1].Name); method.Parameters[1].Name);
WriteLine("if ({0}Null || {1}Null)", WriteLine("if ({0}Null || {1}Null)",
method.Parameters[0].Name, method.Parameters[1].Name); method.Parameters[0].Name, method.Parameters[1].Name);
WriteLineIndent("return {0}Null && {1}Null;", WriteLineIndent("return {0}{1}Null && {2}Null{3};",
method.Parameters[0].Name, method.Parameters[1].Name); method.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : "!(",
method.Parameters[0].Name, method.Parameters[1].Name,
method.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : ")");
} }
GenerateInternalFunctionCall(method); GenerateInternalFunctionCall(method);

20
tests/Common/Common.Tests.cs

@ -502,10 +502,22 @@ public class CommonTests : GeneratorTestFixture
[Test] [Test]
public void TestEqualityOperator() public void TestEqualityOperator()
{ {
Assert.AreEqual(new Foo { A = 5, B = 5.5f }, new Foo { A = 5, B = 5.5f }); using (var foo = new Foo { A = 5, B = 5.5f })
Assert.AreNotEqual(new Foo { A = 5, B = 5.6f }, new Foo { A = 5, B = 5.5f }); {
Assert.AreEqual(new Bar { A = 5, B = 5.5f }, new Bar { A = 5, B = 5.5f }); Assert.IsTrue(foo == foo);
Assert.AreNotEqual(new Bar { A = 5, B = 5.6f }, new Bar { A = 5, B = 5.5f }); using (var notEqual = new Foo { A = 5, B = 5.6f })
{
Assert.IsTrue(notEqual != foo);
}
Assert.IsTrue(foo != null);
}
var bar = new Bar { A = 5, B = 5.5f };
Assert.IsTrue(bar == bar);
Assert.IsFalse(new Bar { A = 5, B = 5.6f } == bar);
using (var differentConstOverloads = new DifferentConstOverloads())
{
Assert.IsTrue(differentConstOverloads != null);
}
} }
[Test] [Test]

11
tests/Common/Common.cpp

@ -460,9 +460,18 @@ DLL_API const HasFriend operator-(const HasFriend& f1, const HasFriend& f2)
return HasFriend(f1.m - f2.m); return HasFriend(f1.m - f2.m);
} }
DifferentConstOverloads::DifferentConstOverloads() : i(5)
{
}
bool DifferentConstOverloads::operator ==(const DifferentConstOverloads& other) bool DifferentConstOverloads::operator ==(const DifferentConstOverloads& other)
{ {
return true; return i == other.i;
}
bool DifferentConstOverloads::operator !=(const DifferentConstOverloads& other)
{
return i != other.i;
} }
bool DifferentConstOverloads::operator ==(int number) const bool DifferentConstOverloads::operator ==(int number) const

4
tests/Common/Common.h

@ -772,8 +772,12 @@ template<typename T> class FriendTemplate
class DLL_API DifferentConstOverloads class DLL_API DifferentConstOverloads
{ {
public: public:
DifferentConstOverloads();
bool operator ==(const DifferentConstOverloads& other); bool operator ==(const DifferentConstOverloads& other);
bool operator !=(const DifferentConstOverloads& other);
bool operator ==(int number) const; bool operator ==(int number) const;
private:
int i;
}; };
class TestNamingAnonymousTypesInUnion class TestNamingAnonymousTypesInUnion

Loading…
Cancel
Save