Ava
Ava

Reputation: 2132

Put GLib.Array in a GLib.Value?

I can't figure out how to wrap a GLib.Array in a GLib.Value.

I tried this code.

public int main (string[] args) {
  var value = Value(typeof (Array));

  var a = new Array<string>();
  a.append_val("test");
  value.set_object((Object) a);

  return 0;
}

But it resulted in these errors.

(process:1797): GLib-GObject-WARNING **: invalid uninstantiatable type '(null)' in cast to 'GObject'
(process:1797): GLib-GObject-CRITICAL **: g_value_set_object: assertion 'G_VALUE_HOLDS_OBJECT (value)' failed

Upvotes: 0

Views: 622

Answers (2)

Philip Withnall
Philip Withnall

Reputation: 5733

GArray is not a GObject. However, it is a boxed type (a more primitive version of type tagging used in GValue), so you should be able to store it in the GValue using value.set_boxed (a).

Upvotes: 1

AlThomas
AlThomas

Reputation: 4289

I think of GValue being used as a container for a single type of value, whereas GArray is a collection. GValue does have a type_compatible () method. Using that method as a check before your code shows GLib doesn't allow a GArray to be stored as a GObject:

public int main (string[] args) {
  if (!Value.type_compatible (typeof (Array), typeof (Object))) {
    message ("Incompatible types");
    return 1;
  }

  var value = Value(typeof (Array));

  var a = new Array<string>();
  a.append_val("test");
  value.set_object((Object) a);

  return 0;
}

This code stops where the check for compatible types is made.

I don't know what you are trying to achieve, but I suggest looking at GVariant:

void main () {
    string[] a = {"test"};
    Variant variant = a;
    print (variant.print(true) + "\n");
}

If you need a GValue then you can store a GVariant in a GValue using GValue's take_variant () method.

Upvotes: 1

Related Questions