Ich habe eine Include-Datei (Download: ClassTools.pbi) mit einigen Makros und Funktion geschrieben, womit ich z.B. folgendes machen kann:
Code: Alles auswählen
XIncludeFile "ClassTools.pbi"
;=======================================================
; Define the Class (Interface) of FIRST_CLASS
DEFINE_CLASS(FIRST_CLASS)
SetName(Name.s)
GetName.s()
Release()
END_CLASS(FIRST_CLASS)
; Define Class data
DEFINE_CLASS_DATA(FIRST_CLASS)
Name.s
END_CLASS_DATA
; Class Methods
METHOD_1(FIRST_CLASS,SetName,l,Name.s)
CLASS_DATA\Name = Name
END_METHOD
METHOD_0(FIRST_CLASS,GetName,s)
METHOD_RETURN(CLASS_DATA\Name)
END_METHOD
METHOD_0(FIRST_CLASS,Release,l)
METHOD_RETURN(CLASS_RELEASE)
END_METHOD
; CLASS Constructor
CLASS_CONSTRUCTOR(FIRST_CLASS)
VTABLE(FIRST_CLASS,SetName)
VTABLE(FIRST_CLASS,GetName)
VTABLE(FIRST_CLASS,Release)
CONSTRUCTOR_INIT(FIRST_CLASS)
CLASS_DATA\Name = "Initial Name"
END_CONSTRUCTOR(FIRST_CLASS)
;=======================================================
;Test FIRST_CLASS
Debug "--------------------------------"
Debug "Test of FIRST_CLASS"
Debug "--------------------------------"
oClass.IClass(FIRST_CLASS) = NEW_OBJECT(FIRST_CLASS)
Debug oClass\GetName()
oClass\SetName("New Name")
Debug oClass\GetName()
Debug oClass\Release() ; returns #True if successful
;=======================================================
; Define the Class (Interface) of SECOND_CLASS
DEFINE_CLASS(SECOND_CLASS) Extends EXTEND_CLASS(FIRST_CLASS)
; need the Extends keyword becaus of a macro bug!
; otherwise the Extends keyword would be in ClassTools.pbi
SetCountry(Country.s)
GetCountry.s()
END_CLASS(SECOND_CLASS)
; Define Class data
DEFINE_CLASS_DATA(SECOND_CLASS) Extends EXTEND_CLASS_DATA(FIRST_CLASS)
; need the Extends keyword becaus of a macro bug!
; otherwise the Extends keyword would be in ClassTools.pbi
Country.s
END_CLASS_DATA
; Class Methods
METHOD_1(SECOND_CLASS,SetCountry,l,Country.s)
CLASS_DATA\Country = Country
END_METHOD
METHOD_0(SECOND_CLASS,GetCountry,s)
METHOD_RETURN(CLASS_DATA\Country)
END_METHOD
METHOD_0(SECOND_CLASS,Release,l)
METHOD_RETURN(CLASS_RELEASE)
END_METHOD
; CLASS Constructor
CLASS_CONSTRUCTOR(SECOND_CLASS,FIRST_CLASS) ; Second class name is the one
; which should be extended
VTABLE(SECOND_CLASS,SetCountry)
VTABLE(SECOND_CLASS,GetCountry)
VTABLE(SECOND_CLASS,Release) ; Replace the Release() function of
; FIRST_CLASS With new one
CONSTRUCTOR_INIT(SECOND_CLASS)
CLASS_DATA\Country = "Initial Country"
END_CONSTRUCTOR(SECOND_CLASS)
;=======================================================
;Test SECOND_CLASS
Debug "--------------------------------"
Debug "Test of SECOND_CLASS"
Debug "--------------------------------"
oClass2.IClass(SECOND_CLASS) = NEW_OBJECT(SECOND_CLASS)
Debug oClass2\GetName()
Debug oClass2\GetCountry()
oClass2\SetName("New Name")
oClass2\SetCountry("New Country")
Debug oClass2\GetName()
Debug oClass2\GetCountry()
Debug oClass2\Release() ; returns #True if successful
;=======================================================
; Debuger output:
; --------------------------------
; Test of FIRST_CLASS
; --------------------------------
; Initial Name
; New Name
; 1
; --------------------------------
; Test of SECOND_CLASS
; --------------------------------
; Initial Name
; Initial Country
; New Name
; New Country
; 1
=> Danilos OOP Beispiele
Einige Anmerkungen:
- Two macros in one line ...
Wenn das funktioniert, dann kann ich noch ein wenig vereinfachen (siehe auch Kommentare im Beispiel oben). - FreeStructureStrings as normal PB FUNCTION/COMMAND
Wegen einem "Memory Leak" werden durch das Relase die Strings noch nicht freigegeben, weil sich diese in einer Struktur befinden, die dynamisch angelegt wurde. Ich hab' zwar einige Tests mit dem _SYS_FreeStructureStrings gemacht ... das führte aber immer wieder zu abstürzen. - Macro with wildcard for arguments
Mit diesem Feature könnte ich noch einiges vereinfachen!
Zur Zeit braiche ich noch die Makros METHOD_0, METHOD_1, METHOD_2, ... um Methoden mit 0, 1, 2 oder mehr Argumenten zu erstellen.
Die aktuelle Include-Datei unterstützt zur Zeit auch nur NEW_OBJECT(ClassName) ohne Initialisierungs-Parameter. Das wäre mit "Wildcard argumenten recht einfach zu lösen.
Habt Ihr Vorschläge für Verbesserungen?
cu, helpy
[edit]
Download-Link für ClassTools.pbi hinzugefügt ... siehe oben.
[/edit]