comp.lang.ada
 help / color / mirror / Atom feed
* acceess problem
@ 2011-07-24 15:59 ldries46
  2011-07-24 17:27 ` Simon Wright
  0 siblings, 1 reply; 9+ messages in thread
From: ldries46 @ 2011-07-24 15:59 UTC (permalink / raw)


I am converting a C/C++ program with openGL to Ada and found the following 
problem:

C/C++ code: float pAxis1Amb[] = { AxL, 0, 0, 1};
                     glMaterialfv(vSide, GL_AMBIENT, pAxis1Amb);
where         : GLAPI void APIENTRY glMaterialfv( GLenum face, GLenum pname, 
const GLfloat *params );

In the Ada bindings the routine is declared as:
                     procedure glMaterialfv (face   : GLenum; pname  : 
GLenum; params : GLfloatPtr);
where :         type GLfloatPtr is access all GLfloat;
and     :         subtype GLfloat is Float;
I have created the following declarations : Axis1Amb   : array(0..3) of 
GLfloat := ( AxL, 0.0, 0.0, 1.0);
                                                                             
              pAxis1Amb  : GLfloatPtr := Axis1Amb'Access;
Now I get the following error message : prefix of "Access" attribute must be 
aliased
I have to follow the C/C++ code as close as possible.
What do I do wrong?

L. Dries 




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

* Re: acceess problem
  2011-07-24 15:59 acceess problem ldries46
@ 2011-07-24 17:27 ` Simon Wright
  2011-07-25  6:57   ` ldries46
  0 siblings, 1 reply; 9+ messages in thread
From: Simon Wright @ 2011-07-24 17:27 UTC (permalink / raw)


"ldries46" <bertus.dries@planet.nl> writes:

> I am converting a C/C++ program with openGL to Ada and found the
> following problem:
>
> C/C++ code: float pAxis1Amb[] = { AxL, 0, 0, 1};
>                     glMaterialfv(vSide, GL_AMBIENT, pAxis1Amb);
> where         : GLAPI void APIENTRY glMaterialfv( GLenum face, GLenum
> pname, const GLfloat *params );
>
> In the Ada bindings the routine is declared as:
>                     procedure glMaterialfv (face   : GLenum; pname  :
> GLenum; params : GLfloatPtr);
> where :         type GLfloatPtr is access all GLfloat;
> and     :         subtype GLfloat is Float;
> I have created the following declarations : Axis1Amb   : array(0..3)
> of GLfloat := ( AxL, 0.0, 0.0, 1.0);
>                                                                                        pAxis1Amb
> : GLfloatPtr := Axis1Amb'Access;
> Now I get the following error message : prefix of "Access" attribute
> must be aliased
> I have to follow the C/C++ code as close as possible.
> What do I do wrong?

That managed to get formatted _really_ strangely!

You could write

   Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);

and then

   pAxis1Amb : GLfloatPtr := Axis1Amb (0)'Access;

I guess you're going to pass pAxisAmb to glMaterialfv()? you will
probably get away with this, since a pointer to the first element is
very likely to be a pointer to the array. It might help to tie things
down to add

  pragma Convention (C, Axis1Amb);

I must say, this is a pretty low-quality binding! There appear to be 5
cases of 'pname' where 'params' is a 4-element array, one case where
it's a 3-element array, and one where a single value is required.

Actually IMO it's a low-quality C spec, too; a case of control coupling.



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

* Re: acceess problem
  2011-07-24 17:27 ` Simon Wright
