Page 1 of 1

Applied oop way by macro in purebasic

Posted: Sat Feb 07, 2009 4:20 am
by goldmate
Hi,everyone
I'm sure have a applied oop way that use standard purebasic syntax in purebasic language.
My oop include code:

Code: Select all

;OOP include file "Class.pbi"
;For Purebasic vesion 4.30+ 
;*********************************************************************
;Syntax:
;CLASS[EX](classname[,mothername])
;function1([arg1,...])
;      ...  if needed
;MEMBER[EX](classname[,mothername])
;memvar1.type
;      ...  if needed
;ENDCLASS(classname)
;--------------------KEY WORDS LIST-----------------------------------
;CLASS   
;CLASSEX   
;ENDCLASS
;MEMBER  
;MEMBEREX
;---------------------------------------------------------------------
;Class self own functions:
;*object.classname_=NewClass(classname)    ;create object
;*this\Release()                                   ;Free_classname()--\method[0]
;Init_Classname(*this.classname)         ;Initial Class method&member
;---------------------------------------------------------------------
;Class self own Structure:
;InterFace Classname_
;Structure Classname
;Structure member_classname
;*********************************************************************

Macro class(classname)
   Interface classname#_
      Release()
EndMacro

Macro member(classname)
   EndInterface
   Structure member_#classname
EndMacro

Macro endclass(classname)
   EndStructure
   Structure classname
      *vtable                      ;pointer to virtual function table
      method.i[SizeOf(classname#_)/SizeOf(integer)] ;addr of functions
      member.member_#classname
   EndStructure
   Declare Free_#classname(*this.classname)
   Declare Classname(classname)   
   Declare init_#classname(*instance.classname)
EndMacro

Macro classext(classname,mothername)
   Interface classname#_ Extends mothername#_
EndMacro

Macro memberext(classname,mothername)
   EndInterface
   Structure member_#classname Extends member_#mothername
EndMacro

Macro NewClass(classname)
   classname(classname)               ;creat object and allocate memory
EndMacro

The function named classname(classname) write by programer
ex:
Procedure classname(classname)                 ;must use this name
      Protected *this.classname
      *this=AllocateMemory(SizeOf(classname))
      If *this
         *this\vtable=*this+OffsetOf(classname\method)
         *this\method[0]=@Free_#classname()        ;must use this name
      EndIf
      init_#classname(*this)
      ProcedureReturn *this
EndProcedure

Show my oop sample:
XIncludeFile "class.pbi"
class(test)
   function1(message.s)
   function2()
member(test)
   member1.l
   member2.w
   member3.s
endclass(test)
Declare Message(*this,mg.s)
Declare Byby(*this,mg.s)
Procedure Init_test(*this.test)
   With *this
      \method[1]=@message()
      \method[2]=@byby()
   EndWith
   With *this\memb
      \member1=1
      \member2=2
      \member3="3"
   EndWith
EndProcedure

classext(extend,test)
   function3()
   memberext(extend,test)
   member4.s
endclass(extend)


Declare DBG(*instance.extend)

Procedure Init_extend(*this.extend)
   With *this
      \method[1]=@message()
      \method[2]=@byby()
      \method[3]=@dbg()
   EndWith
   With *this\memb
      \member1=1
      \member2=2
      \member3="3"
      \member4="I am a string."
   EndWith
EndProcedure

*myobj.extend_                                                             ;interface
*myobj=NewClass(extend)                                             ;creat a object
*myobj\function1("It's a test for OOP in Purebasic")          ;method1
*myobj\function2()                                                         ;method2
*myobj\function4()                                                         ;method4
*myobj\Release()                                                          ;destroy object
*myobj=0
End

procedure test(test)
...
endprocedure

procedure extend(extend)
...
endprocedure

procedure Free_test(*this.test)
...
endprocedure

procedure Free_extend(*this.extend)
...
endprocedure

procedure dbg(*this.extend)
...
endprocedure

procedure message(*this,msg.s)
...
endprocedure

procedure byby(*this,msg.s)
...
endprocedure
edited by Rings, set BBCode]

Posted: Sat Feb 07, 2009 6:09 pm
by Demivec
I found your implementation interesting.

If your "example" code was meant to be a working example then you will need to make some adjustments to each of the Init procedures.

The change is for the init_test, but it is also needed for init_extend.

Code: Select all

Procedure Init_test(*this.test)
   With *this
      \method[1]=@message()
      \method[2]=@byby()
   EndWith
   With *this\memb   ;<-- change to *this\member
      \member1=1
      \member2=2
      \member3="3"
   EndWith
EndProcedure
Your implementation would be better demonstrated if you actually had a working example.

Another correction to your example. You write that a programmer must supply a procedure named "classname(classname)" for allocated memory for a new instance of "classname". Shouldn't it actually be in the form of "classname()" ?

Polymorphism

Posted: Sun Feb 08, 2009 7:50 am
by goldmate
I am not solve Polymorphism.
code:
class(test)
fun1()
member(test)
var1.l
endclass(test)

classext(extend)
fun2()
memberext(extend)
var2.s
endclass(extend)

Define method fun1 with para *this.
Fun1() must application to base class(test) and inherited class(extend) both.
Which is *this pointer point to ?