OOP using Module and Macro

Share your advanced knowledge/code with the community.
User avatar
eddy
Posts: 124
Joined: Thu Mar 27, 2014 8:34 am

OOP using Module and Macro

Post by eddy »

Code: Select all

DeclareModule ROBOT
  Prototype ROBOT_Move(x,y)       ;// auto generated method signature
  Structure ROBOT 
    model.s
    x.i:y.i  
    *Move.ROBOT_Move              ;// auto generated methods
  EndStructure
  
  Declare.i New(model.s,x=0,y=0)  ;// auto generated constructor signature
EndDeclareModule   

Module ROBOT
  Macro This(THIS_TYPE)  
    Protected THIS_TYPE
    !p_this=$(this)[0];
  EndMacro
  
  Procedure Move(x,y)
    This(*this.ROBOT)
    *this\x+x
    *this\y+y
  EndProcedure
  
  Procedure.i New(model.s,x=0,y=0)
    Protected new.ROBOT
    new\Model=model
    new\x=x
    new\y=y
    new\Move=@Move()
    ProcedureReturn new
  EndProcedure
EndModule 

; ***********************
; EXAMPLE
; ***********************
UseModule ROBOT

Define *terminator.ROBOT=ROBOT::New("T800",2,2)  
Define *t1000.ROBOT=ROBOT::New("T1000",2,2)

*terminator\Move(10,10)  
*t1000\Move(1,1)  
Debug *terminator\x
Debug *terminator\y
Debug *t1000\x
Debug *t1000\y