comp.lang.ada
 help / color / mirror / Atom feed
* Access type conversions, how?
@ 2004-04-13  0:32 Luke A. Guest
  2004-04-13  1:25 ` Randy Brukardt
  2004-04-13 14:57 ` zhenglonggen
  0 siblings, 2 replies; 16+ messages in thread
From: Luke A. Guest @ 2004-04-13  0:32 UTC (permalink / raw)


Hi,

We have the following in our GL package:

package C renames Interfaces.C;
...
type GLfloat      is new C.C_float;
...
type GLfloatPtr   is access all GLfloat;
...
procedure glMultMatrixf(M : in GLfloatPtr);

Now, in my code, I've created a Matrix package which has as it's type:

type Object is array(Integer range 0 .. 15) of aliased Float;

Now, I've made it aliased because I want to pass this to glMultMatrixf,
but for some reason I cannot work out exactly how to do it.

I've tried all sorts, like:

GL.glMultMatrixf(M(M'First)'Unchecked_Access); -- Wrong type

I've tried casting it to GL.GLfloat and GL.GLfloatPtr, but nothing's
working. Can anyone help?

Thanks,
Luke.




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

* Re: Access type conversions, how?
  2004-04-13  0:32 Access type conversions, how? Luke A. Guest
@ 2004-04-13  1:25 ` Randy Brukardt
  2004-04-13  8:08   ` Luke A. Guest
  2004-04-13 14:57 ` zhenglonggen
  1 sibling, 1 reply; 16+ messages in thread
From: Randy Brukardt @ 2004-04-13  1:25 UTC (permalink / raw)


"Luke A. Guest" <laguest@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.uk> wrote
in message
news:pan.2004.04.13.00.32.33.603741@n_o_p_o_r_k_a_n_d_h_a_m.abyss2.demon.co.
uk...
> Hi,
>
> We have the following in our GL package:
>
> package C renames Interfaces.C;
> ...
> type GLfloat      is new C.C_float;
> ...
> type GLfloatPtr   is access all GLfloat;
> ...
> procedure glMultMatrixf(M : in GLfloatPtr);
>
> Now, in my code, I've created a Matrix package which has as it's type:
>
> type Object is array(Integer range 0 .. 15) of aliased Float;
>
> Now, I've made it aliased because I want to pass this to glMultMatrixf,
> but for some reason I cannot work out exactly how to do it.
>
> I've tried all sorts, like:
>
> GL.glMultMatrixf(M(M'First)'Unchecked_Access); -- Wrong type
>
> I've tried casting it to GL.GLfloat and GL.GLfloatPtr, but nothing's
> working. Can anyone help?

Ready for a head slapping moment? :-)

GLfloatPtr points at a GLFloat (a C.C_Float), the 'Unchecked_Access points
at a (Standard.)Float. These aren't the same type! They don't even need to
be the same size (although they will be in practice), so you can't convert
the access type.

If Object was declared "... of aliased GLFloat" everything should be fine.

(You can slap your head and go "Duh!" now. :-)

                 Randy.






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

* Re: Access type conversions, how?
  2004-04-13  1:25 ` Randy Brukardt
@ 2004-04-13  8:08   ` Luke A. Guest
  2004-04-13 15:11     ` chris
  0 siblings, 1 reply; 16+ messages in thread
From: Luke A. Guest @ 2004-04-13  8:08 UTC (permalink / raw)


On Mon, 12 Apr 2004 20:25:23 -0500, Randy Brukardt wrote:

> Ready for a head slapping moment? :-)
> 
> GLfloatPtr points at a GLFloat (a C.C_Float), the 'Unchecked_Access points
> at a (Standard.)Float. These aren't the same type! They don't even need to
> be the same size (although they will be in practice), so you can't convert
> the access type.
> 
> If Object was declared "... of aliased GLFloat" everything should be fine.

Well, I know that! But, I don't want to have different Matrix types
for different platforms, and that's essentially what that will mean.

Luke.




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

