A Global Singleton is a class of which only one instance exists within a program.

Any attempt to use non-static members of the class involves performing operations on this one instance.




Global SingletonSemaphore=CreateSemaphore(1)
 
Interface OO_Interface    ; Interface for any value of this type 
  Get.i()        
  Set(Value.i) 
  Destroy()
EndInterface 
 
Structure OO_Structure ; The *VTable structure  
  Get.i
  Set.i
  Destroy.i
EndStructure 
 
Structure OO_Var  
  *VirtualTable.OO_Structure
  Value.i 
EndStructure 
 
Procedure OO_Get(*Self.OO_Var)
  ProcedureReturn *Self\Value
EndProcedure
 
Procedure OO_Set(*Self.OO_Var, n)
  *Self\Value = n
EndProcedure
 
Procedure CreateSingleton()
  If TrySemaphore(SingletonSemaphore)
    *p.OO_Var = AllocateMemory(SizeOf(OO_Var))
    If *p
      *p\VirtualTable = ?VTable
    EndIf
  EndIf
  ProcedureReturn *p
EndProcedure
 
Procedure OO_Destroy(*Self.OO_Var)
  FreeMemory(*Self)
  SignalSemaphore(SingletonSemaphore)
EndProcedure
 
DataSection
  VTable:
  Data.i @OO_Get()
  Data.i @OO_Set()
  Data.i @OO_Destroy()
EndDataSection