comp.lang.ada
 help / color / mirror / Atom feed
* Function pointer in Ada?
@ 2002-05-14 12:51 Henrik Quintel
  2002-05-14 13:08 ` Markus Knauss
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Henrik Quintel @ 2002-05-14 12:51 UTC (permalink / raw)


Hi, as the most people know, in C there is the possibility to make use
of a function pointer. In Ada this not possible. Does someone knows how
is it possible to make use of such a feature like a function pointer in
Ada? I know it is difficult but perhaps someone knows.

Thanks for all.

Greetings Henrik


-- 
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG



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

* Re: Function pointer in Ada?
  2002-05-14 12:51 Function pointer in Ada? Henrik Quintel
@ 2002-05-14 13:08 ` Markus Knauss
  2002-05-14 13:12 ` Marin David Condic
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Markus Knauss @ 2002-05-14 13:08 UTC (permalink / raw)


Henrik Quintel wrote:

> Hi, as the most people know, in C there is the possibility to make use
> of a function pointer. In Ada this not possible. Does someone knows how
> is it possible to make use of such a feature like a function pointer in
> Ada? I know it is difficult but perhaps someone knows.


Hi,

have a look in the Ada95-RM, Section 3.10 Access Types, Paragraph (26). 
A complete example is there. It isn't as difficult as you might think.

Greetings,
   Markus






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

* Re: Function pointer in Ada?
  2002-05-14 12:51 Function pointer in Ada? Henrik Quintel
  2002-05-14 13:08 ` Markus Knauss
@ 2002-05-14 13:12 ` Marin David Condic
  2002-05-14 14:36 ` Alfred Hilscher
  2002-05-14 17:58 ` Kai Schuelke
  3 siblings, 0 replies; 14+ messages in thread
From: Marin David Condic @ 2002-05-14 13:12 UTC (permalink / raw)


Yes Ada (at least Ada95) can create pointers to functions. (General advice:
Think long and hard about why you want to use access to functions and ask
yourself if there is a more Ada-ish way to do the same thing - like using
tagged types with dispatching) Look at ARM 3.10 for the syntax, etc. An
example can be found in ARM 3.10(26) which for your convenience is:

26 type Message_Procedure is access procedure (M : in String := "Error!");
procedure Default_Message_Procedure(M : in String);
Give_Message : Message_Procedure := Default_Message_Procedure'Access;
...
procedure Other_Procedure(M : in String);
...
Give_Message := Other_Procedure'Access;
...
Give_Message("File not found."); -- call with parameter (.all is optional)
Give_Message.all; -- call with no parameters


MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com


"Henrik Quintel" <henrik.quintel@gmx.de> wrote in message
news:5f4adaeaa2a7b35810acd4e20fb98b25.86200@mygate.mailgate.org...
> Hi, as the most people know, in C there is the possibility to make use
> of a function pointer. In Ada this not possible. Does someone knows how
> is it possible to make use of such a feature like a function pointer in
> Ada? I know it is difficult but perhaps someone knows.
>






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

* Re: Function pointer in Ada?
  2002-05-14 12:51 Function pointer in Ada? Henrik Quintel
  2002-05-14 13:08 ` Markus Knauss
  2002-05-14 13:12 ` Marin David Condic
@ 2002-05-14 14:36 ` Alfred Hilscher
  2002-05-14 17:58 ` Kai Schuelke
  3 siblings, 0 replies; 14+ messages in thread
From: Alfred Hilscher @ 2002-05-14 14:36 UTC (permalink / raw)




Henrik Quintel wrote:
> 
> Hi, as the most people know, in C there is the possibility to make use
> of a function pointer. In Ada this not possible. 

Not quite right.

> Does someone knows how
> is it possible to make use of such a feature like a function pointer in
> Ada?

Have a look at "access to subprograms".



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

* Re: Function pointer in Ada?
  2002-05-14 12:51 Function pointer in Ada? Henrik Quintel
                   ` (2 preceding siblings ...)
  2002-05-14 14:36 ` Alfred Hilscher
@ 2002-05-14 17:58 ` Kai Schuelke
  2002-05-14 18:01   ` Preben Randhol
                     ` (2 more replies)
  3 siblings, 3 replies; 14+ messages in thread