* Re: Access type conversions, how?
  2004-04-13  0:32 Access type conversions, how? Luke A. Guest
  2004-04-13  1:25 ` Randy Brukardt
@ 2004-04-13 14:57 ` zhenglonggen
  2004-04-14  7:45   ` Martin Dowie
  1 sibling, 1 reply; 16+ messages in thread
From: zhenglonggen @ 2004-04-13 14:57 UTC (permalink / raw)
  To: comp.lang.ada

Luke A. Guest wrote:

 >Hi,
 >
 >We have the following in our GL package:
 >
 >package C renames Interfaces.C;
 >...
 >type GLfloat      is new C.C_float;
 >...
 >type GLfloatPtr   is access all GLfloat;
 >...
 >procedure glMultMatrixf(M : in GLfloatPtr);
 >
 >Now, in my code, I've created a Matrix package which has as it's type:
 >
 >type Object is array(Integer range 0 .. 15) of aliased Float;
 >Now, I've made it aliased because I want to pass this to glMultMatrixf,
 >but for some reason I cannot work out exactly how to do it.
 >
 >I've tried all sorts, like:
 >
 >GL.glMultMatrixf(M(M'First)'Unchecked_Access); -- Wrong type
 >
The following code works:


with Ada.Text_Io;
use Ada.Text_Io;
with System;
use System;
with Interfaces.C;
use Interfaces.C;
procedure Test_access2address is
    type GLFloat is new C_Float;
    type GLFloat_Ptr is access all GLFloat;
    type Object is array(0..15) of aliased GLFloat;
    type Object_Ptr is access all Object;
    function Access2address(An_Object_Ptr:Object_Ptr) return GLFloat_Ptr
    is
       A_GLfloat:aliased GLFloat;
       for A_GLFloat'Address use An_Object_Ptr(1)'Address;
    begin
       return A_GLfloat'Unchecked_Access;
    end;
    procedure Test(A_Float:GLFloat_Ptr) is
    begin
       Put_Line("the first element is "&GLfloat'Image(A_Float.all));
    end;
    My_Object:aliased Object;
begin
    My_Object(1):=0.001;
    Test(Access2address(My_Object'Access));
end;








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

* Re: Access type conversions, how?
  2004-04-13  8:08   ` Luke A. Guest
@ 2004-04-13 15:11     ` chris
  2004-04-13 16:01       ` Luke Guest
  0 siblings, 1 reply; 16+ messages in thread
From: chris @ 2004-04-13 15:11 UTC (permalink / raw)


Luke A. Guest wrote:
> Well, I know that! But, I don't want to have different Matrix types
> for different platforms, and that's essentially what that will mean.

Why not use this?

type GLMatrix is array (1 .. 15) of GLFloat;
pragma Convention(C, GLMatrix);

What do you want to do?  Pass an array to a C function or an array of 
arrays?



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

* Re: Access type conversions, how?
  2004-04-13 15:11     ` chris
@ 2004-04-13 16:01       ` Luke Guest
  2004-04-13 17:54         ` chris
  0 siblings, 1 reply; 16+ messages in thread
From: Luke Guest @ 2004-04-13 16:01 UTC (permalink / raw)



"chris" <spamoff.danx@ntlworld.com> wrote in message
news:LnTec.5$094.0@newsfe1-win...
> Luke A. Guest wrote:
> > Well, I know that! But, I don't want to have different Matrix types
> > for different platforms, and that's essentially what that will mean.
>
> Why not use this?
>
> type GLMatrix is array (1 .. 15) of GLFloat;
> pragma Convention(C, GLMatrix);

No.

> What do you want to do?  Pass an array to a C function or an array of
> arrays?

Like I said, we have a GL package which is the OpenGL bindings (yeah, all
the functions).

I want to have an abstraction layer so I can *hide* the details of the 3D
API (or whatever
I'm using on a particular platform), above my layer, I don't want any other
code to know
about the lower levels, i.e. it should not reply on GL.GLfloat, simple as
that.

But as GL bindings use the GL types, I cannot get a pointer to convert to
another pointer with
the same base types.

Luke.





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

* Re: Access type conversions, how?
  2004-04-13 16:01       ` Luke Guest
@ 2004-04-13 17:54         ` chris
  2004-04-13 19:31           ` Luke A. Guest
  0 siblings, 1 reply; 16+ messages in thread
From: chris @ 2004-04-13 17:54 UTC (permalink / raw)


