79 lines
1.6 KiB
Plaintext
79 lines
1.6 KiB
Plaintext
;Enforced immutability Variable-Class
|
|
|
|
Interface PBVariable ; Interface for any value of this type
|
|
Get() ; Get the current value
|
|
Set(Value.i) ; Set (if allowed) a new value in this variable
|
|
ToString.s() ; Transferee the value to a string.
|
|
Destroy() ; Destructor
|
|
EndInterface
|
|
|
|
Structure PBV_Structure ; The *VTable structure
|
|
Get.i
|
|
Set.i
|
|
ToString.i
|
|
Destroy.i
|
|
EndStructure
|
|
|
|
Structure PBVar
|
|
*VirtualTable.PBV_Structure
|
|
Value.i
|
|
EndStructure
|
|
|
|
;- Functions for any PBVariable
|
|
Procedure immutable_get(*Self.PBVar)
|
|
ProcedureReturn *Self\Value
|
|
EndProcedure
|
|
|
|
Procedure immutable_set(*Self.PBVar, N.i)
|
|
ProcedureReturn #False
|
|
EndProcedure
|
|
|
|
Procedure.s immutable_ToString(*Self.PBVar)
|
|
ProcedureReturn Str(*Self\Value)
|
|
EndProcedure
|
|
|
|
Procedure DestroyImmutabe(*Self.PBVar)
|
|
FreeMemory(*Self)
|
|
EndProcedure
|
|
|
|
;- Init an OO-Table
|
|
DataSection
|
|
VTable:
|
|
Data.i @immutable_get()
|
|
Data.i @immutable_set()
|
|
Data.i @immutable_ToString()
|
|
Data.i @DestroyImmutabe()
|
|
EndDataSection
|
|
|
|
;- Create-Class
|
|
Procedure CreateImmutabe(Init.i=0)
|
|
Define *p.PBVar
|
|
*p=AllocateMemory(SizeOf(PBVar))
|
|
*p\VirtualTable = ?VTable
|
|
*p\Value = Init
|
|
ProcedureReturn *p
|
|
EndProcedure
|
|
|
|
;- **************
|
|
;- Test the Code
|
|
|
|
;- Initiate two Immutabe variables
|
|
*v1.PBVariable = CreateImmutabe()
|
|
*v2.PBVariable = CreateImmutabe(24)
|
|
|
|
;- Present therir content
|
|
Debug *v1\ToString() ; = 0
|
|
Debug *v2\ToString() ; = 24
|
|
|
|
;- Try to change the variables
|
|
*v1\Set(314) ; Try to change the value, which is not permitted
|
|
*v2\Set(7)
|
|
|
|
; Present the values again
|
|
Debug Str(*v1\Get()) ; = 0
|
|
Debug Str(*v2\Get()) ; = 24
|
|
|
|
;- And clean up
|
|
*v1\Destroy()
|
|
*v2\Destroy()
|