<refentry id="glib-Keyed-Data-Lists">
<refmeta>
<refentrytitle>Keyed Data Lists</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Keyed Data Lists</refname><refpurpose>lists of data elements which are accessible by a string or <link linkend="GQuark"><type>GQuark</type></link> identifier.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;


struct      <link linkend="GData">GData</link>;
<link linkend="void">void</link>        <link linkend="g-datalist-init">g_datalist_init</link>                 (<link linkend="GData">GData</link> **datalist);

#define     <link linkend="g-datalist-id-set-data">g_datalist_id_set_data</link>          (dl, q, d)
<link linkend="void">void</link>        <link linkend="g-datalist-id-set-data-full">g_datalist_id_set_data_full</link>     (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="GDestroyNotify">GDestroyNotify</link> destroy_func);
<link linkend="gpointer">gpointer</link>    <link linkend="g-datalist-id-get-data">g_datalist_id_get_data</link>          (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id);
#define     <link linkend="g-datalist-id-remove-data">g_datalist_id_remove_data</link>       (dl, q)
<link linkend="gpointer">gpointer</link>    <link linkend="g-datalist-id-remove-no-notify">g_datalist_id_remove_no_notify</link>  (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id);

#define     <link linkend="g-datalist-set-data">g_datalist_set_data</link>             (dl, k, d)
#define     <link linkend="g-datalist-set-data-full">g_datalist_set_data_full</link>        (dl, k, d, f)
#define     <link linkend="g-datalist-get-data">g_datalist_get_data</link>             (dl, k)
#define     <link linkend="g-datalist-remove-data">g_datalist_remove_data</link>          (dl, k)
#define     <link linkend="g-datalist-remove-no-notify">g_datalist_remove_no_notify</link>     (dl, k)

<link linkend="void">void</link>        <link linkend="g-datalist-foreach">g_datalist_foreach</link>              (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GDataForeachFunc">GDataForeachFunc</link> func,
                                             <link linkend="gpointer">gpointer</link> user_data);
<link linkend="void">void</link>        <link linkend="g-datalist-clear">g_datalist_clear</link>                (<link linkend="GData">GData</link> **datalist);
</synopsis>
</refsynopsisdiv>









<refsect1>
<title>Description</title>
<para>
Keyed data lists provide lists of arbitrary data elements which can be accessed
either with a string or with a <link linkend="GQuark"><type>GQuark</type></link> corresponding to the
string.
</para>
<para>
The <link linkend="GQuark"><type>GQuark</type></link> methods are quicker, since the strings have to be converted to
<link linkend="GQuarks"><type>GQuarks</type></link> anyway.
</para>
<para>
Data lists are used for associating arbitrary data with
<link linkend="GObjects"><type>GObjects</type></link>, using <link linkend="g-object-set-data"><function>g_object_set_data()</function></link> and related functions.
</para>

