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.
Types |
---|
type Glist is private; | |
This type is both a list and an item in the list.
Each item points to its successor.
|
Subprograms |
---|
procedure Alloc (List : out Glist); | ||
Allocate a new item in the list. | ||
procedure Append (List : in out Glist; Data : Gpointer); | ||
Add a new item at the end of the list, and stores the new list | ||
function Concat (List1 : Glist; List2 : Glist) return Glist; | ||
Concatenate two lists, and return the result. | ||
procedure Insert (List : in out Glist; Data : Gpointer; Position : Gint); | ||
Insert an item in the middle of a list. | ||
function Find (List : Glist; Data : Gpointer) return Glist; | ||
Find a value in the list, and return the first item that contains it. | ||
function First (List : Glist) return Glist; | ||
Return the first item in the list. | ||
procedure Free (List : in out Glist); | ||
Free the list (but does not free the data in each of its elements).
Note also that the memory might not be actually freed. For efficiency
reasons, GtkAda will keep the memory allocated and try to reuse it as
much as possible.
| ||
function Get_Data (List : Glist) return Gpointer; | ||
Return the value pointed to by List. | ||
function Get_Data_Address (List : Glist) return System.Address; | ||
Return directly the System.Address contained in the C list. | ||
function Index (List : Glist; Data : Gpointer) return Gint; | ||
Return the index of the first element in List that contains Data. | ||
function Last (List : Glist) return Glist; | ||
Return the last element in the list.
| ||
function Length (List : Glist) return Guint; | ||
Return the number of elements in the list. | ||
procedure List_Reverse (List : in out Glist); | ||
Reverse the order of the list (the last item becomes the first, etc.)
| ||
function Next (List : Glist) return Glist; | ||
Returns the Item following List in the global list that contains | ||
function Nth (List : Glist; N : Guint) return Glist; | ||
Give the nth item following LIST in the global list that | ||
function Nth_Data (List : Glist; N : Guint) return Gpointer; | ||
Return the Data contained in the N-th item of List. | ||
function Position (List : Glist; Link : Glist) return Gint; | ||
Return the position of Link in the List. | ||
procedure Prepend (List : in out Glist; Data : Gpointer); | ||
Add an item at the beginning of the list. | ||
function Prev (List : Glist) return Glist; | ||
Return the item before List in the global list that contains both. | ||
procedure Remove (List : in out Glist; Data : Gpointer); | ||
Remove the first item in List that contains Data. | ||
procedure Remove_Link (List : in out Glist; Link : Glist); | ||
Remove Link from the list to which it belongs. | ||
function Is_Created (List : Glist) return Boolean; | ||
Return True if there is a C widget associated with List.
|
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;