mirror of
https://github.com/Ryujinx/GtkSharp.git
synced 2024-09-07 07:23:18 +02:00
generator: fixed optional array parameters
This commit is contained in:
parent
2152f4626e
commit
53312d5fc0
@ -149,6 +149,9 @@ namespace GtkSharp.Generation {
|
||||
|
||||
result [i] = p.PassAs != "" ? p.PassAs + " " : "";
|
||||
if (p.IsOptional && p.PassAs == String.Empty) {
|
||||
if (p.IsArray)
|
||||
result [i++] += "null";
|
||||
else
|
||||
result [i++] += p.Generatable.DefaultValue;
|
||||
}
|
||||
else
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user