* Constructors in Ada95
@ 2000-02-21 0:00 Andy Askey
2000-02-22 0:00 ` David
2000-02-22 0:00 ` Simon Wright
0 siblings, 2 replies; 3+ messages in thread
From: Andy Askey @ 2000-02-21 0:00 UTC (permalink / raw)
Hello,
I am sure this has been asked/answered several hundred times but
nothing current appears in DejaNews.
I want to call a procedure each time an object is instantiated for a
specific class/type and pass the current instance into the procedures.
(I could call the procedure after the object is instantiated, but it
will be easier on the user of my class/type if the procedure is
automatically called after instantiation.) I looked into using a
Controlled type, but I still cannot figure out how to work on a
specific object instance's data.
For example (possible syntax errors included)...
package pkg is
type type1 is tagged private;
private
type type1 is tagged
record
i : integer := 0;
end record;
end pkg;
package body pkg is
...
begin
Proc_To_Call( This_Current_Instance : in Type1'class);
end pkg;
Any tips will be useful as I cannot find any info about this in any of
my Ada books. I have "Ada(95) as a Second Language", so if you know
of a section that discusses C++ style constructors, please help me out
with the page numbers. Also, any info on the web that deals with this
would be helpful. I do not mind reading, but I don't have a lot of
time to figure out what it is I should read to answer my immediate
question.
Thanx.
--
----------------------------------------------------
| Andy Askey |
| Software Engineer |
| Raytheon Electronic Systems |
| 353 James Record Road, Huntsville, AL 35824 |
| Phone: (256) 772-4846 Fax: (256) 772-4771 |
| andrew_j_askey@res.raytheon.com |
----------------------------------------------------
--
Andy Askey
ajaskey@mindspring.com
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Constructors in Ada95
2000-02-21 0:00 Constructors in Ada95 Andy Askey
@ 2000-02-22 0:00 ` David
2000-02-22 0:00 ` Simon Wright
1 sibling, 0 replies; 3+ messages in thread
From: David @ 2000-02-22 0:00 UTC (permalink / raw)
To: ajaskey
In article <h414bsc31c83a96l429kfeflk8rsfbk7fq@4ax.com>,
Andy Askey <ajaskey@mindspring.com> wrote:
> Hello,
> I am sure this has been asked/answered several hundred times but
> nothing current appears in DejaNews.
>
> I want to call a procedure each time an object is instantiated for a
> specific class/type and pass the current instance into the procedures.
> (I could call the procedure after the object is instantiated, but it
> will be easier on the user of my class/type if the procedure is
> automatically called after instantiation.) I looked into using a
> Controlled type, but I still cannot figure out how to work on a
> specific object instance's data.
>
Here is a fairly complete example:
with Ada.Finalization;
package Constructor_Example is
type String_Access is access all String;
type Example
(Initial_Name : String_Access;
Initial_Value : Integer)
is new Ada.Finalization.Limited_Controlled with private;
function Image (Object : Example) return String;
procedure Copy (Object : in out Example; From : Example);
procedure Set (Object : in out Example; Name : String);
procedure Set (Object : in out Example; Value : Integer);
private
type Example
(Initial_Name : String_Access;
Initial_Value : Integer)
is new Ada.Finalization.Limited_Controlled with record
Name : String_Access;
Value : Integer;
end record;
procedure Initialize (Object : in out Example);
procedure Finalize (Object : in out Example);
end Constructor_Example;
with Ada.Text_IO;
with Ada.Strings.Unbounded;
with Ada.Unchecked_Deallocation;
package body Constructor_Example is
procedure Free is new Ada.Unchecked_Deallocation
(Object => String,
Name => String_Access);
----------
-- Copy --
----------
procedure Copy (Object : in out Example; From : Example)
is
begin
Free (Object.Name);
Object.Name := new String '(From.Name.all);
Object.Value := From.Value;
end Copy;
-----------
-- Image --
-----------
function Image (Object : Example) return String is
Unbounded_String : Ada.Strings.Unbounded.Unbounded_String;
begin
if Object.Name = null then
Ada.Strings.Unbounded.Append
(Source => Unbounded_String,
New_Item => "Object is un-named;");
else
Ada.Strings.Unbounded.Append
(Source => Unbounded_String,
New_Item => Object.Name.all);
end if;
Ada.Strings.Unbounded.Append
(Source => Unbounded_String,
New_Item => " has the value" &
Integer'Image (Object.Value));
return Ada.Strings.Unbounded.To_String (Unbounded_String);
end Image;
---------
-- Set --
---------
procedure Set (Object : in out Example; Name : String) is
begin
Free (Object.Name);
Object.Name := new String '(Name);
end Set;
---------
-- Set --
---------
procedure Set (Object : in out Example; Value : Integer) is
begin
Object.Value := Value;
end Set;
----------------
-- Initialize --
----------------
procedure Initialize (Object : in out Example)
is
begin
if Object.Initial_Name /= null then
Object.Name := new String '(Object.Initial_Name.all);
end if;
Object.Value := Object.Initial_Value;
Ada.Text_IO.Put_Line
("Initialized object """ & Image (Object) & """.");
end Initialize;
--------------
-- Finalize --
--------------
procedure Finalize (Object : in out Example)
is
begin
Ada.Text_IO.Put_Line
("Finalizing object """ & Image (Object) & """.");
Free (Object.Name);
end Finalize;
end Constructor_Example;
with Ada.Text_IO;
with Constructor_Example;
procedure Test_Constructor_Example is
Instance_1_Initial_Name : aliased String := "Instance_1";
Instance_1 : Constructor_Example.Example
(Instance_1_Initial_Name'Unchecked_Access, 1234);
begin
declare
Instance_2 : Constructor_Example.Example
(null, 4321);
begin
Ada.Text_IO.Put_Line (Constructor_Example.Image (Instance_2));
Constructor_Example.Set (Instance_2, "INSTANCE_2");
Constructor_Example.Set (Instance_2, 5678);
Ada.Text_IO.Put_Line (Constructor_Example.Image (Instance_2));
Constructor_Example.Copy (Instance_2, Instance_1);
Ada.Text_IO.Put_Line
("Image of Instance_2, after Instance_1 has been copied to
it:");
Ada.Text_IO.Put_Line (Constructor_Example.Image (Instance_2));
Ada.Text_IO.Put_Line ("Leaving inner scope.");
end;
Ada.Text_IO.Put_Line (Constructor_Example.Image (Instance_1));
Ada.Text_IO.Put_Line ("Leaving outer scope.");
end Test_Constructor_Example;
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Constructors in Ada95
2000-02-21 0:00 Constructors in Ada95 Andy Askey
2000-02-22 0:00 ` David
@ 2000-02-22 0:00 ` Simon Wright
1 sibling, 0 replies; 3+ messages in thread
From: Simon Wright @ 2000-02-22 0:00 UTC (permalink / raw)
Andy Askey <ajaskey@mindspring.com> writes:
> I am sure this has been asked/answered several hundred times but
> nothing current appears in DejaNews.
Perhaps because there's a pretty full discussion in the LRM/Rationale!
If you don't have access, try www.adapower.com ..
with Ada.Finalization;
> package pkg is
> type type1 is tagged private;
type Type1 is new Ada.Finalization.Controlled with private;
> private
> type type1 is tagged
> record
> i : integer := 0;
> end record;
type Type1 is new Ada.Finalization.Controlled with record
I : Integer := 0;
end record;
procedure Initialize (T : in out Type1);
> end pkg;
>
> package body pkg is
> ...
procedure Initialize (T : in out Type1) is
begin
..
end Initialize;
> begin
> Proc_To_Call( This_Current_Instance : in Type1'class);
-- no, leave this out.
> end pkg;
Initialize gets called whenever an object of type Type1 is created.
See also Adjust and Finalize.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2000-02-22 0:00 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-21 0:00 Constructors in Ada95 Andy Askey
2000-02-22 0:00 ` David
2000-02-22 0:00 ` Simon Wright
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox