home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.pascal
- Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!ux1.cso.uiuc.edu!mp.cs.niu.edu!jeffbyrn
- From: jeffbyrn@mp.cs.niu.edu (Jeff Byrne)
- Subject: Calling Local Procedures - Problems
- Message-ID: <1992Dec21.233611.1711@mp.cs.niu.edu>
- Organization: Northern Illinois University
- Date: Mon, 21 Dec 1992 23:36:11 GMT
- Lines: 85
-
- { Hi,
- I'm having a problem that I hope someone can help me with. I want to
- have a method that takes an address to a procedure/function and calls
- that routine with some value. I have tried this a couple ways and have
- run into problems. It seems that I am having problems because I want the
- procedure/function to be local to another method. I don't know enough
- about assembler or the TP calling conventions to write the procedure
- I need.
-
- I know that it can be done because the ForEach and FirstThat methods in
- TVISION's TGroup object do this. However, I don't have the source to
- these units and can't seem to figure it out. I have written a small
- test program below that illustrates the problem and two attempted
- solutions. The answer is probably a relativly simple assembler routine,
- however, I lack knowledge in that area. I would appreciate any ideas.
- Thank you, and Happy Holidays.
- - Jeff
- jeffbyrn@mp.cs.niu.edu
- jeff@art.niu.edu }
-
- Program Problem;
-
- Uses
- Crt;
-
- Type
- PTestObj = ^TestObj;
- LocalMethodType = Procedure (P : PTestObj); { Procedure Type }
- TestObj = Object
- Procedure SetAll (Value : Word; Enable : Boolean);
- Procedure SetOne (Value : Word; Enable : Boolean);
- Procedure ForEach (Proc : LocalMethodType);
- Procedure ForEach2 (Proc : Pointer);
- end;
-
- Var
- Test : TestObj; { Global Variable }
-
- Procedure TestObj.SetAll (Value : Word; Enable : Boolean);
- Procedure SetLocal (P : PTestObj); Far; { LOCAL PROCEDURE }
- begin { Doesn't work with variables? }
- P^.SetOne (Value, Enable); { This doesn't send the right values }
- P^.SetOne (123, True); { This works - But I want to access }
- end;{ Works with literals } { SetAll's variables }
- begin
- { ForEach (SetLocal); } { This way won't work unless SetLocal is not Local }
- ForEach2 (@SetLocal);
- end;
-
- Procedure TestObj.SetOne (Value : Word; Enable : Boolean);
- begin
- If Enable then
- Writeln ('Value ',Value, ' is Enabled.')
- else
- Writeln ('Value ',Value, ' is Disabled.');
- end;
-
- { This method won't work if Proc is Local to a procedure }
- Procedure TestObj.ForEach (Proc : LocalMethodType);
- Var
- Count : Integer;
- begin
- For Count := 1 to 10 do
- begin
- Proc (@Test); { These would be different instances of the object }
- end;
- end;
-
- Procedure TestObj.ForEach2 (Proc : Pointer);
- Var
- P : LocalMethodType absolute Proc; { Cast as Procedure }
- Count : Integer;
- begin
- For Count := 1 to 10 do
- begin
- asm
- {???} Push AX { Push somthing because the subroutine eats one word too many }
- end;
- P (@Test); { These would be different instances of the object }
- end;
- end;
-
- begin { Main }
- Clrscr;
- Test.SetAll (123, True);
-