Store info about two objects' relationship

BlitzMax Forums/BlitzMax Programming/Store info about two objects' relationship

An array won't work because you have to be able to create and destroy objects on the fly.

Check this out. You can store an object for any two objects.

Can Mark or someone who understand this better tell me whether this is great or stupid? What do I need to do to clean up memory for this?

Global ObjectMap:TMap=New TMap

Type TParternship
	Field o1:Object
	Field o2:Object
	Field h1:Int
	Field h2:Int
	
	Method Compare:Int( with:Object )
		p2:TParternship=TParternship(with)
		If h1>p2.h1 Return 1
		If h1<p2.h1 Return -1
		If h2>p2.h2 Return 1
		If h2<p2.h2 Return -1		
		Return 0
	End Method
	
	Method Delete()
		Release h1
		Release h2
		o1=Null
		o2=Null
	EndMethod
	
	Function Create:TParternship(o1:Object,o2:Object)
		p:TParternship=New TParternship
		p.o1=o1
		p.o2=o2
		p.h1=HandleFromObject(o1)
		p.h2=HandleFromObject(o2)
		If p.h1>p.h2
			h3=p.h2
			p.h2=p.h1
			p.h1=h3
			o3:Object=p.o2
			p.o2=p.o1
			p.o1=o3
		EndIf
		Return p
	EndFunction
	
EndType

Function SetObjectPartnership(o1:Object,o2:Object,link:Object)
	p:TParternship=TParternship.Create(o1,o2)
	If link=Null
		ObjectMap.Remove p
	Else
		ObjectMap.Insert p,link
	EndIf
EndFunction

Function GetObjectPartnership:Object(o1:Object,o2:Object)
	p:TParternship=TParternship.Create(o1,o2)
	Return ObjectMap.ValueForKey(p)
EndFunction

Function ClearObjectPartnerShips(o:Object)
	For p:TParternship=EachIn ObjectMap.Values()
		If p.o1=o Or p.o2=o ObjectMap.Remove p
	Next
EndFunction

'===================================================================
'Test
'===================================================================

Type TA
EndType

a:TA=New TA
b:TA=New TA
c:TA=New TA

SetObjectPartnership(a,b,"Hello")
SetObjectPartnership(a,c,"Goodbye")

s$=String(GetObjectPartnership(a,b))
Notify s

s$=String(GetObjectPartnership(a,c))
Notify s