From: Kai Schuelke @ 2002-05-14 17:58 UTC (permalink / raw)


Hello,

I managed to get a function pointer (left, right : FLOAT) return FLOAT but I
couldn't manage to get a pointer to the according Standard."+". I tried
various ways but had to use a workaround. Is it possible to do something
like:

plus_pointer : pointer_type := access Standard."+";

Thanks in advance

Kai Schuelke






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

* Re: Function pointer in Ada?
  2002-05-14 17:58 ` Kai Schuelke
@ 2002-05-14 18:01   ` Preben Randhol
  2002-05-14 18:24   ` David C. Hoos
  2002-05-14 18:25   ` Marin David Condic
  2 siblings, 0 replies; 14+ messages in thread
From: Preben Randhol @ 2002-05-14 18:01 UTC (permalink / raw)


On Tue, 14 May 2002 19:58:23 +0200, Kai Schuelke wrote:
> Hello,
> 
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:

First why do you want to do this?

Preben



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

* Re: Function pointer in Ada?
  2002-05-14 17:58 ` Kai Schuelke
  2002-05-14 18:01   ` Preben Randhol
@ 2002-05-14 18:24   ` David C. Hoos
  2002-05-14 20:00     ` Kai Schuelke
  2002-05-14 18:25   ` Marin David Condic
  2 siblings, 1 reply; 14+ messages in thread
From: David C. Hoos @ 2002-05-14 18:24 UTC (permalink / raw)



----- Original Message -----
From: "Kai Schuelke" <kai.schuelke@gmx.net>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Tuesday, May 14, 2002 12:58 PM
Subject: Re: Function pointer in Ada?


> Hello,
>
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but
I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:
>
> plus_pointer : pointer_type := access Standard."+";
>
Why you would want to do this, I can't imageine, but here's how it can be
done:

First, since you are not allowed to take the 'Access attribute of an
intrinsic
function, I "wrapped" the Standard (intrinsic) function in a user-defined
function, then took the 'Access attribute of that.

The code to demonstrate it is here:

with Ada.Text_IO;
procedure Standard_Function_Access
is
   type Dyadic_Float_Access is access
     function (Left, Right : Float) return Float;
   function "+" (Left, Right : Float) return Float
   is
   begin
      return Standard."+" (Left, Right);
   end "+";
   Float_Plus_Access : constant Dyadic_Float_Access := "+"'Access;
begin
   Ada.Text_IO.Put_Line
     ("3.1 + 7.2 =" & Float'Image
      (Float_Plus_Access (3.1, 7.2)));
end Standard_Function_Access;

I also took the liberty of using "Adaesque" names for the declarations.




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

* Re: Function pointer in Ada?
  2002-05-14 17:58 ` Kai Schuelke
  2002-05-14 18:01   ` Preben Randhol
  2002-05-14 18:24   ` David C. Hoos
@ 2002-05-14 18:25   ` Marin David Condic
  2002-05-15 22:30     ` Kai Schuelke
  2 siblings, 1 reply; 14+ messages in thread
From: Marin David Condic @ 2002-05-14 18:25 UTC (permalink / raw)


That seems a sick and twisted thing to want to do! :-) Anyway, off the top
of my head, I'd guess that you can't do that. Ada tends to not let you have
much control over its standard stuff because compilers may want to treat it
very differently. (There may not be an actual function to point to, correct?
The compiler may see 'Standard."+"' and just generate an add instruction.)

However, you could define your own addition function that encapsulates the
standard "+" operator and try getting a pointer to that. It might let you do
what you want to do, but I suspect that whatever you are attempting to do,
there is a better way than with pointers to functions. Don't try to make Ada
look like C or C++ - learn to do it The Ada Way(tm). Perhaps if you describe
the problem you are trying to solve, you might get a better answer here.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com


