Module BaseClass (Module as Object)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

Recreated the baseclass.zip

Will probably switch off the AFP support for macOS, then no strange folders and files will be created.
You can also connect to an SMB server under macOS.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Module BaseClass (Module as Object)

Post by Kurzer »

:shock: Something went wrong!

Example4 and Example7 are broken in your zip archive.
The files are smaller than 201 bytes and 95% of the code is missing.

Also the separate, non compressed files on your webspace are broken in the same manner.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

Its empty code... 8)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Module BaseClass (Module as Object)

Post by Kurzer »

Indeed, but why you include code like this into the zip archive?

Code: Select all

;-TOP

; Example 4 - v1.13

IncludeFile "Modul_BaseClassSmall.pb"
But maybe I didn't get the joke. Sometimes I feel a little bit autistic :oops:
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

These were examples that no longer worked with the small version.

Sorry :!:

These will be filled in sometime
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

1.
I thought about how to separate public and private attributes.
To do this you have to create memory and store the pointer for the private attributes in the object. There is a new entry in the BaseSystem (*This\System\Private) and a fixed structure name (sPrivate)

2.
There are also class attributes (package attributes) that are valid for all objects in the class.
If necessary, you must create memory for them and store the pointer for the package attributes in the class.
There is a new entry for this in the class. (*This\System\Class\Package) and a fixed structure name (sPackage)
For initializing the package attributes there is a new macro (InitPackage) which requests the memory and allocates it to the class.

The class methods are programmed in the same way as object methods. But you can consider to program the class methods first and then inherit them to the object.

Here is a simple example.

Code: Select all

; Removed
Last edited by mk-soft on Thu May 28, 2020 11:52 am, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

Update v1.18
- Added macros for private attributes :wink:

* AllocatePrivate(...)
* FreePrivate(...)
* GetPrivate(...)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Module BaseClass (Module as Object)

Post by Kurzer »

mk-soft,
many thanks for the continuous development of your baseclass. And especially that it still gets along completely without precompiler. Image

Greetings
Kurzer
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

Update v1.19
- Bugfix private attributes for inheritance of objects.

Update v1.20
- Optimize Private (Initializations only if necessary)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5398
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Module BaseClass (Module as Object)

Post by mk-soft »

Update v1.21
- Removed Private Macros and Pointer

I didn't like the solution for Private Attributes and it was too complex and made it confusing.
So I removed this from the BaseClass.
Most of the time it is also better to inherit the attributes to make it easier to access

There are better methods to solve this if needed. For example with a map.

Code: Select all

;-TOP

; Example 4 - v1.21
; - Public and Private Attributes
; - Package Attributes

IncludeFile "Modul_BaseClassSmall.pb"

DeclareModule Cars
  
  ; Public Methods
  Interface iCars Extends BaseClass::iBaseClass
    getType.s()
    setType.s(Type.s)
    getColor.s()
    setColor(Color.s)
    getWheels()
    setWheels(Count)
    getDriver.s()
    setDriver(Driver.s)
    Count()
  EndInterface
  
  ; Public Attributes
  Structure sCars Extends BaseClass::sBaseClass
    Type.s
    Color.s
    Wheels.i
  EndStructure
  
  ; Create Object
  Declare New()
  
EndDeclareModule

Module Cars
  
  UseModule BaseClass
  
  NewClass(iCars)
  
  ; Define Package Attributes
  Structure sPackage
    Count.i
  EndStructure
  
  ; Init Package Attributes
  InitPackage()
  
  ; Define Private Attributes
  Structure sPrivate
    Driver.s
  EndStructure
  
  Global NewMap Private.sPrivate()
  
  ; ----
  
  ;- Private Methods
  
  Procedure Init(*This.sCars)
    ; Allocate memory for private attributes
    AddMapElement(Private(), Str(*This))
    ; Inc class counter
    *This\System\Class\Package\Count + 1
  EndProcedure : AsInitializeObject(Init)
  
  Procedure Dispose(*This.sCars)
    ; Free memory for private attributes
    DeleteMapElement(Private(), Str(*This))
    ; Dec class counter
    *This\System\Class\Package\Count - 1
  EndProcedure : AsDisposeObject(Dispose)
  
  ; ----
  
  ;- Public Object Methods
  
  Procedure.s getType(*This.sCars)
    ProcedureReturn *This\Type
  EndProcedure : AsMethode(getType)
  
  Procedure setType(*This.sCars, Type.s)
    *This\Type = Type
  EndProcedure : AsMethode(setType)
  
  ; ----
  
  Procedure.s getColor(*This.sCars)
    ProcedureReturn *This\Color
  EndProcedure : AsMethode(getColor)
  
  Procedure setColor(*This.sCars, Color.s)
    *This\Color = Color
  EndProcedure : AsMethode(setColor)
  
  ; ----
  
  Procedure getWheels(*This.sCars)
    ProcedureReturn *This\Wheels
  EndProcedure : AsMethode(getWheels)
  
  Procedure setWheels(*This.sCars, Wheels)
    *This\Wheels = Wheels
  EndProcedure : AsMethode(setWheels)
  
  ; ----
  
  Procedure.s getDriver(*This.sCars)
    ProcedureReturn Private(Str(*this))\Driver
  EndProcedure : AsMethode(getDriver)
  
  Procedure setDriver(*This.sCars, Driver.s)
    Private(Str(*This))\Driver = Driver
  EndProcedure : AsMethode(setDriver)
  
  ; ----
  
  ;- Public Class Method
  Procedure Count(*This.sCars)
    Protected *Package.sPackage = GetPackage()
    ProcedureReturn *Package\Count
  EndProcedure : AsMethode(Count)
  
  ; ----
  
  Procedure New()
    InitObject(sCars)
  EndProcedure
  
  CheckInterface()
  
EndModule

; ****

Define.Cars::iCars obj1, obj2, obj3

obj1 = Cars::New()
obj1\setType("Mercedes")
obj1\setColor("Green")
obj1\setWheels(4)
obj1\setDriver("Michael")

obj2 = Cars::New()
obj2\setType("Mercedes")
obj2\setColor("Gray")
obj2\setWheels(4)
obj2\setDriver("Tom")

obj3 = Cars::New()
obj3\setType("Ford")
obj3\setColor("Blue")
obj3\setWheels(4)
obj3\setDriver("Jerry")

Debug "Class Package - Count = " + obj1\Count()
obj2\Release()
Debug "Class Package - Count = " + obj1\Count()
obj3\Release()
Debug "Class Package - Count = " + obj1\Count()

obj3 = Cars::New()
obj3\setType("Ford")
obj3\setColor("Blue")
obj3\setWheels(4)
obj3\setDriver("Jerry")
Debug "Class Package - Count = " + obj1\Count()

Debug obj1\getType() + " - " + obj1\getDriver()
Debug obj3\getType() + " - " + obj3\getDriver()
[/size]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply