From: Brad Moore <brad.moore@shaw.ca>
Subject: Re: Proper program structure
Date: Fri, 02 Oct 2009 07:10:43 -0600
Date: 2009-10-02T07:10:43-06:00 [thread overview]
Message-ID: <DZmxm.23668$tG1.21868@newsfe22.iad> (raw)
In-Reply-To: <6rgxm.83139$u76.321@newsfe10.iad>
Sorry, need to make a correction to my last posting.
The automatic finalization would not have worked like I would have
wanted, since the vehicle_type is an access type, finalization would
have only occurred when the access type went out of scope (at program
finalization). To get the effect I was looking for would have required
garbage collection. To correct this, I make the Vehicle_Type a
controlled record type containing an access to the internals.
I got rid of the inheritance, since it did not seem to be providing
much of a benefit.
Brad
i.e.
private with Ada.Finalization;
package Cars is
type Vehicle_Type (<>) is limited private;
function Construct return Vehicle_Type;
private
use Ada.Finalization;
type Car_Type;
type Car_Access_Type is access Car_Type;
type Vehicle_Type is new Limited_Controlled with
record
Car : Car_Access_Type;
end record;
overriding procedure Finalize (Vehicle : in out Vehicle_Type);
end Cars;
---------------------------
with Cars.Vehicle_Internal;
use Cars.Vehicle_Internal;
with Ada.Unchecked_Deallocation;
package body Cars is
type Car_Type is
record
Internals : Vehicle_Internal_Type;
end record;
procedure Free_Car is new Ada.Unchecked_Deallocation
(Object => Car_Type,
Name => Car_Access_Type);
function Construct return Vehicle_Type is
begin
return Vehicle : Vehicle_Type
do
Vehicle.Car :=
new Car_Type'(Internals => Vehicle_Internal_Type'(Construct));
return;
end return;
end Construct;
overriding procedure Finalize (Vehicle : in out Vehicle_Type) is
begin
Free_Car (Vehicle.Car);
end Finalize;
end Cars;
next prev parent reply other threads:[~2009-10-02 13:10 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-09-30 21:25 Proper program structure Maciej Sobczak
2009-09-30 22:16 ` Robert A Duff
2009-10-01 7:13 ` Maciej Sobczak
2009-09-30 23:43 ` Adam Beneschan
2009-10-01 7:35 ` Maciej Sobczak
2009-10-01 6:34 ` Brad Moore
2009-10-01 7:44 ` Maciej Sobczak
2009-10-01 9:39 ` Maciej Sobczak
2009-10-01 15:36 ` Brad Moore
2009-10-01 20:01 ` Maciej Sobczak
2009-10-02 5:44 ` Brad Moore
2009-10-02 13:10 ` Brad Moore [this message]
2009-10-01 8:08 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox