1.34 <GVariantDict>

Structure: <GVariantDict>

<GVariantDict> is a mutable interface to <GVariant> dictionaries.

It can be used for doing a sequence of dictionary lookups in an efficient way on an existing <GVariant> dictionary or it can be used to construct new dictionaries with a hashtable-like interface. It can also be used for taking existing dictionaries and modifying them in order to create new ones.

<GVariantDict> can only be used with G_VARIANT_TYPE_VARDICT dictionaries.

It is possible to use <GVariantDict> allocated on the stack or on the heap. When using a stack-allocated <GVariantDict>, you begin with a call to g_variant_dict_init() and free the resources with a call to variant-dict:clear.

Heap-allocated <GVariantDict> follows normal refcounting rules: you allocate it with variant-dict:new and use variant-dict:ref and variant-dict:unref.

variant-dict:end is used to convert the <GVariantDict> back into a dictionary-type <GVariant>. When used with stack-allocated instances, this also implicitly frees all associated memory, but for heap-allocated instances, you must still call variant-dict:unref afterwards.

You will typically want to use a heap-allocated <GVariantDict> when you expose it as part of an API. For most other uses, the stack-allocated form will be more convenient.

Consider the following two examples that do the same thing in each style: take an existing dictionary and look up the "count" uint32 key, adding 1 to it if it is found, or returning an error if the key is not found. Each returns the new dictionary as a floating <GVariant>.

Using a stack-allocated GVariantDict


  GVariant *
  add_to_count (GVariant  *orig,
                GError   **error)
  {
    GVariantDict dict;
    guint32 count;

    g_variant_dict_init (&dict, orig);
    if (!g_variant_dict_lookup (&dict, "count", "u", &count))
      {
        g_set_error (...);
        g_variant_dict_clear (&dict);
        return NULL;
      }

    g_variant_dict_insert (&dict, "count", "u", count + 1);

    return g_variant_dict_end (&dict);
  }

Using heap-allocated GVariantDict


  GVariant *
  add_to_count (GVariant  *orig,
                GError   **error)
  {
    GVariantDict *dict;
    GVariant *result;
    guint32 count;

    dict = g_variant_dict_new (orig);

    if (g_variant_dict_lookup (dict, "count", "u", &count))
      {
        g_variant_dict_insert (dict, "count", "u", count + 1);
        result = g_variant_dict_end (dict);
      }
    else
      {
        g_set_error (...);
        result = NULL;
      }

    g_variant_dict_unref (dict);

    return result;
  }

Procedures

Method: variant-dict:clear self
Method: clear self

Releases all memory associated with a <GVariantDict> without freeing the <GVariantDict> structure itself.

It typically only makes sense to do this on a stack-allocated <GVariantDict> if you want to abort building the value part-way through. This function need not be called if you call variant-dict:end and it also doesn’t need to be called on dicts allocated with g_variant_dict_new (see variant-dict:unref for that).

It is valid to call this function on either an initialised <GVariantDict> or one that was previously cleared by an earlier call to variant-dict:clear but it is not valid to call this function on uninitialised memory.

self

Passed: a <GVariantDict>.

a <GVariantDict>

Method: variant-dict:contains? self key ⇒ %return
Method: contains? self key ⇒ %return

Checks if key exists in self.

self

Passed: a <GVariantDict>.

a <GVariantDict>

key

Passed: a string.

the key to look up in the dictionary

%return

Returned: a boolean.

#t if key is in self

Method: variant-dict:end self ⇒ %return
Method: end self ⇒ %return

Returns the current value of self as a <GVariant> of type G_VARIANT_TYPE_VARDICT, clearing it in the process.

It is not permissible to use self in any way after this call except for reference counting operations (in the case of a heap-allocated <GVariantDict>) or by reinitialising it with g_variant_dict_init() (in the case of stack-allocated).

self

Passed: a <GVariantDict>.

a <GVariantDict>

%return

Returned: a <GVariant>.

a new, floating, <GVariant>

Method: variant-dict:insert-value self key value
Method: insert-value self key value

Inserts (or replaces) a key in a <GVariantDict>.

value is consumed if it is floating.

self

Passed: a <GVariantDict>.

a <GVariantDict>

key

Passed: a string.

the key to insert a value for

value

Passed: a <GVariant>.

the value to insert

Method: variant-dict:lookup-value self key [expected-type] ⇒ %return
Method: lookup-value self key [expected-type] ⇒ %return

Looks up a value in a <GVariantDict>.

If key is not found in dictionary, #f is returned.

The expected-type string specifies what type of value is expected. If the value associated with key has a different type then #f is returned.

If the key is found and the value has the correct type, it is returned. If expected-type was specified then any non-#f return value will have this type.

self

Passed: a <GVariantDict>.

a <GVariantDict>

key

Passed: a string.

the key to look up in the dictionary

expected-type

Passed, or left out: a <GVariantType>, or #f.

a <GVariantType>, or #f

%return

Returned: a <GVariant>, or #f.

the value of the dictionary key, or #f

Procedure: variant-dict:new [from-asv] ⇒ %return

Allocates and initialises a new <GVariantDict>.

You should call variant-dict:unref on the return value when it is no longer needed. The memory will not be automatically freed by any other call.

In some cases it may be easier to place a <GVariantDict> directly on the stack of the calling function and initialise it with g_variant_dict_init(). This is particularly useful when you are using <GVariantDict> to construct a <GVariant>.

from-asv

Passed, or left out: a <GVariant>, or #f.

the <GVariant> with which to initialise the dictionary

%return

Returned: a <GVariantDict>.

a <GVariantDict>

Method: variant-dict:ref self ⇒ %return
Method: ref self ⇒ %return

Increases the reference count on self.

Don’t call this on stack-allocated <GVariantDict> instances or bad things will happen.

self

Passed: a <GVariantDict>.

a heap-allocated <GVariantDict>

%return

Returned: a <GVariantDict>.

a new reference to self

Method: variant-dict:remove? self key ⇒ %return
Method: remove? self key ⇒ %return

Removes a key and its associated value from a <GVariantDict>.

self

Passed: a <GVariantDict>.

a <GVariantDict>

key

Passed: a string.

the key to remove

%return

Returned: a boolean.

#t if the key was found and removed

Method: variant-dict:unref self
Method: unref self

Decreases the reference count on self.

In the event that there are no more references, releases all memory associated with the <GVariantDict>.

Don’t call this on stack-allocated <GVariantDict> instances or bad things will happen.

self

Passed: a <GVariantDict>.

a heap-allocated <GVariantDict>