Storing Ptr in String

BlitzMax Forums/BlitzMax Programming/Storing Ptr in String

markcw(Posted 2014) [#1]
Ok, so I have a working system that uses TMap to store pointers to Openb3d objects! But I have questions now for the gurus. :)

I am casting to Long and it seems to be ok, but should I use in my BytePtrToString function: Int if Max is 32 bit and Long if 64 bit? Is it bad practise to use Long on 32 bit for this purpose?
' maptest.bmx

Framework brl.retro
Import brl.map

Type TGlobal

	Global ent:TEntity=New TEntity

	Function BytePtrToString:String( instance:Byte Ptr )
	
		Return Long(instance)
		
	End Function

End Type

Type TEntity

	Global entity_map:TMap=New TMap
	
	Field instance:Byte Ptr
	
	Method NewEntity:TEntity( inst:Byte Ptr )

		Local ent:TEntity=New TEntity
		entity_map.Insert( TGlobal.BytePtrToString(inst), ent )
		ent.instance=inst
		Return ent
		
	End Method
	
	Method DeleteEntity( inst:Byte Ptr )
	
		entity_map.Remove( TGlobal.BytePtrToString(inst) )
	
	End Method
	
	Method EntityValue:TEntity( inst:Byte Ptr )
	
		Return TEntity( entity_map.ValueForKey( TGlobal.BytePtrToString(inst) ) )
	
	End Method
	
End Type

Local globals:TGlobal=New TGlobal

Local instance:Byte Ptr=globals ' 1st object
Local ent:TEntity=globals.ent.NewEntity( instance )

Local instance2:Byte Ptr=ent ' 2nd object
Local ent2:TEntity=globals.ent.NewEntity( instance2 )

Local test:TEntity=globals.ent.EntityValue( instance ) ' test map contents
Local test2:TEntity=globals.ent.EntityValue( instance2 )

DebugLog "globals:"+Long Byte Ptr globals
DebugLog "ent:"+Long Byte Ptr ent

DebugLog "test.instance:"+Long test.instance
DebugLog "test2.instance:"+Long test2.instance



markcw(Posted 2014) [#2]
Ho hum.

I tried using String(instance[0]) and it seemed to work in this test but when applied to the wrapper it blew up in my face! :P At least this works. Also I took out the call to BytePtrToString() to optimize it a tiny bit.


Brucey(Posted 2014) [#3]
If you are worried about using Long on a 32-bit build you can do this :
?not x64
		Return Int(instance)
?x64	
		Return Long(instance)
?

which is compatible with the official bcc because "x64" is not defined, and therefore "not x64" will always be true.

I don't think instance[0] is a good idea, unless instance is an Int Ptr or Long Ptr (depending on architecture).


markcw(Posted 2014) [#4]
Thank you Brucey!

I see, I think I was getting ahead of myself with this question.

When I used instance[0] it was from Byte Ptr and when I ran the examples they crashed pretty soon!