<para>
To create a datalist, use <link linkend="g-datalist-init"><function>g_datalist_init()</function></link>.
</para>
<para>
To add data elements to a datalist use <link linkend="g-datalist-id-set-data"><function>g_datalist_id_set_data()</function></link>,
<link linkend="g-datalist-id-set-data-full"><function>g_datalist_id_set_data_full()</function></link>, <link linkend="g-datalist-set-data"><function>g_datalist_set_data()</function></link>
and <link linkend="g-datalist-set-data-full"><function>g_datalist_set_data_full()</function></link>.
</para>
<para>
To get data elements from a datalist use <link linkend="g-datalist-id-get-data"><function>g_datalist_id_get_data()</function></link> and
<link linkend="g-datalist-get-data"><function>g_datalist_get_data()</function></link>.
</para>
<para>
To iterate over all data elements in a datalist use <link linkend="g-datalist-foreach"><function>g_datalist_foreach()</function></link>.
</para>
<para>
To remove data elements from a datalist use <link linkend="g-datalist-id-remove-data"><function>g_datalist_id_remove_data()</function></link> and
<link linkend="g-datalist-remove-data"><function>g_datalist_remove_data()</function></link>.
</para>
<para>
To remove all data elements from a datalist, use <link linkend="g-datalist-clear"><function>g_datalist_clear()</function></link>.
</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GData"/>struct GData</title>
<indexterm><primary>GData</primary></indexterm><programlisting>struct GData;</programlisting>
<para>
The <link linkend="GData"><type>GData</type></link> struct is an opaque data structure to represent a
<link linkend="glib-Keyed-Data-Lists">Keyed Data List</link>.
It should only be accessed via the following functions.
</para></refsect2>
<refsect2>
<title><anchor id="g-datalist-init"/>g_datalist_init ()</title>
<indexterm><primary>g_datalist_init</primary></indexterm><programlisting><link linkend="void">void</link>        g_datalist_init                 (<link linkend="GData">GData</link> **datalist);</programlisting>
<para>
Resets the datalist to <literal>NULL</literal>.
It does not free any memory or call any destroy functions.
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a pointer to a pointer to a datalist.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-id-set-data"/>g_datalist_id_set_data()</title>
<indexterm><primary>g_datalist_id_set_data</primary></indexterm><programlisting>#define     g_datalist_id_set_data(dl, q, d)</programlisting>
<para>
Sets the data corresponding to the given <link linkend="GQuark"><type>GQuark</type></link> id.
Any previous data with the same key is removed, and its
destroy function is called.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>q</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GQuark"><type>GQuark</type></link> to identify the data element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>d</parameter>&nbsp;:</term>
<listitem><simpara>the data element, or <literal>NULL</literal> to remove any previous element
corresponding to <parameter>q</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-id-set-data-full"/>g_datalist_id_set_data_full ()</title>
<indexterm><primary>g_datalist_id_set_data_full</primary></indexterm><programlisting><link linkend="void">void</link>        g_datalist_id_set_data_full     (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id,
                                             <link linkend="gpointer">gpointer</link> data,
                                             <link linkend="GDestroyNotify">GDestroyNotify</link> destroy_func);</programlisting>