"Kai Schuelke" <kai.schuelke@gmx.net> wrote in message
news:3ce15027.0@news.unibw-muenchen.de...
> Hello,
>
> I managed to get a function pointer (left, right : FLOAT) return FLOAT but
I
> couldn't manage to get a pointer to the according Standard."+". I tried
> various ways but had to use a workaround. Is it possible to do something
> like:
>
> plus_pointer : pointer_type := access Standard."+";
>






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

* Re: Function pointer in Ada?
  2002-05-14 18:24   ` David C. Hoos
@ 2002-05-14 20:00     ` Kai Schuelke
  0 siblings, 0 replies; 14+ messages in thread
From: Kai Schuelke @ 2002-05-14 20:00 UTC (permalink / raw)



"David C. Hoos" <david.c.hoos.sr@ada95.com> schrieb im Newsbeitrag
news:mailman.1021400702.30533.comp.lang.ada@ada.eu.org...

[..]

> Why you would want to do this, I can't imageine, but here's how it can be
> done:

[..]

That's how I did it. It was for a function-plotter. You put in a String and
you get back a pointer to a mathmatical function.

It looked like this. I hope you get a idea from this ut down version. If
anybody's interessted in it I can send the sources.

type Single_Operation is
    Op : Operator_Pointer;
    L    : Single_Operation_Pointer;
    R    : Single_Operation_Pointer;
    Parameter : FLOAT;
end record;

The mathematical function is represented with a tree of Single_Operation. To
solve the equation you just have to do a recursive call:

function solve(temp : Single_Operation_Pointer) return FLOAT is
begin
    if L /= null then
        return temp.Op.all(solve(L), solve(R));
    else
        return Parameter;
    end if;
end;

The program works, but it would be more elegant if I could get a pointer to
the Standard"+" without wrapping a extra function around it.

I thought it would be better to use pointers to functions because it is more
memory efficient and more flexible, too.

Have a good night!

Kai Schuelke








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

* Re: Function pointer in Ada?
  2002-05-14 18:25   ` Marin David Condic
@ 2002-05-15 22:30     ` Kai Schuelke
  2002-05-16 11:37       ` Marc A. Criley
  2002-05-16 12:53       ` Georg Bauhaus
  0 siblings, 2 replies; 14+ messages in thread
From: Kai Schuelke @ 2002-05-15 22:30 UTC (permalink / raw)


Hi,

I solved the problem like you suggested but I think that there must be a
better way to do it, too.

