comp.lang.ada
 help / color / mirror / Atom feed
From: Richard Riehle <richard@adaworks.com>
Subject: Re: What's it's name again?
Date: Tue, 30 Jul 2002 11:55:54 -0700
Date: 2002-07-30T18:51:26+00:00	[thread overview]
Message-ID: <3D46E13A.10D50FDF@adaworks.com> (raw)
In-Reply-To: wcclm7ugkpx.fsf@shell01.TheWorld.com

The question about completing an incomplete type in the package
body has lots of interesting history.   The name is, opaque type, and
it comes from Modula-2.

If Ada had followed the Modula-2 language design, there would
be no private part in the package specification (Modula-2's Definition
Module).   All complete type definitions would be in the package
body (Implementation Module).

This design decsion created a problem for Modula-2 when trying
to do separate compilations.   The Modula-2 solution was to create
"opaque types" that were referenced through a pointer mechanism.
Modula-3 continues to follow some of the Modula-2 rules but simplifies
this feature with some new language syntax.

Ada's solution was to include a private part in the specification.  This
allowed the compiler developers enough information to do separate
compilation at the specification level without resorting to opaque
types.

The example below, contributed by Robert Duff, is an example of
creating an opaque type in Ada.    This idiom is becoming more and
more common in reusable components at some of our client sites.

With Ada 83, opaque types were not too problematic.   With the
need for extensible types in Ada 95 a new problem arose: how
to create extensible opaque types?   There is a solution to this
problem, but it requires additional levels of indirection.  I am
including some sample code at the end of this message for
anyone who wants to play with it.



Robert A Duff wrote:

> "chris.danx" <spamoff.danx@ntlworld.com> writes:
>
> > What do you call private types whose declaration is found in the
> > specification but whose definition is found in the body?  I remember
> > reading/hearing that they have a name (or term) associated with them,
> > but can't remember what it is.
>
> That's not quite legal.  I think you mean a private type completed by an
> access type, where the access type points to an incomplete type
> completed in the body.  Like this:
>
>     package P is
>         type T is private;
>     private
>         type R; -- completed in body
>         type T is access R;
>     end P;
>
>     package body P is
>         type R is
>             record
>                 ...
>
> I call R an "stt incomplete type", and I call T an "stt access type",
> named after Tucker Taft (initials "stt"), who invented them around 1982
> or so, just before Ada 83 was standardized.  Jean Ichbiah called them
> "Taft Amendment Types".
>
> > Are there any complications with moving type definitions to the body?
> > (e.g. with controlled types?).
>
> None other than the fact that you have to have a pointer type involved.
> If I need a pointer type anyway, then I use stt access types, but I
> don't normally introduce *extra* pointers just to be able to use this
> feature.
>
> Introducing extra pointers causes the obvious problems: how do you
> allocate and deallocate the pointed-to things?
>
> > I currently have some packages which look like
> >
> >    package some_kind_of_data_structure is
> >
> >      type data_structure is private;
> >
> >    private
> >
> >      type data_structure is Ada.Finalized.Controlled with record
> >        ...
> >      end record;
> >
> >    end some_data_structure;
> >

From Richard Riehle.

This code will compile, but we don't have it totally
implemented in this sample.    You can implement
the Controlled type procedures as you wish, and don't
forget to handle potential memory leaks.


First we show the package body.

-- Example of an Opaque Controlled Type
-- Author:  Richard Riehle,   AdaWorks Software Engineering
-- Permission to use this as you wish for any purpose at all.
with Ada.Finalization;
use  Ada;
package Opaque_Tagged_Type is

   type Opaque is tagged private;

   procedure Create     (Data : in out Opaque);

private

  type Opaque_Data;
  type Data_Reference is access all Opaque_Data;
  type Opaque is new Ada.Finalization.Controlled with
     record
        Value : Data_Reference;
     end record;

  procedure Initialize(Data : in out Opaque);
  procedure Finalize  (Data : in out Opaque);
  procedure Adjust    (Data : in out Opaque);

end Opaque_Tagged_Type;

-- ======================================================

Now we provide the stubbed-out code for the package body.

package body Opaque_Tagged_Type is

   type Opaque_Data is new Ada.Finalization.Controlled with record
       X : Integer;
       Y : Integer;
   end record;

   procedure Initialize(Data : in out Opaque_Data);
   procedure Finalize  (Data : in out Opaque_Data);
   procedure Adjust    (Data : in out Opaque_Data);

   procedure Create(Data : in out Opaque) is
      Temp : Data_Reference := new Opaque_Data'(Ada.Finalization.Controlled
                                                with X => 0, Y => 0);
   begin
      Data.Value := Temp;
   end Create;


   procedure Initialize (Data : in out Opaque) is
   begin
      Data.Value.X := 0;
      Data.Value.Y := 0;
   end Initialize;

   procedure Finalize  (Data : in out Opaque)is
   begin
      null;
   end Finalize;


   procedure Adjust    (Data : in out Opaque)is
   begin
      null;
   end Adjust;

      procedure Initialize (Data : in out Opaque_Data) is
   begin
      null;
   end Initialize;

   procedure Finalize  (Data : in out Opaque_Data)is
   begin
      null;
   end Finalize;


   procedure Adjust    (Data : in out Opaque_Data)is
   begin
      null;
   end Adjust;

end Opaque_Tagged_Type;








      parent reply	other threads:[~2002-07-30 18:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-29 23:13 What's it's name again? chris.danx
2002-07-29 23:32 ` Larry Elmore
2002-07-30  0:08   ` chris.danx
2002-07-29 23:47 ` Robert A Duff
2002-07-30  0:50   ` chris.danx
2002-07-30  8:21   ` Dmitry A. Kazakov
2002-07-30 18:53     ` Robert A Duff
2002-08-01  0:11       ` Dmitry A.Kazakov
2002-07-31 20:38         ` Robert A Duff
2002-08-02  2:21           ` Dmitry A.Kazakov
2002-07-30 18:55   ` Richard Riehle [this message]
replies disabled

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