<para>
Sets the data corresponding to the given <link linkend="GQuark"><type>GQuark</type></link> id, and the function to
be called when the element is removed from the datalist.
Any previous data with the same key is removed, and its
destroy function is called.
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>key_id</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GQuark"><type>GQuark</type></link> to identify the data element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>data</parameter>&nbsp;:</term>
<listitem><simpara>the data element or <literal>NULL</literal> to remove any previous element
corresponding to <parameter>key_id</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>destroy_func</parameter>&nbsp;:</term>
<listitem><simpara>the function to call when the data element is removed. This
function will be called with the data element and can be used to free any
memory allocated for it. If <parameter>data</parameter> is <literal>NULL</literal>, then <parameter>destroy_func</parameter> must
also be <literal>NULL</literal>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-id-get-data"/>g_datalist_id_get_data ()</title>
<indexterm><primary>g_datalist_id_get_data</primary></indexterm><programlisting><link linkend="gpointer">gpointer</link>    g_datalist_id_get_data          (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id);</programlisting>
<para>
Retrieves the data element corresponding to <parameter>key_id</parameter>. 
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>key_id</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GQuark"><type>GQuark</type></link> identifying a data element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the data element, or <literal>NULL</literal> if it is not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-id-remove-data"/>g_datalist_id_remove_data()</title>
<indexterm><primary>g_datalist_id_remove_data</primary></indexterm><programlisting>#define     g_datalist_id_remove_data(dl, q)</programlisting>
<para>
Removes an element, using its <link linkend="GQuark"><type>GQuark</type></link> identifier.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>q</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GQuark"><type>GQuark</type></link> identifying the data element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-id-remove-no-notify"/>g_datalist_id_remove_no_notify ()</title>
<indexterm><primary>g_datalist_id_remove_no_notify</primary></indexterm><programlisting><link linkend="gpointer">gpointer</link>    g_datalist_id_remove_no_notify  (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GQuark">GQuark</link> key_id);</programlisting>
<para>
Removes an element, without calling its destroy notification function.
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>key_id</parameter>&nbsp;:</term>
<listitem><simpara>the <link linkend="GQuark"><type>GQuark</type></link> identifying a data element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the data previously stored at <parameter>key_id</parameter>, or <literal>NULL</literal> if none.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-set-data"/>g_datalist_set_data()</title>
<indexterm><primary>g_datalist_set_data</primary></indexterm><programlisting>#define     g_datalist_set_data(dl, k, d)</programlisting>
<para>
Sets the data element corresponding to the given string identifier.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>k</parameter>&nbsp;:</term>
<listitem><simpara>the string to identify the data element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>d</parameter>&nbsp;:</term>
<listitem><simpara>the data element, or <literal>NULL</literal> to remove any previous element
corresponding to <parameter>k</parameter>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-set-data-full"/>g_datalist_set_data_full()</title>
<indexterm><primary>g_datalist_set_data_full</primary></indexterm><programlisting>#define     g_datalist_set_data_full(dl, k, d, f)</programlisting>
<para>
Sets the data element corresponding to the given string identifier, and the
function to be called when the data element is removed.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>k</parameter>&nbsp;:</term>
<listitem><simpara>the string to identify the data element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>d</parameter>&nbsp;:</term>
<listitem><simpara>the data element, or <literal>NULL</literal> to remove any previous element corresponding to
<parameter>k</parameter>.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>f</parameter>&nbsp;:</term>
<listitem><simpara>the function to call when the data element is removed. This
function will be called with the data element and can be used to free any
memory allocated for it. If <parameter>d</parameter> is <literal>NULL</literal>, then <parameter>f</parameter> must also be <literal>NULL</literal>.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-get-data"/>g_datalist_get_data()</title>
<indexterm><primary>g_datalist_get_data</primary></indexterm><programlisting>#define     g_datalist_get_data(dl, k)</programlisting>
<para>
Gets a data element, using its string identifer.
This is slower than <link linkend="g-datalist-id-get-data"><function>g_datalist_id_get_data()</function></link> because the string is first
converted to a <link linkend="GQuark"><type>GQuark</type></link>.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>k</parameter>&nbsp;:</term>
<listitem><simpara>the string identifying a data element.
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara>the data element, or <literal>NULL</literal> if it is not found.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-remove-data"/>g_datalist_remove_data()</title>
<indexterm><primary>g_datalist_remove_data</primary></indexterm><programlisting>#define     g_datalist_remove_data(dl, k)</programlisting>
<para>
Removes an element using its string identifier.
The data element's destroy function is called if it has been set.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>k</parameter>&nbsp;:</term>
<listitem><simpara>the string identifying the data element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-remove-no-notify"/>g_datalist_remove_no_notify()</title>
<indexterm><primary>g_datalist_remove_no_notify</primary></indexterm><programlisting>#define     g_datalist_remove_no_notify(dl, k)</programlisting>
<para>
Removes an element, without calling its destroy notifier.
</para><variablelist role="params">
<varlistentry><term><parameter>dl</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>k</parameter>&nbsp;:</term>
<listitem><simpara>the string identifying the data element.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-foreach"/>g_datalist_foreach ()</title>
<indexterm><primary>g_datalist_foreach</primary></indexterm><programlisting><link linkend="void">void</link>        g_datalist_foreach              (<link linkend="GData">GData</link> **datalist,
                                             <link linkend="GDataForeachFunc">GDataForeachFunc</link> func,
                                             <link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Calls the given function for each data element of the datalist.
The function is called with each data element's <link linkend="GQuark"><type>GQuark</type></link> id and data,
together with the given <parameter>user_data</parameter> parameter.
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>func</parameter>&nbsp;:</term>
<listitem><simpara>the function to call for each data element.
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data to pass to the function.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-datalist-clear"/>g_datalist_clear ()</title>
<indexterm><primary>g_datalist_clear</primary></indexterm><programlisting><link linkend="void">void</link>        g_datalist_clear                (<link linkend="GData">GData</link> **datalist);</programlisting>
<para>
Frees all the data elements of the datalist.
The data elements' destroy functions are called if they have been set.
</para><variablelist role="params">
<varlistentry><term><parameter>datalist</parameter>&nbsp;:</term>
<listitem><simpara>a datalist.


</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>




</refentry>
