comp.lang.ada
 help / color / mirror / Atom feed
From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: Exposing agreggate members of a class
Date: 1999/01/06
Date: 1999-01-06T00:00:00+00:00	[thread overview]
Message-ID: <m3yanhvwqh.fsf@mheaney.ni.net> (raw)
In-Reply-To: 76cd39$21c8$1@news.gate.net

"David Botton" <dbotton@hotmail.com> writes:

> Given:
> 
> with GCanvas_Package; use GCanvas_Package;
> 
> type GWindow_Object is tagged with private;
> type GWindow_Pointer is access all GWindow_Object;
> 
> private:
> 
> type GWindow_Object is tagged
>    record
>         Canvas : GCanvas_Object;
>    end record;
> 
> What is the cleanest way to give access to the Canvas by reference in a way
> that will not require Unchecked Programming
> 
> What I don't want:
> 
>     function Get_Canvas( window : in GWindow_Object ) return GCanvas_Pointer
> is
>     begin
>         return window.Canvas'Unchecked_Access;
>     end Get_Canvas;

Then try passing the window object as an access parameter:

  
private

type GWindow_Object is tagged
   record
        Canvas : aliased GCanvas_Object;
   end record;

   function Get_Canvas
     (Window : access GWindow_Object)
     return GCanvas_Pointer is
   begin
     return Window.Canvas'Access;
   end;

end;


Alternatively, you could make Canvas a public attribute of
GWindow_Object.  It's a more direct way of doing what you were trying to
do:


package Windows is

   type GCanvas_Object is new Integer;

   type GWindow_Object_Public is
     abstract tagged record
        Canvas : aliased GCanvas_Object;
     end record;

   type GWindow_Object is
     new GWindow_Object_Public with private;

   type GWindow_Pointer is
      access all GWindow_Object;

private

   type GWindow_Object is
     new GWindow_Object_Public with null record;

end Windows;



 
> Which on top of the Unchecked_Access issue means that consumers of such a
> class would have to dereference first to use the GCanvas_Object methods:
> 
> declare
>     Canvas : GCanvas_Pointer := Get_Canvas( window );
> begin
>     Draw_Line(Canvas.all, 10, 10, 100, 100);
> end;
> 
> Of course you could write the methods to accept GCanvas_Pointer's, but I
> want to avoid the use of Access variables when possible.

Do a deference at the point of invocation of the selector:

  declare
    Canvas : GCanvas_Object renames Get_Canvas (Window'Access).all;
  begin
    Draw_Line (Canvas, ...);
  end;

(To avoid any unchecked programming, you'll have to pass the window as an
access parameter.)






      parent reply	other threads:[~1999-01-06  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-12-30  0:00 Exposing agreggate members of a class David Botton
1998-12-30  0:00 ` Tom Moran
1998-12-31  0:00 ` LeakyStain
1998-12-31  0:00   ` David Botton
1999-01-03  0:00 ` Simon Wright
1999-01-06  0:00 ` Matthew Heaney [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