@ 2011-07-25  6:57   ` ldries46
  2011-07-25  8:27     ` Simon Wright
  2011-07-25  9:08     ` AdaMagica
  0 siblings, 2 replies; 9+ messages in thread
From: ldries46 @ 2011-07-25  6:57 UTC (permalink / raw)


I have done what you suggested and got the following error message on the 
line
Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);

non-local pointer cannot point to local object

I agree that the bindings are not an example of beautifull ada programming 
but as far as I can see the biggest problem is that the OpenGL library is a 
rather old library with originally only C bindings. As you know C is not 
much more as a streamlined assembler language, where you do everything only 
for simplicity and no for readability.

L. Dries

"Simon Wright"  schreef in bericht news:m2fwlvzj0a.fsf@pushface.org...

"ldries46" <bertus.dries@planet.nl> writes:

> I am converting a C/C++ program with openGL to Ada and found the
> following problem:
>
> C/C++ code: float pAxis1Amb[] = { AxL, 0, 0, 1};
>                     glMaterialfv(vSide, GL_AMBIENT, pAxis1Amb);
> where         : GLAPI void APIENTRY glMaterialfv( GLenum face, GLenum
> pname, const GLfloat *params );
>
> In the Ada bindings the routine is declared as:
>                     procedure glMaterialfv (face   : GLenum; pname  :
> GLenum; params : GLfloatPtr);
> where :         type GLfloatPtr is access all GLfloat;
> and     :         subtype GLfloat is Float;
> I have created the following declarations : Axis1Amb   : array(0..3)
> of GLfloat := ( AxL, 0.0, 0.0, 1.0);
> 
> pAxis1Amb
> : GLfloatPtr := Axis1Amb'Access;
> Now I get the following error message : prefix of "Access" attribute
> must be aliased
> I have to follow the C/C++ code as close as possible.
> What do I do wrong?

That managed to get formatted _really_ strangely!

You could write

   Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);

and then

   pAxis1Amb : GLfloatPtr := Axis1Amb (0)'Access;

I guess you're going to pass pAxisAmb to glMaterialfv()? you will
probably get away with this, since a pointer to the first element is
very likely to be a pointer to the array. It might help to tie things
down to add

  pragma Convention (C, Axis1Amb);

I must say, this is a pretty low-quality binding! There appear to be 5
cases of 'pname' where 'params' is a 4-element array, one case where
it's a 3-element array, and one where a single value is required.

Actually IMO it's a low-quality C spec, too; a case of control coupling. 




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

* Re: acceess problem
  2011-07-25  6:57   ` ldries46
@ 2011-07-25  8:27     ` Simon Wright
  2011-07-25  9:59       ` ldries46
  2011-07-26 15:57       ` Gautier write-only
  2011-07-25  9:08     ` AdaMagica
  1 sibling, 2 replies; 9+ messages in thread
From: Simon Wright @ 2011-07-25  8:27 UTC (permalink / raw)


"ldries46" <bertus.dries@planet.nl> writes:

> I have done what you suggested and got the following error message on
> the line
> Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);
>
> non-local pointer cannot point to local object

I should have tried it, sorry.

Replace 'Access with 'Unchecked_Access or, if that doesn't work, with
the (GNAT-special) 'Unrestricted_Access.

Later:

I did try it, with these two:

   package Floats is

      type P is access all Float;

   end Floats;

   with Floats;
   package Aliased_Stuff is

      A : array (0 .. 3) of aliased Float := (42.0, 0.0, 0.0, 1.0);

      P : Floats.P := A (0)'Access;

   end Aliased_Stuff;

and the compiler (GNAT GPL 2011) was quite happy.



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

* Re: acceess problem
  2011-07-25  6:57   ` ldries46
  2011-07-25  8:27     ` Simon Wright
@ 2011-07-25  9:08     ` AdaMagica
  1 sibling, 0 replies; 9+ messages in thread
From: AdaMagica @ 2011-07-25  9:08 UTC (permalink / raw)


On 25 Jul., 08:57, "ldries46" <bertus.dr...@planet.nl> wrote:
> I have done what you suggested and got the following error message on the
> line
> Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);
>
> non-local pointer cannot point to local object

For 'Access, you have to declare the pointer and the object it's meant
to access on the same accessibility level, i.e. if the pointer is on
library level, the object has to be as well.

For 'Unchecked_Access, the accessibility level checks are suppressed,
i.e. the object is treated as if it were on library level. Then you
are on yourself to prevent dangling pointers. So be careful.



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

* Re: acceess problem
  2011-07-25  8:27     ` Simon Wright
@ 2011-07-25  9:59       ` ldries46
  2011-07-26 15:57       ` Gautier write-only
  1 sibling, 0 replies; 9+ messages in thread
From: ldries46 @ 2011-07-25  9:59 UTC (permalink / raw)


Thanks this was the solution to get the compiler accepting the construction.

L. Dries

"Simon Wright"  schreef in bericht news:m24o2azrwb.fsf@pushface.org... 

"ldries46" <bertus.dries@planet.nl> writes:

> I have done what you suggested and got the following error message on
> the line
> Axis1Amb : array (0 .. 3) of aliased GLfloat := (AxL, 0.0, 0.0, 1.0);
>
> non-local pointer cannot point to local object

I should have tried it, sorry.

Replace 'Access with 'Unchecked_Access or, if that doesn't work, with
the (GNAT-special) 'Unrestricted_Access.

Later:

I did try it, with these two:

   package Floats is

      type P is access all Float;

   end Floats;

   with Floats;
   package Aliased_Stuff is

      A : array (0 .. 3) of aliased Float := (42.0, 0.0, 0.0, 1.0);

      P : Floats.P := A (0)'Access;

   end Aliased_Stuff;

and the compiler (GNAT GPL 2011) was quite happy.



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

* Re: acceess problem
  2011-07-25  8:27     ` Simon Wright
  2011-07-25  9:59       ` ldries46
