comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Re: C++ to Ada95, help please
Date: Thu, 9 Mar 1995 21:23:28 GMT
Date: 1995-03-09T21:23:28+00:00	[thread overview]
Message-ID: <D56zF5.88I@inmet.camb.inmet.com> (raw)
In-Reply-To: 3jjnru$e02@jerry.rb.icl.co.uk

Simon Johnston (skj@rb.icl.co.uk) wrote:
: Hi, I am moving over to Ada95 from C++, I did do some Ada a while ago, and I
: am fairly confident, but I need some help to move some C++ knowledge onto
: Ada95.

: First I would like to declare an Opaque type, or in C++ a 'smiley'.  There
: are two uses for such a feature. 
: ...I include two 
: examples below to illustrate.

: // Start ->
: class ACollectionClass {
: public:
:   ...
: private:
:   class Implementation;
:   Implementation* hidden;
: };

Ada has a very direct equivalent to this:

    package A is
        type Collection is tagged private;
      ...
    private
	type Implementation;    -- This is called an "incomplete type"
			        -- it can only be used as the designated
				-- type for an access type before
				-- it is fully defined.
				-- The full definition may be deferred to the
				-- package body if the incomplete type
				-- definition is in the private part of
				-- the package spec.
        type Impl_Ptr is access Implementation;

        type Collection is tagged record
           B : Impl_Ptr;
        end record;
    end A;

  -- in the body for package A we would give the full definition
  -- of Implementation.

: class Screen {
: public:
:   ...
:   class SavedScreen;
:   const SavedScreen* save(void);
:   void  restore(SavedScreen* image);
: };
: // <- end.

This one is similar to the above, except that you export the access
type as a private type:

    package Screens is
        type SavedScreen is private;
	type Screen is tagged private;
        function Save(S : Screen) return Saved_Screen;
	procedure Restore(S : in out Screen; From : Saved_Screen);
    private
        type SavedScreen_Impl;
        type SavedScreen is access SavedScreen_Impl;

        type Screen is tagged record
           ...
        end record;
    end Screens;

  -- In the body for Screens, we would give the full definition
  -- for SavedScreen_Impl.

: Now I know from experience I can do this sort of thing in C++, Modula-2 and
: Modula-3 but I cannot see how to do it in Ada, even using private types.

As illustrated above, the thing you need to use are access types,
and a designated type which is an incomplete type whose full
type definition is deferred to the package body
(so called "deferred incomplete types")  This feature is
available in both Ada 83 and Ada 95.  

In Ada 95, you have the additional possibility of using abstract 
types and access-to-class-wide, which would allow the completion
of the "impl" types potentially in other packages, but
at least in the above cases, that seems like overkill.

If you are familiar with Modula-2, then an 
"access-to-deferred-incomplete-type" is almost identical
in functionality to Modula-2's "opaque" types.

: |Simon K. Johnston - Development Engineer (C/C++)      |ICL Retail Systems |
: |------------------------------------------------------|3/4 Willoughby Road|
: |Unix Mail : S.K.Johnston@bra0801.wins.icl.co.uk       |Bracknell, Berks   |
: |Telephone : +44 (0)344 476320   Fax: +44 (0)344 476302|United Kingdom     |
: |Internal  : 7261 6320    OP Mail: S.K.Johnston@BRA0801|RG12 8TJ           |

-Tucker Taft  stt@inmet.com
Intermetrics, Inc.



  parent reply	other threads:[~1995-03-09 21:23 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-03-08  7:54 C++ to Ada95, help please Simon Johnston
1995-03-08 13:56 ` Robert A Duff
1995-03-09 21:23 ` Tucker Taft [this message]
  -- strict thread matches above, loose matches on Subject: below --
1995-03-20  8:06 Simon Johnston
1995-03-21 23:27 ` Kevin F. Quinn
1995-03-22  5:07   ` Vladimir Vukicevic
1995-03-22  1:22 ` Tucker Taft
1995-03-22 11:38   ` Robb Nebbe
1995-03-23 12:28   ` Mike Meier
1995-03-23 18:31     ` Tucker Taft
1995-03-24 21:24     ` Robert Dewar
1995-03-27 14:58       ` Norman H. Cohen
1995-03-23 13:30   ` Robert Dewar
1995-03-23 18:01 ` Stephen A. Leake
1995-03-24  9:07   ` Vladimir Vukicevic
1995-03-25 10:02     ` Keith Thompson
replies disabled

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