1-- Object-oriented generalized stack. This illustrates the use of a 2-- controlled type, which is one that has construction and destructions. 3-- It also shows how to create two related types in the same package so 4-- that they can share private declarations. This sort of thing is 5-- accomplished in Java or C++ using nested classes, or using friend 6-- declarations in C++. 7-- 8with Ada.Finalization; use Ada.Finalization; 9 10package GenStack is 11 -- This is the stack type. 12 type Stack is new Controlled with private; 13 14 -- This is the base type for nodes. Client packages must derive their 15 -- nodes from StackData. Since it comes from Controlled, the user can 16 -- override the Initialize, Adjust, and Finalize methods as needed. 17 type StackData is new Controlled with null record; 18 19 -- Initialization operations. 20 procedure Initialize(S: in out Stack); 21 procedure Adjust(S: in out Stack); 22 procedure Finalize(S: in out Stack); 23 24 -- Stack operations. 25 procedure Push(S: in out Stack; D: StackData'class); 26 procedure Pop(S: in out Stack; D: in out StackData'class); 27 procedure Top(S: Stack; Data: in out StackData'class); 28 function Empty(S: Stack) return Boolean; 29 30 private 31 -- Pointer to the node type. 32 type Node; 33 type Node_Ptr is access Node; 34 35 -- Here is the generalized stack itself. We would just make it the 36 -- pointer itself, but it needs to be a record so it can be in a with. 37 type Stack is new Controlled with record 38 Head: Node_Ptr; 39 end record; 40 41 -- Now, we need a pointer to the data part. 42 type Data_Ptr is access StackData'Class; 43 44 -- This is the node type. 45 type Node is record 46 Data: Data_Ptr; 47 Next: Node_Ptr; 48 end record; 49 50end GenStack; 51