HELP! ForEach/FirstThat methods 
Author Message
 HELP! ForEach/FirstThat methods

Hi all

I'm stuck on trying to build a similar FirstThat routine to the one found
in turbovision's collection object.  However, I think it's my understanding
of using procedures within other procedures (poor) and assembler (awful) which
is fouling me up.

What happens is that the following object method cannot access the DataID
parameter passed to the GetHookForID. What is particularly wierd is that
the IDE *does* see it when I step through it.  And if I try to access any
variables (ie B) it cannot "see" them, and if I try to modify them I can
get all kinds of {*filter*} crashes.  How do I get this TestHookID method to
access the variables from GetHookFOrID?  The bit of assembler in .FirstThat
is cobbled out of the objects.pas unit.

I know it *can* be done because the TCollection object does it...

I hope there's enough here for some expert to just spot the problem - I
can't post the whole lot as there's a lot of code, but if anyone wants I'll
try and put together a self-contained example to demonstrate it.

Please please give a little help - it'll save me a lot of trouble and botched
code that I'm using 'cos I can't use this.

Thank you very much

Martin

{returns Node pointing to given DataID}
function TNodeStream.GetNodeForID(const FirstNodeID, DataID : longint) :
PNode;var B : boolean;

  function TestNodeID(Node : PNode) : boolean; far;
  var B2 : boolean;
  begin
    B2 := (Node^.DataID = DataID);
    TestNodeID := B2;
  end;

begin

end;

{returns first node that matches passed test condition}
function TNodeStream.FirstThat;

  {recursive part to firstthat}
  function DoNode(Test : pointer; NodeID : longint) : PNode;
  var FoundNode : PNode;
      FOund : boolean;
      Node : PNode;
  begin
    FoundNode := nil;
    while (FoundNode=nil) and (NodeID<>-1) do begin
      Node := PNode(GetAt(NodeID));

      asm
        {put the node parameter on the stack}
        LES     DI,Node
        PUSH    ES
        PUSH    DI

        PUSH    WORD PTR [BP] {no idea - used for returning?}
        CALL    Test                                    {Call test given}
        MOV     Found,AL                        {load answer - from AL to Found}
      end;

      if Found then
        FoundNode := Node
      else begin
        if (Node^.Expanded) and (Node^.ChildID<>-1) then
           FoundNode := DoNode(Test, Node^.ChildID); {do children}
        NodeID := Node^.NextID;
        dispose(Node, done);
      end;
    end;

    DoNode := FoundNode;
  end;

begin
  FirstThat := DoNode(Test, RootNodeID); {start at root}
end;

---- end ---------



Sun, 28 Feb 1999 03:00:00 GMT  
 HELP! ForEach/FirstThat methods


Quote:
> Hi all

> I'm stuck on trying to build a similar FirstThat routine to the one found
> in turbovision's collection object.  However, I think it's my understanding
> of using procedures within other procedures (poor) and assembler (awful) which
> is fouling me up.

[...]

Quote:

>         PUSH    WORD PTR [BP] {no idea - used for returning?}

No, this is a reference to parameters and local variables of
GetNodeForID. Every nested procedure gets this hidden parameter because
there is no other way to reference local vars of upper level procedure.
When CPU enters FirstThat function, BP points to the value that must be
passed to Test, *BUT* this is true only inside the main part of
FirstThat, not here. Everything is right, but this code can not be in a
local procedure.

Quote:
> begin
>   FirstThat := DoNode(Test, RootNodeID); {start at root}
> end;

... and this is, of course, wrong: do everything HERE, but not in DoNode.

Bye,
Hovik Melikian.



Sun, 28 Feb 1999 03:00:00 GMT  
 HELP! ForEach/FirstThat methods

Hmm, it didn't really work as it's recursive.

I've posted the details in the comp.lang.Pascal.borland newsgroup if you're
interested in following it up.  And apologies, for some reason I remembered
your name as Frank, so substitute Frank for Hovik throughout the message,
sorry about that, only realised after I'd posted it.  Stupid
stupid.



Mon, 01 Mar 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. HELP! ForEach/FirstThat methods

2. ForEach and traversal methods in OOP data structures

3. ForEach and traversal methods in OOP data structures

4. Transaction Methods in Interbase using Delphi 3.0 -Problems in commit-rollback methods

5. Temporary Tables / FOREACH

6. FPC: Wrong behavior in nested use of TCollection.ForEach

7. Bug in tCollection.ForEach?

8. Need help: got error trying to override a virtual method

9. Problem with AfterInsert Method, help needed...

10. Problem with Insert Method, Please Help...

11. HELP: CONNECT METHOD!

12. Search method in Delphi - Help!

 

 
Powered by phpBB® Forum Software