Page 3 of 3

Re: OOP structure style

Posted: Sun Jun 21, 2020 10:18 am
by Rinzwind
First post updated with interface/structure combo.

All inherit from base object class:

Code: Select all

;{-Base Class
;-Base Class
Interface Object
  Free()
EndInterface

Structure _ObjectData
  *vt
EndStructure

Structure _ObjectMethods
  Free.i
EndStructure
Global _ObjectMethods._ObjectMethods

Procedure _ObjectFree(*This._ObjectData)
  ;Protected *methods.Object = *This
  FreeStructure(*This)
EndProcedure

Procedure NewObject(*Data._ObjectData = 0)
  Protected *methods._ObjectMethods
  If *Data = 0: *Data = AllocateStructure(_ObjectData): EndIf
  If *Data\vt = 0: *Data\vt = _ObjectMethods: EndIf  
  *methods = *Data\vt
  With *methods
    If \Free = 0: \Free = @_ObjectFree(): EndIf
  EndWith
  With *Data
    \vt = *methods
  EndWith
  ProcedureReturn *Data
EndProcedure

Macro FreeAndNull(_Data)
  If _Data <> 0: _Data\Free(): _Data = 0: EndIf
EndMacro
;}
Also makes some neat tricks possible. Replace a method of a class when needed, access data directly by assigning interface to structure var (casts would be welcome addition here...).