home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / pascal / 7610 < prev    next >
Encoding:
Text File  |  1992-12-22  |  3.0 KB  |  95 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!elroy.jpl.nasa.gov!sdd.hp.com!ux1.cso.uiuc.edu!mp.cs.niu.edu!jeffbyrn
  3. From: jeffbyrn@mp.cs.niu.edu (Jeff Byrne)
  4. Subject: Calling Local Procedures - Problems
  5. Message-ID: <1992Dec21.233611.1711@mp.cs.niu.edu>
  6. Organization: Northern Illinois University
  7. Date: Mon, 21 Dec 1992 23:36:11 GMT
  8. Lines: 85
  9.  
  10. {   Hi,
  11.   I'm having a problem that I hope someone can help me with.  I want to
  12.   have a method that takes an address to a procedure/function and calls
  13.   that routine with some value.  I have tried this a couple ways and have
  14.   run into problems.  It seems that I am having problems because I want the
  15.   procedure/function to be local to another method.  I don't know enough
  16.   about assembler or the TP calling conventions to write the procedure
  17.   I need.
  18.  
  19.   I know that it can be done because the ForEach and FirstThat methods in
  20.   TVISION's TGroup object do this.  However, I don't have the source to
  21.   these units and can't seem to figure it out.  I have written a small
  22.   test program below that illustrates the problem and two attempted
  23.   solutions.  The answer is probably a relativly simple assembler routine,
  24.   however, I lack knowledge in that area.  I would appreciate any ideas.
  25.   Thank you, and Happy Holidays.
  26.   - Jeff
  27.   jeffbyrn@mp.cs.niu.edu
  28.   jeff@art.niu.edu  }
  29.  
  30. Program Problem;
  31.  
  32. Uses
  33.   Crt;
  34.  
  35. Type
  36.   PTestObj = ^TestObj;
  37.   LocalMethodType = Procedure (P : PTestObj); { Procedure Type }
  38.   TestObj = Object
  39.     Procedure SetAll (Value : Word; Enable : Boolean);
  40.     Procedure SetOne (Value : Word; Enable : Boolean);
  41.     Procedure ForEach (Proc : LocalMethodType);
  42.     Procedure ForEach2 (Proc : Pointer);
  43.   end;
  44.  
  45. Var
  46.   Test : TestObj; { Global Variable }
  47.  
  48. Procedure TestObj.SetAll (Value : Word; Enable : Boolean);
  49.    Procedure SetLocal (P : PTestObj); Far;  { LOCAL PROCEDURE }
  50.    begin    { Doesn't work with variables? }
  51.      P^.SetOne (Value, Enable); { This doesn't send the right values }
  52.      P^.SetOne (123, True);     { This works - But I want to access }
  53.    end;{ Works with literals }  { SetAll's variables }
  54. begin
  55. {  ForEach (SetLocal); } { This way won't work unless SetLocal is not Local }
  56.   ForEach2 (@SetLocal);
  57. end;
  58.  
  59. Procedure TestObj.SetOne (Value : Word; Enable : Boolean);
  60. begin
  61.   If Enable then
  62.     Writeln ('Value ',Value, ' is Enabled.')
  63.   else
  64.     Writeln ('Value ',Value, ' is Disabled.');
  65. end;
  66.  
  67. { This method won't work if Proc is Local to a procedure }
  68. Procedure TestObj.ForEach (Proc : LocalMethodType);
  69. Var
  70.   Count : Integer;
  71. begin
  72.   For Count := 1 to 10 do
  73.     begin
  74.       Proc (@Test);  { These would be different instances of the object }
  75.     end;
  76. end;
  77.  
  78. Procedure TestObj.ForEach2 (Proc : Pointer);
  79. Var
  80.   P : LocalMethodType absolute Proc; { Cast as Procedure }
  81.   Count : Integer;
  82. begin
  83.   For Count := 1 to 10 do
  84.     begin
  85.       asm
  86. {???}   Push AX { Push somthing because the subroutine eats one word too many }
  87.       end;
  88.       P (@Test);  { These would be different instances of the object }
  89.     end;
  90. end;
  91.  
  92. begin { Main }
  93.   Clrscr;
  94.   Test.SetAll (123, True);
  95.