- OOP module to provide some helpers
- Make code simple and readable
- No vtable, No interface
- Using prototype trick : http://www.purebasic.fr/english/viewtopic.php?p=382866
- Using macro trick : http://www.purebasic.fr/english/viewtop ... 12&t=56522
- For PB 5.30+
- Example1 Features:
- public get/set methods
- constructor
- instanciate/free object instance
- private method used by public method
- overriden default FreeInstance method
- static method to count objects
- class inheritance
Code: Select all
DeclareModule OOP
Macro _CONCAT(a=, b=) ; helpers used to escape some MACRO variable
a#b
EndMacro
Macro _DQUOTE ; helpers used to build MACRO string
"
EndMacro
Macro _COLON ; helpers used to construct nested MACRO
:
EndMacro
Macro UseInstanceType(T) ; determine which instance TYPE will be implemented
Procedure.i DefaultFreeInstance_#T()
UseInstance(*this.T)
ClearStructure(*this.T, T)
ProcedureReturn #Null
EndProcedure
UndefineMacro _CONCAT(INSTANCE, _TYPE)
_COLON#Macro _CONCAT(INSTANCE, _TYPE)_COLON#T#_COLON#EndMacro
EndMacro
Macro Instanciate(This, ParentType=OOP::OBJECT, InstanciateParent=#Null) ; create NEW instance
Protected *parent.ParentType=InstanciateParent
Protected This=AllocateStructure(INSTANCE_TYPE)
If *parent
CopyStructure(*parent, This, ParentType)
This\InstanceParentType$=*parent\InstanceType$
Else
This\InstanceParentType$="OBJECT"
EndIf
This\InstanceType$=_DQUOTE#INSTANCE_TYPE#_DQUOTE
This\FreeInstance=@DefaultFreeInstance_#INSTANCE_TYPE()
This\DefaultFreeInstance=@DefaultFreeInstance_#INSTANCE_TYPE()
EndMacro
Macro SetInstanceMethod(Method) ; init NEW instance method
*this\Method=@Method()
EndMacro
Macro UseInstance(This) ; provide THIS instance to module method
CompilerIf Not Defined(_CONCAT(*th, is), #PB_Variable)
Protected This
CompilerIf #PB_Compiler_Processor=#PB_Processor_x64
! MOV [p.p_this], Rbp
CompilerElse
! MOV [p.p_this], Ebp
CompilerEndIf
CompilerEndIf
EndMacro
Prototype.i FreeInstance()
Structure OBJECT ; instance base TYPE
InstanceParentType$
InstanceType$
FreeInstance.FreeInstance
DefaultFreeInstance.FreeInstance
EndStructure
EndDeclareModule
Module OOP : EndModule
Macro FreeInstance(This)
This=This\OOP::_CONCAT(Free, Instance)()
EndMacro
CompilerIf #PB_Compiler_IsMainFile
; ***********************
; EXAMPLE 1
; ***********************
DeclareModule USER
UseModule OOP
Prototype SetName(name.s)
Prototype.s GetName()
Structure USER Extends OBJECT
;- public methods
SetName.SetName
GetName.GetName
;- public properties
Name$
EndStructure
;- constructor
Declare.i InstanciateUSER()
EndDeclareModule
Module USER
UseModule OOP
UseInstanceType(USER)
Procedure.s GetName()
UseInstance(*this.USER)
ProcedureReturn *this\Name$
EndProcedure
Procedure SetName(name.s)
UseInstance(*this.USER)
*this\Name$=name
EndProcedure
Procedure.i InstanciateUSER()
Instanciate(*this.USER)
SetInstanceMethod(GetName)
SetInstanceMethod(SetName)
*this\Name$="No name"
ProcedureReturn *this
EndProcedure
EndModule
;////// create and manipulate user object
UseModule USER
Define *user.USER=InstanciateUSER()
*user\SetName("Toto")
Debug *user\GetName()
FreeInstance(*user)
CompilerEndIf