Forcing an object into a handle and back

BlitzMax Forums/BlitzMax Programming/Forcing an object into a handle and back

teamonkey(Posted 2005) [#1]
Just wondering if there was a way to do this apart from when calling New
Local handle:Int = New MyType


For example,
Local o:MyType = New MyType
Local h:Int = HandleFromObject(o)

...

Local p:MyType = HandleToObject(h)


mod/brl.mod/blitz.mod/blitz_handle.h has these functions in, but they don't seem to be available for use within Max.


FlameDuck(Posted 2005) [#2]
Just wondering if there was a way to do this apart from when calling New
I was under the impression that you could simply explicitly cast them. Why you would ever want to however is beyond me.


teamonkey(Posted 2005) [#3]
If you leave off the cast it seems that you can turn an object into an int. Can't work out how to do the reverse though.

As for why: my post at the end of http://blitzbasic.com/Community/posts.php?topic=41621

I'm having no luck with pushing pointers around and thought that it would be easier than creating my own handle system. I might be wrong.


dynaman(Posted 2005) [#4]
> If you leave off the cast it seems that you can turn an object into an int.

Most likely it is returning the memory address of the item in that case - just guessing, I think Mark mentioned somewhere that the default item behavior for checking equality was to retu


Muttley(Posted 2005) [#5]
have you tried:
Local o:MyType = New MyType
Local h:MyType = o:MyType

I've passed object handles to functions by:
For Local object:TObject=EachIn objects
   Local test = testSomething(object)
...
...
Function testSomething (p_objectToTest:TObject)


Muttley


skidracer(Posted 2005) [#6]
I think you need to use a function to convert between object and handle:

Strict 

Function ObjectHandle:Object(t:Object)
	Return t
End Function

Type mytype
	Field	f=20
End Type

Local a:mytype=New mytype
Local b:Int=ObjectHandle(a)
Print b
Local c:mytype=mytype(ObjectHandle(b))
Print c.f



teamonkey(Posted 2005) [#7]
Thanks for the suggestions.

In the end I just stopped being lazy and implemented my own index handle. :)

Type GameObject
	Field idx:Int
	
	Global objects:TList
	Global current_idx:Int

	Method New()
		If(Not objects) Then objects = CreateList()

		idx = nextIndex()

		ListAddLast objects,Self
	End Method
	
	Function nextIndex:Int()
		Local idx:Int = current_idx
		current_idx :+ 1
		
		Return idx
	End Function

	Function findFromIndex:GameObject(idx:Int)
		For Local o:GameObject = EachIn objects
			If(o.idx=idx) Then Return o
		Next
		
		Return Null
	End Function
	
	Method getIndex:Int()
		Return idx
	End Method

...


Could probably be optimised a bit (a lot) but it'll do the job for now.


Storm8191(Posted 2006) [#8]
Does anyone have a working example of using HandleToObject? I'm working on a project where I will be storing a stack of objects, each of a different type. I can manage this as a bank, but I don't think Blitz would let me do this with a list object.


tonyg(Posted 2006) [#9]
Strict 
Type mytype
	Field	f:string="Hello World!"
End Type

Local a:mytype=New mytype
Local b:Int=HandleFromObject(a)
Print b
Local c:mytype=mytype(HandleToObject(b))
Print c.f

<edit> took out some extra stuff not needed.


ImaginaryHuman(Posted 2006) [#10]
*Way* too complicated, just do:

Local a:mytype=New mytype
Local b:Int=Int(a) 'convert to int handle

'Do whatever you want with the handle here

local a=VarPtr(b)[0] 'convert to object


`a` is probably really just an Int Ptr, so might as well pass it the address of the contents of the Integer handle.


tonyg(Posted 2006) [#11]
Does that work for you (and they're actually the same number of lines of code)?
Do you not get 'Unable to convert from mytype to int'?


gman(Posted 2006) [#12]
also, anytime you do HandleFromObject() it counts as another pointer to your object. you must Release() the integer handle when you are done with it in order for the garbage collector to do its work. if you dont, you will end up with a memory leak.