Skip to content

Commit

Permalink
Merge pull request #1050 from gircore/untyped-record-equality
Browse files Browse the repository at this point in the history
Untyped records: Implement IEquality via pointer comparison
  • Loading branch information
badcel authored Apr 24, 2024
2 parents 824a7cc + 09f2d67 commit efe0641
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ internal class RecordEqualsMethodCollidesWithGeneratedCode : Fixer<GirModel.Reco
{
public void Fixup(GirModel.Record record)
{
if (Model.Record.IsTyped(record) || Model.Record.IsOpaqueTyped(record) || Model.Record.IsOpaqueUntyped(record))
if (Model.Record.IsTyped(record)
|| Model.Record.IsOpaqueTyped(record)
|| Model.Record.IsOpaqueUntyped(record)
|| Model.Record.IsUntyped(record))
{
foreach (var method in record.Methods)
{
Expand Down
21 changes: 21 additions & 0 deletions src/Generation/Generator/Renderer/Internal/UntypedRecordHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ public abstract class {typeName} : SafeHandle
protected {typeName}(bool ownsHandle) : base(IntPtr.Zero, ownsHandle) {{ }}
{record.Fields.Select(x => RenderField(record, x)).Join(Environment.NewLine)}
public bool Equals({typeName}? other)
{{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return handle.Equals(other.handle);
}}
public override bool Equals(object? obj)
{{
return ReferenceEquals(this, obj) || obj is {typeName} other && Equals(other);
}}
public override int GetHashCode()
{{
return handle.GetHashCode();
}}
}}
public class {unownedHandleTypeName} : {typeName}
Expand Down
21 changes: 21 additions & 0 deletions src/Generation/Generator/Renderer/Public/UntypedRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ public sealed partial class {name}
.Where(Method.IsEnabled)
.Select(MethodRenderer.Render)
.Join(Environment.NewLine)}
public bool Equals({name}? other)
{{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return Handle.Equals(other.Handle);
}}
public override bool Equals(object? obj)
{{
return ReferenceEquals(this, obj) || obj is {name} other && Equals(other);
}}
public override int GetHashCode()
{{
return Handle.GetHashCode();
}}
}}";
}

Expand Down
14 changes: 14 additions & 0 deletions src/Native/GirTestLib/girtest-untyped-record-tester.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,18 @@ int girtest_untyped_record_tester_get_a_from_last_element_pointer(GirTestUntyped
{
GirTestUntypedRecordTester** result = array += (length-1);
return (*result)->a;
}

/**
* girtest_untyped_record_tester_equals:
* @self: An instance
* @other: Another instance
*
* Compare the data of the given instances.
*
* Returns: if the given instances contain the same data.
**/
gboolean girtest_untyped_record_tester_equals(GirTestUntypedRecordTester *self, GirTestUntypedRecordTester *other)
{
return self->a == other->a;
}
1 change: 1 addition & 0 deletions src/Native/GirTestLib/girtest-untyped-record-tester.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ GirTestUntypedRecordTester* girtest_untyped_record_tester_callback_out_parameter
GirTestUntypedRecordTester* girtest_untyped_record_tester_callback_out_parameter_callee_allocates(GirTestUntypedRecordCallbackOutParameterCalleeAllocates callback);
int girtest_untyped_record_tester_get_a_from_last_element(GirTestUntypedRecordTester* array, int length);
int girtest_untyped_record_tester_get_a_from_last_element_pointer(GirTestUntypedRecordTester** array, int length);
gboolean girtest_untyped_record_tester_equals(GirTestUntypedRecordTester *self, GirTestUntypedRecordTester *other);
G_END_DECLS
9 changes: 9 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/UntypedRecordTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,13 @@ public void IsSealed()
{
typeof(UntypedRecordTester).IsSealed.Should().BeTrue();
}

[TestMethod]
public void HasNativeEqualsMethod()
{
var instance1 = new UntypedRecordTester();
var instance2 = new UntypedRecordTester();

instance1.NativeEquals(instance2).Should().BeTrue();
}
}

0 comments on commit efe0641

Please sign in to comment.