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.

Conta<strong>in</strong>ers 255<br />

When an <strong>in</strong>stance of class List is assigned, only the direct storage conta<strong>in</strong>ed <strong>in</strong> the record list will be<br />

copied. This will not physically duplicate the storage conta<strong>in</strong>ed <strong>in</strong> the list, but only copy the po<strong>in</strong>ters to the list.<br />

When a controlled object is assigned, the procedure Adjust is called after the assignment has been made. The<br />

procedure Adjust is used to perform any additional actions required on an assignment. The exact effect of<br />

assign<strong>in</strong>g a controlled object is as follows:<br />

Assignment of controlled objects<br />

A := B;<br />

Actions that take place on assignment<br />

Anon := B;<br />

Adjust( Anon );<br />

F<strong>in</strong>alize( A );<br />

A := Anon;<br />

Adjust( a );<br />

F<strong>in</strong>alize( Anon );<br />

Action on assignment<br />

Anon := B<br />

Adjust(Anon);<br />

F<strong>in</strong>alize(A);<br />

A := Anon;<br />

Adjust(A);<br />

F<strong>in</strong>alize(Anon);<br />

Commentary<br />

Make a temporary anonymous copy anon.<br />

Adjustments required to be made after copy<strong>in</strong>g the<br />

direct storage of the source object B to Anon.<br />

F<strong>in</strong>alize the target of the assignment.<br />

Perform the physical assignment of the direct<br />

components of the anon object.<br />

Adjustments required to be made after copy<strong>in</strong>g the<br />

direct storage of the Anon object.<br />

F<strong>in</strong>alize the anonymous object Anon.<br />

Note: If the object’s storage does not overlap, which will be the usual case, then the compiler may implement<br />

the follow<strong>in</strong>g optimization:<br />

F<strong>in</strong>alize(A); A := B; Adjust(A);<br />

Look at the effect of the assignment A := A; to see why this optimization may not be performed when<br />

the object’s storage overlaps.<br />

If the source and target are the same, then the operation may be skipped.<br />

The procedure Adjust is used to create a new duplicate copy of the storage <strong>in</strong> the list. The contents of the<br />

target <strong>in</strong> the assignment are updated to po<strong>in</strong>t to this newly created copy. Hence, there are now two identical copies<br />

of the l<strong>in</strong>ked list:<br />

• The l<strong>in</strong>ked list <strong>in</strong> the Target.<br />

• The l<strong>in</strong>ked list <strong>in</strong> the Source .<br />

procedure Adjust( The:<strong>in</strong> out List ) is<br />

Cur : P_Node := The.First_Node; --Orig<strong>in</strong>al list<br />

Lst : P_Node := null; --Last created node<br />

Prv : P_Node := null; --Previously created node<br />

Fst : P_Node := null; --The first node<br />

beg<strong>in</strong><br />

while Cur /= null loop<br />

Lst := new Node'( Prv, Cur.Item, null );<br />

if Fst = null then Fst := Lst; end if;<br />

if Prv /= null then Prv.Next := Lst; end if;<br />

Prv := Lst;<br />

Cur := Cur.Next; --Next node<br />

end loop;<br />

The.First_Node := Fst; --Update<br />

The.Last_Node := Lst;<br />

end Adjust;<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!