generator: fixed optional array parameters

This commit is contained in:
Stephan Sundermann 2013-08-14 13:16:40 +02:00 committed by Andrés G. Aragoneses
parent 2152f4626e
commit 53312d5fc0
3 changed files with 54 additions and 2 deletions

View File

@ -149,7 +149,10 @@ namespace GtkSharp.Generation {
result [i] = p.PassAs != "" ? p.PassAs + " " : "";
if (p.IsOptional && p.PassAs == String.Empty) {
result [i++] += p.Generatable.DefaultValue;
if (p.IsArray)
result [i++] += "null";
else
result [i++] += p.Generatable.DefaultValue;
}
else
result [i++] += p.Name;

View File

@ -58,6 +58,10 @@ namespace GtkSharp.Generation {
}
}
bool IsNullTermArray {
get { return elem.GetAttributeAsBoolean ("null_term_array"); }
}
public new string CSType {
get {
string type = base.CSType;
@ -143,9 +147,21 @@ namespace GtkSharp.Generation {
string wrapped_name = SymbolTable.Table.MangleName (CName);
IGeneratable gen = table [CType];
if (IsArray) {
if (IsArray && !IsNullTermArray) {
sw.WriteLine (indent + "[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]");
sw.WriteLine (indent + "{0} {1} {2};", Access, CSType, StudlyName);
} else if (IsArray && IsNullTermArray) {
sw.WriteLine (indent + "private {0} {1};", "IntPtr", StudlyName+ "Ptr");
if ((Readable || Writable) && Access == "public") {
sw.WriteLine (indent + "public {0} {1} {{", CSType, StudlyName);
if (Readable)
sw.WriteLine (indent + "\tget {{ return GLib.Marshaller.StructArrayFromNullTerminatedIntPtr<{0}> ({1}); }}",
base.CSType, StudlyName + "Ptr");
if (Writable)
sw.WriteLine (indent + "\tset {{ {0} = GLib.Marshaller.StructArrayToNullTerminatedStructArrayIntPtr<{1}> (value); }}",
StudlyName + "Ptr", base.CSType);
sw.WriteLine (indent + "}");
}
} else if (IsBitfield) {
base.Generate (gen_info, indent);
} else if (gen is IAccessor) {

View File

@ -463,6 +463,39 @@ namespace GLib {
return result;
}
public static T[] StructArrayFromNullTerminatedIntPtr<T> (IntPtr array)
{
var res = new List<T> ();
IntPtr current = array;
T currentStruct = default(T);
while (current != IntPtr.Zero) {
Marshal.PtrToStructure (current, currentStruct);
res.Add (currentStruct);
current = (IntPtr) ((long)current + Marshal.SizeOf (typeof (T)));
}
return res.ToArray ();
}
public static IntPtr StructArrayToNullTerminatedStructArrayIntPtr<T> (T[] InputArray)
{
int intPtrSize = Marshal.SizeOf (typeof (IntPtr));
IntPtr mem = Marshal.AllocHGlobal ((InputArray.Length + 1) * intPtrSize);
for (int i = 0; i < InputArray.Length; i++) {
IntPtr structPtr = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (T)));
Marshal.StructureToPtr (InputArray[i], structPtr, false);
// jump to next pointer
Marshal.WriteIntPtr (mem, structPtr);
mem = (IntPtr) ((long)mem + intPtrSize);
}
// null terminate
Marshal.WriteIntPtr (mem, IntPtr.Zero);
return mem;
}
}
}