comp.lang.ada
 help / color / mirror / Atom feed
* generic type identification
@ 2003-01-04  1:50 David Holm
  2003-01-04  2:04 ` chris.danx
  2003-01-04  5:39 ` tmoran
  0 siblings, 2 replies; 11+ messages in thread
From: David Holm @ 2003-01-04  1:50 UTC (permalink / raw)


Hi,
a couple of questions related to generic packages and type specifications.

Lets say I have a package like:
generic
  type New_Type is range <>;
package Example
  procedure Some_Procedure(Item: in New_Type);
end Example;

Is it possible to identify the type of New_Type in the Some_Procedure 
procedure?
I have tried doing stuff like "if New_Type'Range = Integer'Range" etc but 
with little luck.

Another thing that I couldn't find an answer to, is it possible to constrain 
the allowed generic types. Like specifying that New_Type has to be either 
Integer or Float?

The alternative, I guess, would be to not use a generic package and instead 
overload Some_Procedure with those types that I want to be able to use but 
this will clutter the specification file =(.

//David Holm



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

* Re: generic type identification
  2003-01-04  1:50 generic type identification David Holm
@ 2003-01-04  2:04 ` chris.danx
  2003-01-04  5:39 ` tmoran
  1 sibling, 0 replies; 11+ messages in thread
From: chris.danx @ 2003-01-04  2:04 UTC (permalink / raw)



Try here for placing *some* constraints on generics,

http://www.cmis.brighton.ac.uk/staff/je/adacraft/ch12.htm#12.2

I don't know if it's a complete list or not, but if not, try the RM

http://www.ada-auth.org/~acats/arm.html



HTH,
Danx
-- 
for personal replies change spamoff to chris




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

* Re: generic type identification
  2003-01-04  1:50 generic type identification David Holm
  2003-01-04  2:04 ` chris.danx
@ 2003-01-04  5:39 ` tmoran
  2003-01-04 12:39   ` David Holm
  1 sibling, 1 reply; 11+ messages in thread
From: tmoran @ 2003-01-04  5:39 UTC (permalink / raw)


> generic
>   type New_Type is range <>;
> Is it possible to identify the type of New_Type in the Some_Procedure
> procedure?
Suppose:
    type Snowmans_Pleasant_Farenheit is range 18 .. 24;
    type My_Pleasant_Centigrade is range 18 .. 24;
    package My_Procedure is new Example.Some_Procedure(My_Pleasant_Centigrade);
  How could your Some_Procedure tell whether it was instantiated with
Farenheit or Centigrade temperatures, especially since the code that
instantiates it, and the two temperature "types", may not have even existed
when you wrote your package Example?

> Another thing that I couldn't find an answer to, is it possible to constrain
> the allowed generic types. Like specifying that New_Type has to be either
> Integer or Float?
> generic
>   type New_Type is range <>;
    type Other_Type is digits <>;
  New_Type is a signed integer of some sort.  (Perhaps My_Pleasant_Centigrade)
  Other_Type is some kind of floating point type.  And there are other
notations for modular, fixed, private, discrete, etc.

> The alternative, I guess, would be to not use a generic package and instead
> overload Some_Procedure with those types that I want to be able to use but
> this will clutter the specification file =(.
    Not to mention that you will have to add to package Example every time
someone wants to use your Some_Procedure with a new type.
  Why do you want Some_Procedure to know what type it was instantiated
with?  How much does it need to know about that type - just whether it's
integer or float or modular, or does it need to know whether it's
My_Pleasant_Centigrade or not?



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

* Re: generic type identification
  2003-01-04  5:39 ` tmoran
@ 2003-01-04 12:39   ` David Holm
  2003-01-04 16:13     ` James S. Rogers
                       ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: David Holm @ 2003-01-04 12:39 UTC (permalink / raw)


tmoran@acm.org wrote:

>> The alternative, I guess, would be to not use a generic package and
>> instead overload Some_Procedure with those types that I want to be able
>> to use but this will clutter the specification file =(.
>     Not to mention that you will have to add to package Example every time
> someone wants to use your Some_Procedure with a new type.
>   Why do you want Some_Procedure to know what type it was instantiated
> with?  How much does it need to know about that type - just whether it's
> integer or float or modular, or does it need to know whether it's
> My_Pleasant_Centigrade or not?

It is a thick binding to OpenGL. In case you have never seen the OpenGL API 
it has one procedure for every type you can access it with.
For instance, glVertex has 24 different declarations depending on whether 
you use (C) floats, doubles, integers, shorts etc and whether your send it 
an array of 2, 3 or 4 coordinates with any of these types.
I was hoping to be able to create one interface for this in Ada (possibly by 
using generic instead of overloading every procedure). But then I would 
need to be able to identify which type is used in a call to say 
AdaGL.Primitives.Vertex(some array) so that I know which binding to the C 
library I should convert the parameters to and then call.

Perhaps I could go with something like this?
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8903101703.AA04518%40ti.com

//David Holm



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

* Re: generic type identification
  2003-01-04 12:39   ` David Holm
@ 2003-01-04 16:13     ` James S. Rogers
  2003-01-05  3:28       ` David Holm
  2003-01-04 19:27     ` tmoran
  2003-01-06 17:56     ` Stephen Leake
  2 siblings, 1 reply; 11+ messages in thread
From: James S. Rogers @ 2003-01-04 16:13 UTC (permalink / raw)


"David Holm" <david@realityrift.com> wrote in message
news:_BAR9.4055$FF4.251139@newsb.telia.net...
> tmoran@acm.org wrote:
> It is a thick binding to OpenGL. In case you have never seen the OpenGL
API
> it has one procedure for every type you can access it with.
> For instance, glVertex has 24 different declarations depending on whether
> you use (C) floats, doubles, integers, shorts etc and whether your send it
> an array of 2, 3 or 4 coordinates with any of these types.
> I was hoping to be able to create one interface for this in Ada (possibly
by
> using generic instead of overloading every procedure). But then I would
> need to be able to identify which type is used in a call to say
> AdaGL.Primitives.Vertex(some array) so that I know which binding to the C
> library I should convert the parameters to and then call.
>
> Perhaps I could go with something like this?
>
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8903101703.
AA04518%40ti.com
>

Another possibility is to create an abstract tagged type with one function
and
one procedure. The function will take a primitive (float, integer, etc.) and
convert it to the corresponding tagged type. The procedure will take an
instance of the tagged type and call the appropriate OpenGL binding.

type GL_Binding is abstract tagged null record;
procedure Access_GL(Item : GL_Binding) is abstract;


Each package creating a concrete instance of the tagged type GL_Binding
would also define a function (basically a constructor) creating an instance
of that concrete tagged type from the appropriate scalar type.

type GL_Integer_Binding is new GL_Binding with record
   Value : Integer;
end record;

procedure Access_GL(Item : GL_Integer_Binding);
function Create(Item : Integer) return GL_Integer_Binding;

Do this for each numeric type you need to bind.
Calling the appropriate binding is then very simple.

Pixel_Location :  Integer;
Access_GL(Create(Pixel_Location));

Jim Rogers





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

* Re: generic type identification
  2003-01-04 12:39   ` David Holm
  2003-01-04 16:13     ` James S. Rogers
@ 2003-01-04 19:27     ` tmoran
  2003-01-05  3:25       ` David Holm
  2003-01-06 17:56     ` Stephen Leake
  2 siblings, 1 reply; 11+ messages in thread
From: tmoran @ 2003-01-04 19:27 UTC (permalink / raw)


>For instance, glVertex has 24 different declarations depending on whether
>you use (C) floats, doubles, integers, shorts etc and whether your send it
  So you don't really need to know the "type" in the Ada sense of the
word, just in the C sense.  If it's OK to call the "double" version for
any float, and the "int" version for shorts as well as ints, then one
generic for int and one for double should do the job.  So how about:

generic
  type An_Integer_Type is range <>;
  type A_Float_Type is digits <>;
package Example
  procedure Some_Procedure(Item: in An_Integer_Type);
  procedure Some_Procedure(Item: in A_Float_Type);
end Example;

package body Example is

  procedure Some_Procedure(Item: in An_Integer_Type) is
  begin gl_i_SomeProcedure(Interfaces.C.Int(Item));end Some_Procedure;

  procedure Some_Procedure(Item: in A_Float_Type) is
  begin gl_d_SomeProcedure(Interfaces.C.Double(Item));end Some_Procedure;

end Example;



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

* Re: generic type identification
  2003-01-04 19:27     ` tmoran
@ 2003-01-05  3:25       ` David Holm
  2003-01-05  5:42         ` tmoran
  0 siblings, 1 reply; 11+ messages in thread
From: David Holm @ 2003-01-05  3:25 UTC (permalink / raw)


tmoran@acm.org wrote:

>   So you don't really need to know the "type" in the Ada sense of the
> word, just in the C sense.  If it's OK to call the "double" version for
> any float, and the "int" version for shorts as well as ints, then one
> generic for int and one for double should do the job.  So how about:

I wish it was this simple but as you will probably pass OpenGL alot of 
vertices every frame (if there are lots of stuff changing in the scene) you 
don't want to pass it 64bit doubles if 32bit floats are enough. Same goes 
for int vs short.

//David



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

* Re: generic type identification
  2003-01-04 16:13     ` James S. Rogers
@ 2003-01-05  3:28       ` David Holm
  2003-01-05 10:03         ` Gautier
  0 siblings, 1 reply; 11+ messages in thread
From: David Holm @ 2003-01-05  3:28 UTC (permalink / raw)


James S. Rogers wrote:
> 
> Another possibility is to create an abstract tagged type with one function
> and
> one procedure. The function will take a primitive (float, integer, etc.)
> and convert it to the corresponding tagged type. The procedure will take
> an instance of the tagged type and call the appropriate OpenGL binding.
> 
> type GL_Binding is abstract tagged null record;
> procedure Access_GL(Item : GL_Binding) is abstract;
> 
> 
> Each package creating a concrete instance of the tagged type GL_Binding
> would also define a function (basically a constructor) creating an
> instance of that concrete tagged type from the appropriate scalar type.
> 
> type GL_Integer_Binding is new GL_Binding with record
>    Value : Integer;
> end record;
> 
> procedure Access_GL(Item : GL_Integer_Binding);
> function Create(Item : Integer) return GL_Integer_Binding;
> 
> Do this for each numeric type you need to bind.
> Calling the appropriate binding is then very simple.
> 
> Pixel_Location :  Integer;
> Access_GL(Create(Pixel_Location));
> 
> Jim Rogers

In this case I'd rather create a couple of overloaded procedures as the idea 
of the thick binding is to make it easier to access OpenGL from Ada.
This method would just make it harder since it would require one extra call 
for each time you pass values to OpenGL.

//David Holm



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

* Re: generic type identification
  2003-01-05  3:25       ` David Holm
@ 2003-01-05  5:42         ` tmoran
  0 siblings, 0 replies; 11+ messages in thread
From: tmoran @ 2003-01-05  5:42 UTC (permalink / raw)


> I wish it was this simple but as you will probably pass OpenGL alot of
> vertices every frame (if there are lots of stuff changing in the scene) you
> don't want to pass it 64bit doubles if 32bit floats are enough. Same goes
> for int vs short.
  So you surely don't want to spend execution time figuring out what
"type" was passed in.
  If I understand correctly, you have a set of C procedures like
    something_int(int x);
    something_short(short x);
    something_float(float x);
    something_double(double x);
etc. and you want to get the compiler to figure out which one to call?
How about
  package Short_Forms is
    subtype Distances is Interfaces.C.Short;
    procedure Something(x : in Distances);
    procedure Other_Thing(x : in Distances);
  end Short_Forms;
  package Int_Forms is
    subtype Distances is Interfaces.C.Int;
    procedure Something(x : in Distances);
    procedure Other_Thing(x : in Distances);
  end Int_Forms;
  package Float_Forms is
    subtype Distances is Interfaces.C.C_Float;
    procedure Something(x : in Distances);
    procedure Other_Thing(x : in Distances);
  end Float_Forms;
and then 'with' and 'use' the appropriate one in each calculation package?
  with Int_Forms; use Int_Forms;
  package body Draw_Stuff is
    x,y : Distances;
    ...
    Something(x);
Does that match what you are trying to do?



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

* Re: generic type identification
  2003-01-05  3:28       ` David Holm
@ 2003-01-05 10:03         ` Gautier
  0 siblings, 0 replies; 11+ messages in thread
From: Gautier @ 2003-01-05 10:03 UTC (permalink / raw)


David Holm <david@realityrift.com>:

> In this case I'd rather create a couple of overloaded procedures as the idea 
> of the thick binding is to make it easier to access OpenGL from Ada.
> This method would just make it harder since it would require one extra call 
> for each time you pass values to OpenGL.

It's the simplest variant IMHO too...
Excerpt from my home-made bindings to OpenGL 1.1:

package GL is

  ....

  -- Specify vertices
  procedure Vertex (x,y: GL.double);
  procedure Vertex_f (x,y: GL.float);

  procedure Vertex (x,y: GL.int);
  procedure Vertex_s (x,y: GL.short);

  procedure Vertex (x,y,z: GL.double);
  procedure Vertex_f (x,y,z: GL.float);

  ....

private

  ....

  -- Wrapper for vertex2d
  procedure vertex2d (x,y: GL.double);
  procedure vertex (x,y: GL.double) renames vertex2d;

  -- Wrapper for vertex2f
  procedure vertex2f (x,y: GL.float);
  procedure vertex_f (x,y: GL.float) renames vertex2f;

  ....
  pragma Import (Stdcall, Vertex3d, "glVertex3d");
  pragma Import (Stdcall, Vertex3f, "glVertex3f");
  pragma Import (Stdcall, Vertex3i, "glVertex3i");
  ....

end GL;

Write me for an applet that produces the "renames" part
________________________________________________________
Gautier  --  http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



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

* Re: generic type identification
  2003-01-04 12:39   ` David Holm
  2003-01-04 16:13     ` James S. Rogers
  2003-01-04 19:27     ` tmoran
@ 2003-01-06 17:56     ` Stephen Leake
  2 siblings, 0 replies; 11+ messages in thread
From: Stephen Leake @ 2003-01-06 17:56 UTC (permalink / raw)


David Holm <david@realityrift.com> writes:

> It is a thick binding to OpenGL. In case you have never seen the OpenGL API 
> it has one procedure for every type you can access it with.

Bindings are supposed to be hard to write and easy to use; your effort
writing the binding saves zillions of people effort in using them :).
We appreciate it!

> 
> For instance, glVertex has 24 different declarations depending on
> whether you use (C) floats, doubles, integers, shorts etc and
> whether your send it an array of 2, 3 or 4 coordinates with any of
> these types. 

Overloaded procedures is the right way to go here; one Ada procedure
for each C procedure.

> I was hoping to be able to create one interface for this in Ada
> (possibly by using generic instead of overloading every procedure).

You would need a real binding generator to do this automatically.

> 
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=8903101703.AA04518%40ti.com

too much overhead. 

-- 
-- Stephe



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

end of thread, other threads:[~2003-01-06 17:56 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-04  1:50 generic type identification David Holm
2003-01-04  2:04 ` chris.danx
2003-01-04  5:39 ` tmoran
2003-01-04 12:39   ` David Holm
2003-01-04 16:13     ` James S. Rogers
2003-01-05  3:28       ` David Holm
2003-01-05 10:03         ` Gautier
2003-01-04 19:27     ` tmoran
2003-01-05  3:25       ` David Holm
2003-01-05  5:42         ` tmoran
2003-01-06 17:56     ` Stephen Leake

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