Description
This package implements a generic double-linked list.
Such lists are used throughout GtkAda to contain lists of widgets
(for the children of containers, or for the list of selected widgets
in a Gtk_Clist for instance), list of strings (for Gtk_Combo_Box),...
They provide a common interface to traverse these lists.
One useful note: you should only Free the lists that you have allocated
yourself, and not the lists that are returned by the subprograms in
GtkAda and should be left under GtkAda's control.
See the example below for an example on how to traverse a list.
Instantiating the package Generic_List requires two functions to convert
back and forth between your data type and a System.Address which is the
type stored at the C level.
Note that the lists used in GtkAda already have associated packages, like
Gtk.Enums.Gint_List, Gtk.Enums.String_List or Gtk.Widget.Widget_List.
Subprograms
-
procedure Alloc (List : out Glist);
-
procedure Append (List : in out Glist; Data : Gpointer);
-
function Concat (List1 : Glist; List2 : Glist) return Glist;
-
procedure Insert
(List : in out Glist;
Data : Gpointer;
Position : Gint);
-
function Find (List : Glist; Data : Gpointer) return Glist;
-
function First (List : Glist) return Glist;
-
procedure Free (List : in out Glist);
-
function Get_Data (List : Glist) return Gpointer;
-
function Get_Data_Address (List : Glist) return System.Address;
-
function Index (List : Glist; Data : Gpointer) return Gint;
-
function Last (List : Glist) return Glist;
-
function Length (List : Glist) return Guint;
-
procedure List_Reverse (List : in out Glist);
-
function Next (List : Glist) return Glist;
-
function Nth (List : Glist; N : Guint) return Glist;
-
function Nth_Data (List : Glist; N : Guint) return Gpointer;
-
function Position (List : Glist; Link : Glist) return Gint;
-
procedure Prepend (List : in out Glist; Data : Gpointer);
-
function Prev (List : Glist) return Glist;
-
procedure Remove (List : in out Glist; Data : Gpointer);
-
procedure Remove_Link (List : in out Glist; Link : Glist);
-
function Is_Created (List : Glist) return Boolean;
Example
with Glib; use Glib;
with Gtk.Enums;
with Ada.Text_IO; use Ada.Text_IO;
procedure Glist_Traverse is
use Gtk.Enums.Gint_List;
List : Gtk.Enums.Gint_List.Glist;
Temp : Gtk.Enums.Gint_List.Glist;
begin
First step: create a new list.
Prepend (List, 2); -- add at the beginning of the list
Append (List, 3); -- add at the end of the list
Insert (List, Data => 1, Position => 1); -- in the middle of the list
Traverse the list (first way)
Temp := First (List);
while Temp /= Null_List loop
Put_Line (Gint'Image (Get_Data (Temp)));
Temp := Next (Temp);
end loop;
Traverse the list (second way)
for I in 1 .. Length (List) loop
Put_Line (Gint'Image (Nth_Data (List, I - 1)));
end loop;
end Glist_Traverse;