Luke Guest wrote:
> "chris" <spamoff.danx@ntlworld.com> wrote in message
> news:LnTec.5$094.0@newsfe1-win...
> 
>>type GLMatrix is array (1 .. 15) of GLFloat;
>>pragma Convention(C, GLMatrix);
> 
> No.
> 
>>What do you want to do?  Pass an array to a C function or an array of
>>arrays?
> 
> Like I said, we have a GL package which is the OpenGL bindings (yeah, all
> the functions).
> 
> I want to have an abstraction layer so I can *hide* the details of the 3D
> API (or whatever I'm using on a particular platform), above my layer, 
> I don't want any other code to know about the lower levels, i.e. it should 
> not reply on GL.GLfloat, simple as that.

 From the code you posted it looks like a convoluted solution.  Maybe 
it's the lack of context.

I would do one of two things.  Either just stick with the native GL 
types or make Ada ones corresponding to them and convert like this...


type GLMatrix is array (1..15) of GLFloat;
pragma Convention (C, GLMatrix);

type OpenGLMatrix is array (1..15) of GLFloat;


A : OpenGLMatrix;

A (1..15) := OpenGLMatrix (SomeMatrix (1..15));

or A := OpenGLMatrix(SomeMatrix);

I think i'd stick with the GL types (possibly subtyping or creating new 
types from them), there seems to be no gain in hiding those since 
they're simple and easy enough to use.

subtype OpenGLMatrix is GL.GLMatrix;

> But as GL bindings use the GL types, I cannot get a pointer to convert to
> another pointer with the same base types.

Why do that?



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

* Re: Access type conversions, how?
  2004-04-13 17:54         ` chris
@ 2004-04-13 19:31           ` Luke A. Guest
  2004-04-13 22:51             ` Luke A. Guest
  2004-04-14 13:36             ` Robert I. Eachus
  0 siblings, 2 replies; 16+ messages in thread
From: Luke A. Guest @ 2004-04-13 19:31 UTC (permalink / raw)


On Tue, 13 Apr 2004 18:54:20 +0100, chris wrote:

>  From the code you posted it looks like a convoluted solution.  Maybe 
> it's the lack of context.

No, it's really really simple...the code I posted was all you needed.
 
> I would do one of two things.  Either just stick with the native GL 
> types or make Ada ones corresponding to them and convert like this...

Yes, and like I said before, when writing portable games, i.e. games that
will not always have OpenGL available, I need to to not do this. I need to
abstract it away, hide the rendering code, etc. I cannot say this any
other way.
 
> type GLMatrix is array (1..15) of GLFloat; pragma Convention (C,
> GLMatrix);
> 
> type OpenGLMatrix is array (1..15) of GLFloat;
> 
> 
> A : OpenGLMatrix;
> 
> A (1..15) := OpenGLMatrix (SomeMatrix (1..15));
> 
> or A := OpenGLMatrix(SomeMatrix);

No, unacceptable. I don't want to be creating/destroying temporary arrays
like that, especially for a 3D game.

>> But as GL bindings use the GL types, I cannot get a pointer to convert
>> to another pointer with the same base types.
> 
> Why do that?

Take the glMultMatrixf procedure I posted before, *you want to have your
own types* but *you also want to be able to pass them to the GL functions*
but it's tricky because the strong typing is getting in the way. I need a
way to convert my aliased matrix type to another. That function expects a
GL.GLfloatPtr, I want to use something else, do you understand?

Luke.





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

* Re: Access type conversions, how?
  2004-04-13 22:51             ` Luke A. Guest
@ 2004-04-13 21:59               ` Lutz Donnerhacke
  2004-04-14 11:53                 ` Luke Guest
  0 siblings, 1 reply; 16+ messages in thread
From: Lutz Donnerhacke @ 2004-04-13 21:59 UTC (permalink / raw)


* Luke A. Guest wrote:
> Well, in answer to my question:
>
> with Unchecked_Conversion;

Please use Address_To_Access_Conversion and map the adresses.
Or (better) define a variable of the different type at the same location.
An example is available in my recent posting here on "Efficient Stream_IO"



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

* Re: Access type conversions, how?
  2004-04-13 19:31           ` Luke A. Guest
@ 2004-04-13 22:51             ` Luke A. Guest
  2004-04-13 21:59               ` Lutz Donnerhacke
  2004-04-14 13:36             ` Robert I. Eachus
  1 sibling, 1 reply; 16+ messages in thread
From: Luke A. Guest @ 2004-04-13 22:51 UTC (permalink / raw)


Well, in answer to my question:

with Unchecked_Conversion;
...
type FloatPtr is access all Float;
	  
function Float_To_GLfloat is new Unchecked_Conversion(Source => FloatPtr, Target => GL.GLfloatPtr);
...
GL.glMultMatrixf(Float_To_GLfloat(M(M'First)'Unchecked_Access));

And all is well.

Luke.




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

* Re: Access type conversions, how?
  2004-04-13 14:57 ` zhenglonggen
