comp.lang.ada
 help / color / mirror / Atom feed
* Using 'Access in a limited type declaration as an initialization hook
@ 2001-08-09  1:23 Michael P. Card
  2001-08-09  8:53 ` Egil Harald Hoevik
  0 siblings, 1 reply; 2+ messages in thread
From: Michael P. Card @ 2001-08-09  1:23 UTC (permalink / raw)
  Cc: michael.p.card

[-- Attachment #1: Type: text/plain, Size: 2501 bytes --]

Hello CLA-

Many of you Ada vets out there may know about this already, but I have
worked with Ada for years and never knew it was possible. What I am
referring to is the ability to use the 'Access attribute on a TYPE in a
limited type declaration. For example:

   type My_Obj_Type is abstract tagged limited private;

private

   -- A special initialization function that returns the initialization
value for one of an object's
   -- attributes, could also as a side effect initialize all of the
other fields or do other things
   function Special_Init_Function (The_Object : access My_Obj_Type)
return My_Type;

   type My_Obj_Type is abstract tagged limited
      record
         Attr_1 : My_Type := Special_Init_Function(The_Object =>
My_Obj_Type'Access);
         -- more attributes
      end record;

This will cause Special_Init_Function to be invoked whenever an instance
of any type derived from My_Obj_Type is created, and the object passed
in to the function will be the newly-created instance! You don't need to
use abstract tagged types with this, but I showed this as an example
because it seemed useful.

I don't know about the rest of you, but I always thought that the only
way to get an "initialization hook" that would be called whenever an
object was created was to use the Ada.Finalization package. Annex K of
the LRM does not indicate that 'Access can be used against a subtype,
and even Norm Cohen's excellent book "Ada as a Second Language" does not
mention this as far as I could tell.

I was informed about it during an e-mail dialogue with our support folks
at Green Hills. The only thing I could find in the LRM that indicated
that something like this would work was LRM 3.10.2 paragraph 24:
"X'Access yields an access value that designates the object denoted by
X. The type of X'Access is an access-to-object type, as determined by
the expected type. The expected type shall be a general access type. X
shall denote an aliased view of an object, including possibly the
current instance (see 8.6) of a limited type within its definition, ..."

I think this is a very powerful and handy capability, as it allows you
have an initialization hook without using controlled types. I was
investigating this as part of our work on the Real-time Object Database
with Extensions to ODMG (RODEO) project, which is an Ada95 real-time
object-oriented database manager we (LMC) are using as part of the
AN/SLY-2 (AIEWS) system.

Hope someone else finds this as useful as I did!

- Mike

[-- Attachment #2: Card for Michael P. Card --]
[-- Type: text/x-vcard, Size: 369 bytes --]

begin:vcard 
n:Card;Michael
tel;fax:315-456-1680
tel;work:315-456-3022
x-mozilla-html:TRUE
org:Lockheed Martin ;Naval Electronics & Surveillance Systems - Syracuse
version:2.1
email;internet:michael.p.card@lmco.com
title:Principal Software Engineer
adr;quoted-printable:;;Electronics Park=0D=0ABuilding 7, Room C172 MD42;Syracuse;NY;13221;USA
fn:Michael Card
end:vcard

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Using 'Access in a limited type declaration as an initialization hook
  2001-08-09  1:23 Using 'Access in a limited type declaration as an initialization hook Michael P. Card
@ 2001-08-09  8:53 ` Egil Harald Hoevik
  0 siblings, 0 replies; 2+ messages in thread
From: Egil Harald Hoevik @ 2001-08-09  8:53 UTC (permalink / raw)


"Michael P. Card" wrote:
> 
<snip>
> 



An even more interesting and useful issue, is the ability to hook a task
to an object in more or less the same way:

type Object;

task type thread( Self : access Object );

type Object is 
record
  t : thread( Object 'access );
  seconds : integer := 0;
end record;

...

task body thread is
begin
  loop
  Self.seconds := Self.seconds + 1;
  delay 1.0;
  end loop;
end thread;



That way, the task is hooked up to the object, and can access the
object's 
members.

~egilhh
-- 
"What I seek is to serve, with my feeble capacity, 
truth and justice at the risk of pleasing no-one."
Albert Einstein.



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2001-08-09  8:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-08-09  1:23 Using 'Access in a limited type declaration as an initialization hook Michael P. Card
2001-08-09  8:53 ` Egil Harald Hoevik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox