SyntaxBomb - Indie Coders
Languages & Coding => Blitz Code Archives => Algorithms => Topic started by: BlitzBot on June 29, 2017, 00:28:42
Title : TFactory Class
Author : Bobysait
Posted : 1+ years ago
Description : Similar to TMap, as it is an extension from TMap
TFactory override the Insert method to rename automatically the keys.
So every entries are single
It can be used to store Brushes/Materials/Textures like TV3D do, and many else.
It is fully "bbDoced" and is coded to be safe for users
-> use of private / public to protect wrong use of the __TFactory__
Code :
SuperStrict
'#region TFactory
'#region Factory Base Type
' - Protected from user -
Private
Type __TFactory__ extends tmap
Field _AutoInc :Int = 0
Field _Default :String = ""
Global _LastName :String = ""
' - generate a valid unique name to insert in a factory hashmap -
Method AutoName :String ( Name:String="" )
' if empty name -> fill it with the default name
If Name="" Then Name = _Default
Local OrigName:String = Name
' check if the map countais this name
If Not(Contains(Name)) Then If Name<>_Default Then Return Name
Repeat
Name = OrigName+_AutoInc
If Not(Contains(Name)) Then Return Name
_AutoInc :+ 1
If _AutoInc=0 Then Return ""
Forever
Return ""
End Method
Method Insert ( Key:Object,Value:Object )
Insert2 ( String(Key),Value )
End Method
' - Insert a new object in the factory -
Method Insert2 :String ( Name:String, obj:Object, autoname:Byte=True )
_LastName = Name
' no empty name
If Name=""
If autoname=False Then Return ""
_LastName = Self.autoname ( Name )
EndIf
' no entries with same name
If Contains ( _LastName )
If autoname=False Then Return ""
_LastName = Self.autoname ( _LastName )
EndIf
' remove old link to the Hack map
Remove ( Name )
' insert the name in the hash- map
Super.Insert ( _LastName, obj )
Return _LastName
End Method
End Type
'#end region
'#region public Factory
' - Public factory SDK -
Public
Rem
bbdoc: TFactory class is an extension of TMap
about: TFactory is an extension of TMap, and provide an easy way to deal with "unique" named object
EndRem
Type TFactory Extends __TFactory__
Rem
bbdoc: Create a factory object
about: create a factory with parameters @DefaultName as the defaultname for autonaming and @CounterStart for auto-increment
EndRem
Function Create :TFactory ( DefaultName:String, CounterStart:Int = 0 )
Local factory:TFactory = New TFactory
factory._AutoInc = CounterStart
factory._Default = DefaultName
Return factory
End Function
Rem
bbdoc: Insert an object in the specified @factory
about: insert an object initialised with the name @Name, the name will then be automaticaly formated to be unique
check the formated name with #FactorylastRegistered
endrem
Method InsertName :String ( Name:String, obj:Object, autoname:Byte=True )
Return Super.Insert2( Name,obj,autoname)
End Method
Rem
bbdoc: Return the last registered name inserted in the specified @factory
about:
endrem
Method LastRegistered :String ( )
Return _LastName
End Method
End Type
Rem
bbdoc: Create a factory object
about: create a factory with parameters @DefaultName as the defaultname for autonaming and @CounterStart for auto-increment
EndRem
Function CreateFactory :TFactory( DefaultName:String="", CounterStart:Int = 0 )
Return TFactory.Create(DefaultName,CounterStart)
End Function
Rem
bbdoc: Insert an object in the specified @factory
about: insert an object initialised with the name @Name, the name will then be automaticaly formated to be unique
check the formated name with #FactorylastRegistered
endrem
Function FactoryInsert :String ( Factory:TFactory, Obj:Object, Name :String, autoname:Byte = True )
Return Factory.InsertName ( Name, Obj, autoname )
End Function
Rem
bbdoc: Return the last registered name inserted in the specified @factory
about:
endrem
Function FactorylastRegistered:String(factory:TFactory)
Return factory.LastRegistered()
End Function
'#end region
'#end region
Comments :
Bobysait(Posted 1+ years ago)
And here is a small exemple on how to use it' - Sample -
' Create a factory -> default name is "Material_"
Local Myfactory :TFactory = CreateFactory ( "Material_", 0 )
' register some objects ( we'll use string objetcs for this sample )
For Local nb:Int = 1 To 10
' name is automatic
Myfactory.Insertname( "", String(nb) )
' print the automatic registered name
Print Myfactory.LastRegistered()
Next
' get the last name registered -> use the factory as a tmap using LastRegistered() as key
Local Name1:String = String(MyFactory.ValueForKey(MyFactory.LastRegistered()))
Print "Name1="+Name1
' let's try to register an object with an existing name ( "Material_5" should have been registered with the previous instertions )
Local Obj:String="My Obj"
' I can use the TMap method "Insert", as it has been override to match the TFactory SDK
Myfactory.Insert ( "Material_5",Obj)
' Let's get the name registered for "Material_5"
Local RegName:String = FactorylastRegistered( Myfactory )
Local Name2:String = String(MyFactory.ValueForKey(RegName))
Print "Name2="+Name2
Print "Registered name = "+RegName
' delete Factory ^^
Myfactory = Null