Convert from byte ptr to object?
BlitzMax Forums/BlitzMax Programming/Convert from byte ptr to object?
| ||
Isn't an object just a byte ptr? Is there a way to convert a byte ptr back to an object? This is for Newton. |
| ||
This? |
| ||
Can't you use HandleFromObject and HandleToObject instead of byte ptrs? Otherwise this dirty (which may fail in a million different cases) trick can be used: Type TJimmy Global list:TList = New TList Field great:Int = 1234 Method New() list.addlast Self EndMethod Function FromBytePtr:TJimmy(b:Byte Ptr) For Local j:TJimmy = EachIn list If Byte Ptr(Varptr(j))[0] = b[0] Return j EndIf Next EndFunction EndType New TJimmy Local j:TJimmy = New TJimmy j.great = 1456 Local k:Byte Ptr = Varptr(j) Local i:TJimmy = TJimmy.FromBytePtr(k) If i Print "Jimmy.great = "+i.great Else Print "Couldn't find Jimmy!" EndIf |
| ||
As byte pointer are not managed, there is no clean way to get a Object (managed) from them again. If you know that only unmanaged data is stored in thought you can memcopy the byte ptr object into a new class object at position 8+ (byte 0-7 are used bm internally for ref count and class ID. Altought with reflection it potentially might be more than 2 ints, so check before depending on this) |
| ||
HandleToObject should work...myObject = HandleToObject(Int(myBytePtr)) although I've never used it, funnily enough. Usually, if I have a void* that lets me store user data, I define it in the Extern function like this: c code... extern "C" { void setMyData(void * data); void * getMyData(); } max code ... Extern Function setMyData(obj:Object) Function getMyData:Object() End Extern with wxMax though, I've gone one step further, and now interface with BBObject* instead, in my wrappings... extern "C" { void setMyData(BBObject * data); BBObject * getMyData(); } and cast to (void*) or not, where necessary. ...each to their own :-) |
| ||
HandleToObject should work... But it won't HandleToObject only works in conjunction with HandleFromObject, as it does a lookup in a special hashtable to find the object (at least from my understanding). |
| ||
HandleToObject only works in conjunction with HandleFromObject, So it does. Just rummaged through the code. Not sure what use you would have of this really then? What's it for? Looks like I'll stick to what I know works :-) |