18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Persistence 283<br />

with <strong>Ada</strong>.Str<strong>in</strong>gs.Unbounded, <strong>Ada</strong>.F<strong>in</strong>alization;<br />

use <strong>Ada</strong>.Str<strong>in</strong>gs.Unbounded, <strong>Ada</strong>.F<strong>in</strong>alization;<br />

generic<br />

type Index is private; --Index for record<br />

type Data is private; --Data for record<br />

with function ">"( F:<strong>in</strong> Index; S:<strong>in</strong> Index ) return Boolean;<br />

package Class_Pic is<br />

Not_There, Ma<strong>in</strong>ists, Per_Error : exception; --Raised Exceptions<br />

type Pic is new Limited_Controlled with private;<br />

procedure Initialize( The:<strong>in</strong> out Pic );<br />

procedure Initialize( The:<strong>in</strong> out Pic; Id:<strong>in</strong> Str<strong>in</strong>g );<br />

procedure F<strong>in</strong>alize( The:<strong>in</strong> out Pic );<br />

procedure Discard( The:<strong>in</strong> out Pic );<br />

procedure Set_Name( The:<strong>in</strong> out Pic; Id:<strong>in</strong> Str<strong>in</strong>g );<br />

function Get_Name( The:<strong>in</strong> Pic ) return Str<strong>in</strong>g;<br />

procedure Add( The:<strong>in</strong> out Pic; I:<strong>in</strong> Index; D:<strong>in</strong> Data );<br />

procedure Extract( The:<strong>in</strong> out Pic; I:<strong>in</strong> Index; D:<strong>in</strong> out Data );<br />

procedure Update( The:<strong>in</strong> out Pic; I:<strong>in</strong> Index; D:<strong>in</strong> out Data );<br />

private<br />

type Leaf;<br />

--Index + Data<br />

type Subtree is access Leaf; --<br />

type Pic is new Limited_Controlled with record<br />

Tree : Subtree := null; -- Storage<br />

Obj_Id : Unbounded_Str<strong>in</strong>g; -- Name of object<br />

end record;<br />

function F<strong>in</strong>d( The:<strong>in</strong> Subtree; I:<strong>in</strong> Index) return Subtree;<br />

procedure Release_Storage( The:<strong>in</strong> out Subtree );<br />

end Class_Pic;<br />

Note:<br />

The declaration of the type leaf is a forward declaration so that the type Subtree, a po<strong>in</strong>ter to a<br />

leaf can be declared. The type leaf is fully declared <strong>in</strong> the package body.<br />

The two generic parameters, Index and Data represent the type of the <strong>in</strong>dex used to access the stored data.<br />

As this generic class will need to compare <strong>in</strong>dices for ">" to establish the position of an <strong>in</strong>dex <strong>in</strong> the b<strong>in</strong>ary tree, a<br />

def<strong>in</strong>ition for ">" must be provided by the user of the class. Remember, the <strong>in</strong>dex may be of a type for which the<br />

operation ">" is not def<strong>in</strong>ed between two <strong>in</strong>stances of Index. This forces the user of the package to provide<br />

implicitly or explicitly an implementation for the comparison function ">".<br />

The implementation of the class Pic uses the data structure Element to hold the <strong>in</strong>dex and the data<br />

associated with the <strong>in</strong>dex. The data structure Leaf represents a leaf of the b<strong>in</strong>ary tree which is composed of a left<br />

and right po<strong>in</strong>ter plus the data structure Element.<br />

with Unchecked_Deallocation, Sequential_Io;<br />

package body Class_Pic is<br />

type Element is record --<br />

S_Index: Index; --The Index<br />

S_Data : Data; --The Data<br />

end record;<br />

type Leaf is record --<br />

Left : Subtree; --Possible left node<br />

Rec : Element; --Index + data<br />

Right : Subtree; --Possible right node;<br />

end record;<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!