ada_util_0d266031/src/core/util-stacks.ads

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
-----------------------------------------------------------------------
--  util-stacks -- Simple stack
--  Copyright (C) 2010, 2011, 2013, 2022 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--  SPDX-License-Identifier: Apache-2.0
-----------------------------------------------------------------------
with Ada.Finalization;
generic
   type Element_Type is private;
   type Element_Type_Access is access all Element_Type;
package Util.Stacks is

   pragma Preelaborate;

   type Stack is limited private;

   --  Get access to the current stack element.
   function Current (Container : in Stack) return Element_Type_Access;

   --  Push an element on top of the stack making the new element the current one.
   procedure Push (Container : in out Stack);

   --  Pop the top element.
   procedure Pop (Container : in out Stack);

   --  Clear the stack.
   procedure Clear (Container : in out Stack);

   --  Returns true if the stack is empty.
   function Is_Empty (Container : in out Stack) return Boolean;

   --  Get the access to the stack element at the given position.
   function Get (Container : in Stack;
                 Position  : in Positive) return Element_Type_Access;

   type Element_Type_Array is array (Natural range <>) of aliased Element_Type;

   procedure Read (Container : in Stack;
                   Process   : not null
                     access procedure (Content : in Element_Type_Array));

private
   type Element_Type_Array_Access is access all Element_Type_Array;

   type Stack is new Ada.Finalization.Limited_Controlled with record
      Current : Element_Type_Access := null;
      Stack   : Element_Type_Array_Access := null;
      Pos     : Natural := 0;
   end record;

   --  Release the stack
   overriding
   procedure Finalize (Obj : in out Stack);

end Util.Stacks;