Are Double Pointers for Objects possible?

BlitzMax Forums/BlitzMax Programming/Are Double Pointers for Objects possible?

AlexO(Posted 2008) [#1]
Hi,

My main goal is to implement a type of 'interface' class that allows you to monitor/get/set 'primitive' values. I have it working wonderfully with ints, longs, shorts, etc. Even though Blitzmax Strings are objects I would like to be able to monitor string pointers also. Now, I know strings are immutable, and modifying a string is not my goal. I just wish to access it through a pointer for read-only access.

below is some example code implemented with integers.
Local myInt:Int = 12345
Local p:Int Ptr = Varptr(myInt)
Local pp:Int Ptr Ptr = Varptr(p)
Print "double pointer value: " + pp[0][0]

' now if 'p' starts to point to another int my double pointer will see it.
Local mySecondInt:Int = 2
p = Varptr(mySecondInt)
Print "double pointer value: " + pp[0][0]


I want to achieve the same thing with the built-in string class. Any ideas of how to get a 'pointer' to a 'reference' is essentially what I'm trying to do.

..Otherwise I'm going to have to with the String.ToCString() route to be able to access these pointers, which is unfortunate as it's a 'copy' of the string and the user of the class will have to update the string object and the byte-ptr reference (remembering to free memory, etc).


ImaginaryHuman(Posted 2008) [#2]
Look at the module which contains the code for the string object, and you'll find the field name of the pointer to the string's memory.


AlexO(Posted 2008) [#3]
hmm, after some snooping in the source, there seems to be no explicit way to access the buffer in Blitzmax (besides altering the source). only the length field is available.

Anywho, I've decided to opt for another method to 'wrapping' object references. It's not 100% transparent like primitives but it'll alteast be a more 'future proof' than hacking away in memory to gain access to non-public fields.

But for anyone curious about how to gain access to a String's internal character array the code is below:
Local myString:String = "this is a test string"

' hacking the string class.
Local sptr:Byte Ptr = Byte Ptr(Object(myString))
Local spp:Short Ptr = Short Ptr(Varptr(sptr[4]))	' 4th index is the hidden BBChar array after 'length'.

' BBChar is a 2byte unsigned short, hence the cast to a short ptr.
For Local i:Int = 0 To myString.Length - 1
	Print Chr(spp[i])
Next

the above snippet allows you to modify the string in-place. Not that I recommend doing that :)...


tonyg(Posted 2008) [#4]
Would these have helped?
Why is Object Ptr an illigal pointer type?
type pointer workaround?
How to convert from byte ptr to type instance ?