Variant: Add a NewArray (Variant[] children) convenience method

This allows you to avoid ugly calls like "NewArray (null, some_array)"
when you want the child type to be determined by the first element of
the children array.

Also throw specific exceptions when both type and children parameters
would be null.
This commit is contained in:
Bertrand Lorentz 2014-06-13 13:10:48 +02:00
parent b605b42540
commit a1636d306b
2 changed files with 17 additions and 3 deletions

View file

@ -162,10 +162,24 @@ namespace GLib {
[DllImport (Global.GLibNativeDll, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr g_variant_new_array (IntPtr child_type, IntPtr[] children, UIntPtr n_children);
public static Variant NewArray (Variant[] children)
{
if (children == null) {
throw new ArgumentNullException ("children", "To create an empty array use NewArray (VariantType.<Type>, null)");
}
return NewArray (null, children);
}
public static Variant NewArray (VariantType type, Variant[] children)
{
if (children == null)
return new Variant (g_variant_new_array (type.Handle, null, new UIntPtr (0ul)));
if (children == null) {
if (type == null) {
throw new ArgumentException ("The type and children parameters cannot be both null");
} else {
return new Variant (g_variant_new_array (type.Handle, null, new UIntPtr (0ul)));
}
}
IntPtr[] native = new IntPtr[children.Length];
for (int i = 0; i < children.Length; i++)

View file

@ -18,7 +18,7 @@ namespace sample
variant = Variant.NewTuple (null);
Console.WriteLine (variant.Print (true));
variant = Variant.NewArray (null, new Variant[] {new Variant ("String 4"), new Variant ("String 5")});
variant = Variant.NewArray (new Variant[] {new Variant ("String 4"), new Variant ("String 5")});
Console.WriteLine (variant.Print (true));
variant = Variant.NewArray (VariantType.String, null);