Skip to content

Commit

Permalink
Generator: Generate property descriptors for boxed records
Browse files Browse the repository at this point in the history
  • Loading branch information
badcel committed Mar 15, 2024
1 parent 1bb54ea commit a85ab77
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Generation/Generator/Model/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public static void ThrowIfNotSupported(GirModel.ComplexType complexType, GirMode
if (property.AnyType.Is<GirModel.Interface>())
return;

if (property.AnyType.Is<GirModel.Record>())
throw new System.NotImplementedException("There is currently no concept for transfering native records (structs) into the managed world.");
if (property.AnyType.Is<GirModel.Record>(out var record) && (Record.IsTyped(record) || Record.IsForeignTyped(record) || Record.IsOpaqueTyped(record)))
return;

throw new System.Exception($"Property {complexType.Name}.{property.Name} is not supported");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace {Namespace.GetPublicName(record.Namespace)};
// AUTOGENERATED FILE - DO NOT MODIFY
{PlatformSupportAttribute.Render(record as GirModel.PlatformDependent)}
public partial class {name}
public partial class {name} : GLib.BoxedRecord
{{
public {internalHandleName} Handle {{ get; }}
Expand All @@ -43,6 +43,11 @@ public partial class {name}
// Implement this to perform additional steps in the constructor
partial void Initialize();
System.IntPtr GLib.BoxedRecord.GetHandle()
{{
return Handle.DangerousGetHandle();
}}
{FunctionRenderer.Render(record.TypeFunction)}
}}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace {Namespace.GetPublicName(record.Namespace)};
// AUTOGENERATED FILE - DO NOT MODIFY
{PlatformSupportAttribute.Render(record as GirModel.PlatformDependent)}
public partial class {name}
public partial class {name} : GLib.BoxedRecord
{{
public {internalHandleName} Handle {{ get; }}
Expand Down Expand Up @@ -57,6 +57,11 @@ public partial class {name}
.Where(Method.IsEnabled)
.Select(MethodRenderer.Render)
.Join(Environment.NewLine)}
System.IntPtr GLib.BoxedRecord.GetHandle()
{{
return Handle.DangerousGetHandle();
}}
}}";
}
}
7 changes: 6 additions & 1 deletion src/Generation/Generator/Renderer/Public/TypedRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace {Namespace.GetPublicName(record.Namespace)};
// AUTOGENERATED FILE - DO NOT MODIFY
{PlatformSupportAttribute.Render(record as GirModel.PlatformDependent)}
public partial class {name}
public partial class {name} : GLib.BoxedRecord
{{
public {internalHandleName} Handle {{ get; }}
Expand Down Expand Up @@ -66,6 +66,11 @@ public partial class {name}
.Where(Method.IsEnabled)
.Select(MethodRenderer.Render)
.Join(Environment.NewLine)}
System.IntPtr GLib.BoxedRecord.GetHandle()
{{
return Handle.DangerousGetHandle();
}}
}}";
}

Expand Down
12 changes: 12 additions & 0 deletions src/Libs/GLib-2.0/Public/BoxedRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Runtime.InteropServices;

namespace GLib;

/// <summary>
/// Defines that this type is a boxed record in GObject terms.
/// </summary>
/// <remarks>Do not implement manually. All implementations of this interface are automatically generated.</remarks>
public interface BoxedRecord
{
System.IntPtr GetHandle();
}
3 changes: 3 additions & 0 deletions src/Libs/GObject-2.0/Public/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ internal void Set(object? value)
var strArray = Utf8StringArrayNullTerminatedOwnedHandle.Create(array);
SetBoxed(strArray.DangerousGetHandle());
break;
case GLib.BoxedRecord b:
SetBoxed(b.GetHandle());
break;
case GLib.Variant v:
SetVariant(v);
break;
Expand Down
16 changes: 16 additions & 0 deletions src/Native/GirTestLib/girtest-property-tester.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "girtest-property-tester.h"
#include "girtest-typed-record-tester.h"

/**
* GirTestPropertyTester:
Expand All @@ -10,6 +11,7 @@ typedef enum
{
PROP_STRING_VALUE = 1,
PROP_PROPERTY_TESTER = 2,
PROP_TYPED_RECORD_VALUE = 3,
N_PROPERTIES
} PropertyTesterProperty;

Expand All @@ -19,6 +21,7 @@ struct _GirTestPropertyTester

gchar *string_value;
gchar *property_tester;
GirTestTypedRecordTester* record;
};

G_DEFINE_TYPE(GirTestPropertyTester, girtest_property_tester, G_TYPE_OBJECT)
Expand All @@ -42,6 +45,9 @@ girtest_property_tester_get_property (GObject *object,
case PROP_PROPERTY_TESTER:
g_value_set_string (value, self->property_tester);
break;
case PROP_TYPED_RECORD_VALUE:
g_value_set_boxed (value, self->record);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
Expand All @@ -63,6 +69,9 @@ girtest_property_tester_set_property (GObject *object,
case PROP_PROPERTY_TESTER:
self->property_tester = g_value_dup_string (value);
break;
case PROP_TYPED_RECORD_VALUE:
self->record = g_value_get_boxed (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
Expand Down Expand Up @@ -95,6 +104,13 @@ girtest_property_tester_class_init(GirTestPropertyTesterClass *class)
NULL /* default value */,
G_PARAM_READWRITE);

/**
* GirPropertyTester:record-value:
* Contains a typed record tester
*/
properties[PROP_TYPED_RECORD_VALUE] =
g_param_spec_boxed ("record-value", NULL, NULL, GIRTEST_TYPE_TYPED_RECORD_TESTER, G_PARAM_READWRITE);

g_object_class_install_properties (object_class, N_PROPERTIES, properties);
}

Expand Down
20 changes: 20 additions & 0 deletions src/Tests/Libs/GirTest-0.1.Tests/PropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public void TestNullStringProperty()
obj.StringValue.Should().Be(text);
obj.StringValue = null;
obj.StringValue.Should().BeNull();

PropertyTester.StringValuePropertyDefinition.UnmanagedName.Should().Be("string-value");
PropertyTester.StringValuePropertyDefinition.ManagedName.Should().Be(nameof(PropertyTester.StringValue));
}

[TestMethod]
Expand All @@ -34,5 +37,22 @@ public void PropertyNamedLikeClass()
//Properties named like a class are suffixed with an underscore.
var obj = PropertyTester.New();
obj.PropertyTester_ = "test";

Check warning on line 40 in src/Tests/Libs/GirTest-0.1.Tests/PropertyTest.cs

View workflow job for this annotation

GitHub Actions / Build (Linux)

Fix formatting
PropertyTester.PropertyTester_PropertyDefinition.UnmanagedName.Should().Be("property-tester");
PropertyTester.PropertyTester_PropertyDefinition.ManagedName.Should().Be(nameof(PropertyTester.PropertyTester_));
}

[TestMethod]
public void SupportsTypedRecordProperty()
{
var r = TypedRecordTester.New();
r.CustomBitfield = TypedRecordTesterBitfield.One | TypedRecordTesterBitfield.Zero;
var obj = PropertyTester.New();
obj.RecordValue = r;

obj.RecordValue.CustomBitfield.Should().Be(TypedRecordTesterBitfield.One | TypedRecordTesterBitfield.Zero);

PropertyTester.RecordValuePropertyDefinition.UnmanagedName.Should().Be("record-value");
PropertyTester.RecordValuePropertyDefinition.ManagedName.Should().Be(nameof(PropertyTester.RecordValue));
}
}

0 comments on commit a85ab77

Please sign in to comment.