@ 2011-07-26 15:57       ` Gautier write-only
  2011-07-26 17:43         ` Adam Beneschan
  1 sibling, 1 reply; 9+ messages in thread
From: Gautier write-only @ 2011-07-26 15:57 UTC (permalink / raw)


On 25 juil, 10:27, Simon Wright <si...@pushface.org> wrote:

> Replace 'Access with 'Unchecked_Access or, if that doesn't work, with
> the (GNAT-special) 'Unrestricted_Access.

Unless 'Unrestricted_Access has been inbetween added to the language,
there is a way to have it in a pure Ada form:

  function Cvt is new
Ada.Unchecked_Conversion(System.Address,DoublePtr);
  -- This method is functionally identical as GNAT's
Unrestricted_Access
  -- but has no type safety (cf GNAT Docs)
  pragma No_Strict_Aliasing(DoublePtr); -- recommended by GNAT 2005

  procedure Vertex (v: Double_vector_3D) is
  begin
    Vertex3dv(Cvt(v(0)'Address));
  end Vertex;

^this is an excerpt from an existing GL binding (why reinvent it?)...
http://globe3d.svn.sourceforge.net/viewvc/globe3d/bindings/gl.adb?revision=152&view=markup

______________________________________________________________________________
Gautier's Ada programming -- http://gautiersblog.blogspot.com/search/label/Ada
NB: follow the above link for a valid e-mail address



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

* Re: acceess problem
  2011-07-26 15:57       ` Gautier write-only
@ 2011-07-26 17:43         ` Adam Beneschan
  2011-07-27 12:06           ` Gautier write-only
  0 siblings, 1 reply; 9+ messages in thread
From: Adam Beneschan @ 2011-07-26 17:43 UTC (permalink / raw)


On Jul 26, 8:57 am, Gautier write-only <gautier_niou...@hotmail.com>
wrote:
> On 25 juil, 10:27, Simon Wright <si...@pushface.org> wrote:
>
> > Replace 'Access with 'Unchecked_Access or, if that doesn't work, with
> > the (GNAT-special) 'Unrestricted_Access.
>
> Unless 'Unrestricted_Access has been inbetween added to the language,
> there is a way to have it in a pure Ada form:
>
>   function Cvt is new
> Ada.Unchecked_Conversion(System.Address,DoublePtr);
>   -- This method is functionally identical as GNAT's
> Unrestricted_Access
>   -- but has no type safety (cf GNAT Docs)
>   pragma No_Strict_Aliasing(DoublePtr); -- recommended by GNAT 2005
>
>   procedure Vertex (v: Double_vector_3D) is
>   begin
>     Vertex3dv(Cvt(v(0)'Address));
>   end Vertex;
>
> ^this is an excerpt from an existing GL binding (why reinvent it?)...http://globe3d.svn.sourceforge.net/viewvc/globe3d/bindings/gl.adb?rev...

Although I don't like this approach, if you're going to convert an
Address to an access object, surely it's better to use
System.Address_To_Access_Conversions, which is designed for this sort
of conversion, than to use Unchecked_Conversion, which assumes that a
System.Address and the access object you're converting to have the
same format, which isn't always the case.  There's no point in using
"pure Ada" if it results in code that compiles on another system but
doesn't work properly.

                              -- Adam



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

* Re: acceess problem
  2011-07-26 17:43         ` Adam Beneschan
@ 2011-07-27 12:06           ` Gautier write-only
  0 siblings, 0 replies; 9+ messages in thread
From: Gautier write-only @ 2011-07-27 12:06 UTC (permalink / raw)


On 26 juil, 19:43, Adam Beneschan <a...@irvine.com> wrote:
> Although I don't like this approach, if you're going to convert an
> Address to an access object, surely it's better to use
> System.Address_To_Access_Conversions, which is designed for this sort
> of conversion, than to use Unchecked_Conversion, which assumes that a
> System.Address and the access object you're converting to have the
> same format, which isn't always the case.  There's no point in using
> "pure Ada" if it results in code that compiles on another system but
> doesn't work properly.

Could not agree more!
http://globe3d.svn.sourceforge.net/viewvc/globe3d/bindings/gl.adb?r1=153&r2=152&pathrev=153



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

end of thread, other threads:[~2011-07-27 12:06 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-24 15:59 acceess problem ldries46
2011-07-24 17:27 ` Simon Wright
2011-07-25  6:57   ` ldries46
2011-07-25  8:27     ` Simon Wright
2011-07-25  9:59       ` ldries46
2011-07-26 15:57       ` Gautier write-only
2011-07-26 17:43         ` Adam Beneschan
2011-07-27 12:06           ` Gautier write-only
2011-07-25  9:08     ` AdaMagica

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