@ 2004-04-14  7:45   ` Martin Dowie
  2004-04-14 11:49     ` Luke Guest
  0 siblings, 1 reply; 16+ messages in thread
From: Martin Dowie @ 2004-04-14  7:45 UTC (permalink / raw)


zhenglonggen <zhenglonggen@163.net> wrote in message news:<mailman.249.1081893601.327.comp.lang.ada@ada-france.org>...
> with Ada.Text_Io;
> use Ada.Text_Io;

> with System;
> use System;
^^^^^^^^^^^^^^
Not required

> with Interfaces.C;
> use Interfaces.C;
> procedure Test_access2address is
>     type GLFloat is new C_Float;
>     type GLFloat_Ptr is access all GLFloat;
>     type Object is array(0..15) of aliased GLFloat;
                                             ^^^^^^^
The OP wants this to be 'Float' - but this can just be changed an it
will still work

>     type Object_Ptr is access all Object;
>     function Access2address(An_Object_Ptr:Object_Ptr) return GLFloat_Ptr
               ^^^^^^^^^^^^^^
To get the real benefits, this subprogram needs to be 'inlined'.

I'm totally paranoid, so I'd be adding some 'C_Float is the same as
Float' assertions!

Cheers,

-- Martin



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

* Re: Access type conversions, how?
  2004-04-14  7:45   ` Martin Dowie
@ 2004-04-14 11:49     ` Luke Guest
  0 siblings, 0 replies; 16+ messages in thread
From: Luke Guest @ 2004-04-14 11:49 UTC (permalink / raw)


> >     type Object_Ptr is access all Object;
> >     function Access2address(An_Object_Ptr:Object_Ptr) return GLFloat_Ptr
>                ^^^^^^^^^^^^^^
> To get the real benefits, this subprogram needs to be 'inlined'.
>
> I'm totally paranoid, so I'd be adding some 'C_Float is the same as
> Float' assertions!

How would you do this?

Thanks,
Luke.





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

* Re: Access type conversions, how?
  2004-04-13 21:59               ` Lutz Donnerhacke
@ 2004-04-14 11:53                 ` Luke Guest
  2004-04-14 12:07                   ` Lutz Donnerhacke
  0 siblings, 1 reply; 16+ messages in thread
From: Luke Guest @ 2004-04-14 11:53 UTC (permalink / raw)



"Lutz Donnerhacke" <lutz@iks-jena.de> wrote in message
news:slrnc7oolr.amr.lutz@belenus.iks-jena.de...
> * Luke A. Guest wrote:
> > Well, in answer to my question:
> >
> > with Unchecked_Conversion;
>
> Please use Address_To_Access_Conversion and map the adresses.

Why? Is this a standard function?

> Or (better) define a variable of the different type at the same location.

I don't know what you mean here.

> An example is available in my recent posting here on "Efficient Stream_IO"

Well, I've just looked and I couldn't see any use of that function.

Thanks,
Luke.





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

* Re: Access type conversions, how?
  2004-04-14 11:53                 ` Luke Guest
