comp.lang.ada
 help / color / mirror / Atom feed
* Re: Exposing agreggate members of a class
  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
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Tom Moran @ 1998-12-30  0:00 UTC (permalink / raw)


>What is the cleanest way to give access to the Canvas
Make it not private.
Do you want it private, or do you not want it private?  Or do you want
to make it private to the hoi polloi but public to a few select other
packages, which could be child packages.




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

* Exposing agreggate members of a class
@ 1998-12-30  0:00 David Botton
  1998-12-30  0:00 ` Tom Moran
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: David Botton @ 1998-12-30  0:00 UTC (permalink / raw)


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;

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.

Thanks,
David Botton







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

* Re: Exposing agreggate members of a class
  1998-12-31  0:00 ` LeakyStain
@ 1998-12-31  0:00   ` David Botton
  0 siblings, 0 replies; 6+ messages in thread
From: David Botton @ 1998-12-31  0:00 UTC (permalink / raw)


My question was more on how can I pass a private member out of the class be
reference.

I can pass by reference to call back functions or class wide dispatched methods
using in out mode. I was curious if there are any other times that Ada passes by
reference.

David Botton

LeakyStain wrote:

> Hope this helps.
>
> -- Stephe







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

* Re: Exposing agreggate members of a class
  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
  3 siblings, 1 reply; 6+ messages in thread
From: LeakyStain @ 1998-12-31  0:00 UTC (permalink / raw)


David Botton wrote:
> 
> 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

Depends on how much "acess" the clients need. If they can destroy
Canvas, then it should not be private.

Windex (http://www.erols.com/leakstan/stephe/index.html) solves a
similar problem; shared access to a Win32 Device Context. The approach I
take is to declare the basic Device Context operations to take a
class-wide object parameter:

package Windex.Graphics_Devices is

   type Handle_Type is new Root_Handle_Type with private;

   procedure BitBlt
      (Dest_Image      : in Handle_Type'class;
       <other params>);

   ... other operations
end package Windex.Graphics_Devices;

Root_Handle_Type is derived from Ada.Finalization.Limited_Controlled.
Then, in a child package, declare a type that takes a Windex.Windows
object as a discriminant:

package Windex.Graphics_Devices.Windows is

   type Handle_Type (Owner : access Windex.Windows.Handle_Type'class) is
new
      Windex.Graphics_Devices.Handle_Type with null record;
   -- Handle_Type can be used with all Windex.Graphics_Devices
operations.

   procedure Initialize (Handle : in out Handle_Type);
   -- Get the device context for Window. This locks the window for
drawing;
   -- after drawing, Handle must be finalized to allow normal Window
   -- painting.

   procedure Finalize (Handle : in out Handle_Type);
   -- Release window device context.

end Windex.Graphics_Devices.Windows;

This allows any Windex.Windows object to gain temporary exclusive access
to the Device_Context. All Paint operations in a window procedure use a
Windex.Graphics_Devices.Windows object to gain lock the Device_Context
for drawing.

Hope this helps.

-- Stephe




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

* Re: Exposing agreggate members of a class
  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
@ 1999-01-03  0:00 ` Simon Wright
  1999-01-06  0:00 ` Matthew Heaney
  3 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 1999-01-03  0:00 UTC (permalink / raw)


"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

You could use a generic:

  generic
    with procedure process (c : in out gcanvas_object);
  procedure access_canvas (g : gwindow_object);

(actually, I suspect that should be (g : in out gwindow_object))

NB, I'm not sure you actually mean 'by reference' here? see LRM 6.2(2)




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

* Re: Exposing agreggate members of a class
  1998-12-30  0:00 Exposing agreggate members of a class David Botton
                   ` (2 preceding siblings ...)
  1999-01-03  0:00 ` Simon Wright
@ 1999-01-06  0:00 ` Matthew Heaney
  3 siblings, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1999-01-06  0:00 UTC (permalink / raw)


"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.)






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

end of thread, other threads:[~1999-01-06  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox