ProcPtr usage 
Author Message
 ProcPtr usage

Hello. I have a syntax question that's been eluding me for a quite
some time. How does one actually code procPtrs and call them etc...? I
my instance I'm writing some portable code that the user should have
control over the functions which process certain aspects, I.e. the UPP
callbacks we see all the time.

theFunction: ProcPtr
...

...
call(theFunction).  A function which would call that is what I'm looking
for, I think...

Is this done with strict code or do I need some API of sorts? I looked
over apples code and found the mixed mode manager, but it was out of
date. Additionally I didn't feel it was the right approach. Any help is
welcome. Thank you.



Sat, 24 Sep 2005 03:11:37 GMT  
 ProcPtr usage

Quote:

> Hello. I have a syntax question that's been eluding me for a quite
> some time. How does one actually code procPtrs and call them etc...? I
> my instance I'm writing some portable code that the user should have
> control over the functions which process certain aspects, I.e. the UPP
> callbacks we see all the time.

> theFunction: ProcPtr
> ...

> ...
> call(theFunction).  A function which would call that is what I'm looking
> for, I think...

You don't name your compiler (but you say something about Apple), but simply try:

theFunction(args);



Sat, 24 Sep 2005 04:24:31 GMT  
 ProcPtr usage

I'm using CodeWarrior, but I think you misunderstand. The fuction which
needs to be called  is DoThis, but it is referenced from theFunction.
Some how the compiler must know this and invoke the variable theFunction
AS an actual function. Does that make more sense?


Quote:

>>Hello. I have a syntax question that's been eluding me for a quite
>>some time. How does one actually code procPtrs and call them etc...? I
>>my instance I'm writing some portable code that the user should have
>>control over the functions which process certain aspects, I.e. the UPP
>>callbacks we see all the time.

>>theFunction: ProcPtr
>>...

>>...
>>call(theFunction).  A function which would call that is what I'm looking
>>for, I think...

>You don't name your compiler (but you say something about Apple), but simply try:

>theFunction(args);



Sat, 24 Sep 2005 05:22:10 GMT  
 ProcPtr usage

[quoting fixed]

Quote:

>>>...
>>>call(theFunction).  A function which would call that is what I'm looking
>>>for, I think...

>>You don't name your compiler (but you say something about Apple), but simply try:

>>theFunction(args);

> I'm using CodeWarrior, but I think you misunderstand. The fuction which
> needs to be called is DoThis, but it is referenced from theFunction.

I'm not familiar with CodeWarrior, I answer from experience with Borland
compilers. (which have slightly extended standard Pascal in procedure variabele syntax)

Quote:
> Some how the compiler must know this and invoke the variable theFunction
> AS an actual function. Does that make more sense?

That is exactly what I meant.

At least if "TheFunction" is a procedure variabele type (same as ptr to address
of function on assembler level)

If not, and TheFunction is really a true pointer,  define the procedure
variabele type with all modifiers (cdecl in the example) and typecast to it:

type prototypeofthefunction=function(x,y:longint):integer; cdecl;

Begin

 result:=prototypeofthefunction(thefunction)(x,y);      
                // equivalent to DoThis(x,y) if dothis is a c-function;
End.

or, if thefunction already has the correct type:

begin

 result:=thefunction(x,y);
end.



Sat, 24 Sep 2005 05:35:20 GMT  
 ProcPtr usage
The following code example adheres to the extended ANSI standard for passing
Procedure parameters (or at least I think it does -- it worked with two
ANSI-compliant compilers).

Here's the scenario -- I had built a data structure corresponding to a graph
consisting of boxes and arrows pointing to the next box.  At various times
in the program, I needed to do a computation on each box.  The procedure
"OncePerBox" knows how to "walk" the data structure, but doesn't know what
to do with each box.  It is passed a parameter, "DoSomething", that does
whatever needs to be done.

For simplicity, I'm just listing part of the code here.

{ The procedure "onceperbox" is used to traverse the graph,
      visiting each box once }

PROCEDURE onceperbox (inbox : boxptr;
          PROCEDURE dosomething (box : boxptr));

  VAR
    boxstack : stackptr;

  PROCEDURE traversegraph (node : nodeptr);

    { walks graph }

    PROCEDURE usebox (box : boxptr);

    { use box if not already used nor Outbox }

    BEGIN   { usebox }
      WITH box^ DO
       IF NOT (boxonstack (box, boxstack) OR (tonode = NIL))
        THEN
         BEGIN
           push (box, boxstack);
           dosomething (box);
           traversegraph (tonode)
         END
    END;

  BEGIN   { traversegraph }
   IF node <> NIL
    THEN WITH node^ DO
     BEGIN
       usebox (thisbox);
       traversegraph (nextbox)
     END
  END;

BEGIN   { onceperbox }
  boxstack := NIL;
  traversegraph (inbox^.tonode);
  WHILE boxstack <> NIL DO pop (boxstack)
END;

The calls to this procedure are as follows:

  onceperbox (inbox, computeaterm)

which walks the graph doing computations defined by the box contents and

BEGIN   { showboxes }
 onceperbox (inbox, showbox)
END;

which (as the name implies) walks the graph and lists the contents of the
boxes for the viewer.

Bob Schor
Pascal Enthusiast

Quote:

> Hello. I have a syntax question that's been eluding me for a quite
> some time. How does one actually code procPtrs and call them etc...? I
> my instance I'm writing some portable code that the user should have
> control over the functions which process certain aspects, I.e. the UPP
> callbacks we see all the time.

> theFunction: ProcPtr
> ...

> ...
> call(theFunction).  A function which would call that is what I'm looking
> for, I think...

> Is this done with strict code or do I need some API of sorts? I looked
> over apples code and found the mixed mode manager, but it was out of
> date. Additionally I didn't feel it was the right approach. Any help is
> welcome. Thank you.



Tue, 27 Sep 2005 03:49:33 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. D1 Database & Record Usage

2. Q : Table Repair - tutil32.dll usage details available ?

3. Research into usage of Delphi

4. Problem with simple(?) usage of TDBRichEdit

5. Timestamp usage in SQL-statement.

6. Details of TTable memory usage, Please!

7. NLS usage in Delphi1.0 with Oracle?

8. Usage of Fork and other process management functions:

9. Two questions on Constructors/Destructors usage (in FreePascal)

10. FPC, TMT, and memory usage.

11. Component usage question

12. DbiDoRestructure call usage

 

 
Powered by phpBB® Forum Software