I want a program that can solve every mathematical function (e.g.: z = 2*x +
sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
function at a time. My spec looks like:

-- Spec
package math is
    procedure Set_Function(Func : in STRING);
    function Calculate(Value : in FLOAT) return FLOAT;
end math;

-- Sample program
with math; use math;
procedure sample is
    result : FLOAT;
begin
    Set_Function("2*x+sin(x)");
    result := Calculate(4.0);
end;

What would be the Ada way to do this?






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

* Re: Function pointer in Ada?
  2002-05-15 22:30     ` Kai Schuelke
@ 2002-05-16 11:37       ` Marc A. Criley
  2002-05-16 19:06         ` Kai Schuelke
  2002-05-16 12:53       ` Georg Bauhaus
  1 sibling, 1 reply; 14+ messages in thread
From: Marc A. Criley @ 2002-05-16 11:37 UTC (permalink / raw)


Kai Schuelke wrote:
> 
> I want a program that can solve every mathematical function (e.g.: z = 2*x +
> sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
> function at a time. My spec looks like:
> 
> -- Spec
> package math is
>     procedure Set_Function(Func : in STRING);
>     function Calculate(Value : in FLOAT) return FLOAT;
> end math;
> 
> -- Sample program
> with math; use math;
> procedure sample is
>     result : FLOAT;
> begin
>     Set_Function("2*x+sin(x)");
>     result := Calculate(4.0);
> end;
> 
> What would be the Ada way to do this?

If you're going to pass the function in as a String, as your package
shows, then you will have to parse that string into its constituent
operands and operations.. These would be stored perhaps in a tree
structure, then traverse the tree in the proper sequence, doing an
interpretive evaluation as you go.  (And watch for malformed functions!)

Marc A. Criley
Consultant
Quadrus Corporation
www.quadruscorp.com



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

* Re: Function pointer in Ada?
  2002-05-15 22:30     ` Kai Schuelke
  2002-05-16 11:37       ` Marc A. Criley
@ 2002-05-16 12:53       ` Georg Bauhaus
  1 sibling, 0 replies; 14+ messages in thread
From: Georg Bauhaus @ 2002-05-16 12:53 UTC (permalink / raw)


Kai Schuelke <kai.schuelke@gmx.net> wrote:
: 
: I want a program that can solve every mathematical function (e.g.: z = 2*x +
: sin(x)) for any value (e.g. x=4). It is enough to work with one mathematical
: function at a time. My spec looks like:

Maybe it's time to look at gnuplot or at a language that has 
EVAL or COMPILE.

-- Georg



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

* Re: Function pointer in Ada?
  2002-05-16 11:37       ` Marc A. Criley
@ 2002-05-16 19:06         ` Kai Schuelke
  2002-05-16 19:18           ` Preben Randhol
  0 siblings, 1 reply; 14+ messages in thread
From: Kai Schuelke @ 2002-05-16 19:06 UTC (permalink / raw)


I took the String a watched for the least significant Operation (binary
operations only at first), which is the root of my tree. I put the left side
of the String onto the left branch, same on the right side, until I get a
remaining number or a variable name.

y = 5*x + 3     becomes

Op:              +
                /        \
Op:        *          3    Num
            /    \
Num:   5    x       Var

Therefore I need a record to describe a leaf that consists of
- 2 pointers to the next leafs
- A storage, that indicates what the leaf is: a operation, a constant or a
variable

I  used:
type LEAF is record
    Left,
    Right : operation_pointer := null    -- thats why I started to post in
this thread
    Con  : FLOAT;
    Var   : BOOLEAN;
end record;

Does anybody see a more ada-like way to do it? Somebody wrote that this is C
style.

Thanks

Kai Schuelke   looking forward to learn something new.

PS: This program works just fine right know, but it is intended to learn
something about Ada. So please don't mind my asking for better ways.








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

* Re: Function pointer in Ada?
  2002-05-16 19:06         ` Kai Schuelke
@ 2002-05-16 19:18           ` Preben Randhol
  0 siblings, 0 replies; 14+ messages in thread
From: Preben Randhol @ 2002-05-16 19:18 UTC (permalink / raw)


On Thu, 16 May 2002 21:06:32 +0200, Kai Schuelke wrote:
> Does anybody see a more ada-like way to do it? Somebody wrote that this is C
> style.

Check:
   http://www.adapower.com/alg/calc.html

If you haven't alread looked at it.


Preben
-- 



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

end of thread, other threads:[~2002-05-16 19:18 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-14 12:51 Function pointer in Ada? Henrik Quintel
2002-05-14 13:08 ` Markus Knauss
2002-05-14 13:12 ` Marin David Condic
2002-05-14 14:36 ` Alfred Hilscher
2002-05-14 17:58 ` Kai Schuelke
2002-05-14 18:01   ` Preben Randhol
2002-05-14 18:24   ` David C. Hoos
2002-05-14 20:00     ` Kai Schuelke
2002-05-14 18:25   ` Marin David Condic
2002-05-15 22:30     ` Kai Schuelke
2002-05-16 11:37       ` Marc A. Criley
2002-05-16 19:06         ` Kai Schuelke
2002-05-16 19:18           ` Preben Randhol
2002-05-16 12:53       ` Georg Bauhaus

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