From 06b966beefa2efe585f1071e1bb895fa85dcebfe Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Mon, 17 Mar 2014 22:39:14 +0100 Subject: [PATCH] generator: Add support for fixed length arrays as method parameters Fixed length arrays are available in gobject introspection and are already converted by the bindinator tool. The array_len attribute was only used in structs. This adds support for them as method parameters, generating the correct code for them. Fixes issue #98. Signed-off-by: Bertrand Lorentz --- gapi.xsd | 1 + generator/ArrayParameter.cs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gapi.xsd b/gapi.xsd index af09aca5a..bd6c05575 100644 --- a/gapi.xsd +++ b/gapi.xsd @@ -296,6 +296,7 @@ + diff --git a/generator/ArrayParameter.cs b/generator/ArrayParameter.cs index 9b4f8034d..9f606c8b2 100644 --- a/generator/ArrayParameter.cs +++ b/generator/ArrayParameter.cs @@ -33,6 +33,8 @@ namespace GtkSharp.Generation { public ArrayParameter (XmlElement elem) : base (elem) { null_terminated = elem.GetAttributeAsBoolean ("null_term_array"); + if (elem.HasAttribute ("array_len")) + FixedArrayLength = Int32.Parse (elem.GetAttribute ("array_len")); } public override string MarshalType { @@ -50,12 +52,19 @@ namespace GtkSharp.Generation { } } + public int? FixedArrayLength { get; private set; } + public override string[] Prepare { get { - if (CSType == MarshalType) + if (CSType == MarshalType && !FixedArrayLength.HasValue) return new string [0]; var result = new List (); + + if (FixedArrayLength.HasValue) { + result.Add (String.Format ("{0} = new {1}[{2}];", Name, MarshalType.TrimEnd ('[', ']'), FixedArrayLength)); + return result.ToArray (); + } result.Add (String.Format ("int cnt_{0} = {0} == null ? 0 : {0}.Length;", CallName)); result.Add (String.Format ("{0}[] native_{1} = new {0} [cnt_{1}" + (NullTerminated ? " + 1" : "") + "];", MarshalType.TrimEnd('[', ']'), CallName)); result.Add (String.Format ("for (int i = 0; i < cnt_{0}; i++)", CallName)); @@ -75,6 +84,8 @@ namespace GtkSharp.Generation { get { if (CSType != MarshalType) return "native_" + CallName; + else if (FixedArrayLength.HasValue) + return base.CallString; else return CallName; }