@ 2004-04-14 12:07                   ` Lutz Donnerhacke
  0 siblings, 0 replies; 16+ messages in thread
From: Lutz Donnerhacke @ 2004-04-14 12:07 UTC (permalink / raw)


* Luke Guest wrote:
> "Lutz Donnerhacke" <lutz@iks-jena.de> wrote in message
>> Please use Address_To_Access_Conversion and map the adresses.
>
> Why? Is this a standard function?

ARM95: 13.7.2 The Package System.Address_To_Access_Conversions

>> Or (better) define a variable of the different type at the same location.
>
> I don't know what you mean here.

Mapping two repesentation at the same memory location does not generate any
code, but provide the required access.

>> An example is available in my recent posting here on "Efficient Stream_IO"
>
> Well, I've just looked and I couldn't see any use of that function.

There is no function. Only a variable of a different type mapped to the same
location. You might use "pragma Union(Variable_Record);" on GNAT to get the
same result.



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

* Re: Access type conversions, how?
  2004-04-13 19:31           ` Luke A. Guest
  2004-04-13 22:51             ` Luke A. Guest
@ 2004-04-14 13:36             ` Robert I. Eachus
  2004-04-14 17:37               ` Jeffrey Carter
  1 sibling, 1 reply; 16+ messages in thread
From: Robert I. Eachus @ 2004-04-14 13:36 UTC (permalink / raw)


Luke A. Guest wrote:

> Take the glMultMatrixf procedure I posted before, *you want to have your
> own types* but *you also want to be able to pass them to the GL functions*
> but it's tricky because the strong typing is getting in the way. I need a
> way to convert my aliased matrix type to another. That function expects a
> GL.GLfloatPtr, I want to use something else, do you understand?

Then you need to (unchecked) convert the access type, not the data.  Of 
course, make sure that both access types are either declared access all, 
or designate objects in the same storage pool.

-- 

                                           Robert I. Eachus

"The terrorist enemy holds no territory, defends no population, is 
unconstrained by rules of warfare, and respects no law of morality. Such 
an enemy cannot be deterred, contained, appeased or negotiated with. It 
can only be destroyed--and that, ladies and gentlemen, is the business 
at hand."  -- Dick Cheney




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

* Re: Access type conversions, how?
  2004-04-14 13:36             ` Robert I. Eachus
@ 2004-04-14 17:37               ` Jeffrey Carter
  0 siblings, 0 replies; 16+ messages in thread
From: Jeffrey Carter @ 2004-04-14 17:37 UTC (permalink / raw)


Robert I. Eachus wrote:
> 
> Then you need to (unchecked) convert the access type, not the data.  Of 
> course, make sure that both access types are either declared access all, 
> or designate objects in the same storage pool.

It seems to me that we may be overcomplicating this problem. The OP 
wants to store values using an application-suitable floating-point type, 
but pass a pointer to a value of a similar, but library-dependent 
floating-point type to an operation of the library. This is primarily a 
problem of abstraction, not of access type conversion.

At the lowest level, you have the library, which may differ by platform, 
but which offers similar functionaility on all platforms.

In the middle, you have a wrapper, which takes parameters of 
application-suitable types, and calls the library operations. Any 
necessary conversions occur here. The operations of the wrapper may be 
inlined if measurements show that timing requirements cannot be met 
otherwise.

At the highest level, you have the application  which uses 
application-suitable types and call the operations of the wrapper.

So we have something like:

Library:

type LDFPT is digits ...; -- Library-Dependent Floating-Point Type
function LF (Value : access LDFPT) return ...;

Application:

type ASFPT is digits ...; -- Application-Suitable Floating-Point Type
F : ASFPT;

Wrapper:

function WF (Value : ASFPT) return ...; -- Calls LF

The application makes the call

WF (F);

The body of WF would look something like

function WF (Value : ASFPT) return ... is
    L : aliased LDFPT := LDFPT (Value);
begin -- WF
    return LF (L'Unchecked_Access);
end WF;

This version doesn't allow the library to change Value, but that can 
easily be arranged if needed.

The important thing about this approach is that the application doesn't 
have to alias variables simply because a library needs pointers to 
values. You can more easily have different versions for different 
libraries, simply by having different bodies for the wrapper.

-- 
Jeff Carter
"Blessed are they who convert their neighbors'
oxen, for they shall inhibit their girth."
Monty Python's Life of Brian
83




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

end of thread, other threads:[~2004-04-14 17:37 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-13  0:32 Access type conversions, how? Luke A. Guest
2004-04-13  1:25 ` Randy Brukardt
2004-04-13  8:08   ` Luke A. Guest
2004-04-13 15:11     ` chris
2004-04-13 16:01       ` Luke Guest
2004-04-13 17:54         ` chris
2004-04-13 19:31           ` Luke A. Guest
2004-04-13 22:51             ` Luke A. Guest
2004-04-13 21:59               ` Lutz Donnerhacke
2004-04-14 11:53                 ` Luke Guest
2004-04-14 12:07                   ` Lutz Donnerhacke
2004-04-14 13:36             ` Robert I. Eachus
2004-04-14 17:37               ` Jeffrey Carter
2004-04-13 14:57 ` zhenglonggen
2004-04-14  7:45   ` Martin Dowie
2004-04-14 11:49     ` Luke Guest

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