diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index defa65fc..8b785d88 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -943,6 +943,21 @@ namespace CppSharp.Generators.CLI { 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; if (publicRetType == null) publicRetType = retType.Type; diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 44a9afe1..5c700740 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -2407,7 +2407,8 @@ namespace CppSharp.Generators.CSharp return; } - if (method.OperatorKind == CXXOperatorKind.EqualEqual) + if (method.OperatorKind == CXXOperatorKind.EqualEqual || + method.OperatorKind == CXXOperatorKind.ExclaimEqual) { WriteLine("bool {0}Null = ReferenceEquals({0}, null);", method.Parameters[0].Name); @@ -2415,8 +2416,10 @@ namespace CppSharp.Generators.CSharp method.Parameters[1].Name); WriteLine("if ({0}Null || {1}Null)", method.Parameters[0].Name, method.Parameters[1].Name); - WriteLineIndent("return {0}Null && {1}Null;", - method.Parameters[0].Name, method.Parameters[1].Name); + WriteLineIndent("return {0}{1}Null && {2}Null{3};", + method.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : "!(", + method.Parameters[0].Name, method.Parameters[1].Name, + method.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : ")"); } GenerateInternalFunctionCall(method); diff --git a/tests/Common/Common.Tests.cs b/tests/Common/Common.Tests.cs index 62e72810..158ee72a 100644 --- a/tests/Common/Common.Tests.cs +++ b/tests/Common/Common.Tests.cs @@ -502,10 +502,22 @@ public class CommonTests : GeneratorTestFixture [Test] public void TestEqualityOperator() { - Assert.AreEqual(new Foo { A = 5, B = 5.5f }, 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.AreNotEqual(new Bar { A = 5, B = 5.6f }, new Bar { A = 5, B = 5.5f }); + using (var foo = new Foo { A = 5, B = 5.5f }) + { + Assert.IsTrue(foo == foo); + 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] diff --git a/tests/Common/Common.cpp b/tests/Common/Common.cpp index 7aadc413..3c02f758 100644 --- a/tests/Common/Common.cpp +++ b/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); } +DifferentConstOverloads::DifferentConstOverloads() : i(5) +{ +} + 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 diff --git a/tests/Common/Common.h b/tests/Common/Common.h index 6656f8d1..6cc9d489 100644 --- a/tests/Common/Common.h +++ b/tests/Common/Common.h @@ -772,8 +772,12 @@ template class FriendTemplate class DLL_API DifferentConstOverloads { public: + DifferentConstOverloads(); bool operator ==(const DifferentConstOverloads& other); + bool operator !=(const DifferentConstOverloads& other); bool operator ==(int number) const; +private: + int i; }; class TestNamingAnonymousTypesInUnion