comp.lang.ada
 help / color / mirror / Atom feed
* Conversion of access types ...
@ 1997-04-30  0:00 t_mjb
  1997-05-03  0:00 ` Simon Wright
  0 siblings, 1 reply; 3+ messages in thread
From: t_mjb @ 1997-04-30  0:00 UTC (permalink / raw)



type F is tagged record A: Integer; end record;
type P is access all F;

I am trying to convert a variable of type P to an unsigned long (ULONG) which will
allow me to pass this value on to a system procedure which will, when the data
finally arrives at its destination, will be converted back into type P.

I cannot seem to get this scenario to work and I cannot think of another way to
make it work. I'm using GNAT 3.09 and OS/2 emx 0.9c (the variable of type P is being
set using WinSetWindowULong and retrieved using WinQueryWindowULong...any help
would be greatly appreciated.

Marc Bejerano




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

* Re: Conversion of access types ...
  1997-04-30  0:00 Conversion of access types t_mjb
@ 1997-05-03  0:00 ` Simon Wright
  1997-05-03  0:00   ` John G. Volan
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Wright @ 1997-05-03  0:00 UTC (permalink / raw)




t_mjb@linkabit.titan.com writes:

> type F is tagged record A: Integer; end record;
> type P is access all F;

> I am trying to convert a variable of type P to an unsigned long
> (ULONG) which will allow me to pass this value on to a system
> procedure which will, when the data finally arrives at its
> destination, will be converted back into type P.

I had a similar problem (calls to my own C procedures, actually, but
equivalent) and found that I could declare the C procedure using the
Ada access types with pragma Import:

  type Connection is abstract tagged limited private;
  type Connection_P is access all Connection'Class;
  ...
  type Handler is access procedure
     (Reason : Call_Reason;
      On_The_Connection : Peripherals.Connection_P);
  pragma Convention (C, Handler);
  function Connect (Name : Interfaces.C.Char_Array;
                    The_Handler : Handler;
		    The_Closure : Connection_P)
     return Interfaces.C.Int;
  pragma Import (C, Connect, "connect");

This all works fine on GNAT-3.09 (osf, solaris, linux) but of course
there may well be stuff I've failed to understand. For example, what
would happen if the pointers were fat? Should I have a pragma
Convention (C, Connection_P); ?

-- 
Simon Wright                        Work Email: simon.j.wright@gecm.com
GEC-Marconi Radar & Defence Systems            Voice: +44(0)1705-701778
Command & Information Systems Divsion            FAX: +44(0)1705-701800




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

* Re: Conversion of access types ...
  1997-05-03  0:00 ` Simon Wright
@ 1997-05-03  0:00   ` John G. Volan
  0 siblings, 0 replies; 3+ messages in thread
From: John G. Volan @ 1997-05-03  0:00 UTC (permalink / raw)



> t_mjb@linkabit.titan.com writes:
[snip]
> > I am trying to convert a variable of type P to an unsigned long
> > (ULONG) which will allow me to pass this value on to a system
> > procedure which will, when the data finally arrives at its
> > destination, will be converted back into type P.

Well, you could just instantiate Unchecked_Conversion:

    with Ada.Unchecked_Conversion;
    ...
      function P_To_ULONG is new Ada.Unchecked_Conversion (P, ULONG);

      Your_P : P;
      ...
      Your_C_Routine ( ..., P_To_ULONG(Your_P), ...);

But then again, why bother...

Simon Wright wrote:
> 
> I had a similar problem (calls to my own C procedures, actually, but
> equivalent) and found that I could declare the C procedure using the
> Ada access types with pragma Import:
[snip]

Exactly. Remember, everything in the computer is just bits and bytes.
Whether you're talking Ada95 or C or whatever, _all_ data types -- even
seemingly "simple" ones such as integers -- are just abstractions
layered on top of the bits and bytes.  High order languages are really
just an illusion.  It's a very _useful_ illusion, and the discipline of
adhering to that illusion reaps benefits in increased programmer
productivity.  But it's still just an illusion.

So whenever you need to push beyond the boundaries of your programming
language to interface with external hardware or code in other languages,
you have the liberty to pick which illusion you want to map onto the
outside world.

If you have to pretend that those four bytes are something, why bother
pretending they're a ULONG?  You could just as easily pretend they're
your access type directly.  As long as the bit-representation is the
same (you _do_ have to know what you're doing), one pretense is as good
as another! :-)

------------------------------------------------------------------------
Internet.Usenet.Put_Signature 
  (Name => "John G. Volan",  Home_Email => "johnvolan@sprintmail.com",
   Slogan => "Ada95: The World's *FIRST* International-Standard OOPL",
   Disclaimer => "These opinions were never defined, so using them " & 
     "would be erroneous...or is that just nondeterministic now? :-) ");
------------------------------------------------------------------------




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

end of thread, other threads:[~1997-05-03  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-04-30  0:00 Conversion of access types t_mjb
1997-05-03  0:00 ` Simon Wright
1997-05-03  0:00   ` John G. Volan

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