Byte Ptr To Object (Part 2)

BlitzMax Forums/BlitzMax Programming/Byte Ptr To Object (Part 2)

beanage(Posted 2010) [#1]
Now, this actually continues this thread, but with some major news (at least to me):

I was just shuffling around a bit in the reflection code, when I encountered this function (heavily cut to prevent/reduce (C) violation);
Function Get:Object( p:Byte Ptr )
	Return bbRefGetObject( p )
End Function

I assume functions like these aint publicly available because of the way you'd be messing up the GC, right?

Good, now, hold breath, the content of bbRefGetObject:
BBObject *bbRefGetObject( BBObject **p ){
	return *p;
}

Isnt this hilarious?! I would kill for a public function doing this conversion, but I'd never have thought of it as being THAT simple!

The only thing I worry about atm is refcount.. do you know of a way to manually deal with it?


Brucey(Posted 2010) [#2]
The only time I want/need to convert a byte ptr to Object is between my C/C++ glue and BlitzMax anyway, so I just extern my glue :
Extern
    Function blah:Object(handle:Byte Ptr)
End Extern


... glue
extern "C" {
   BBObject * blah(SomeCorCPPObject * handle);
}

BBObject * blah(SomeCorCPPObject * handle) {
   return handle->GetMyStoredReferenceFromSomewhere();
}


BBRETAIN(object) and BBRELEASE(object) are useful for doing your own reference counting. Of course, it's up to you to make sure you release as much as you retain.


beanage(Posted 2010) [#3]
Thanks!

You would have to include <blitz.h> into the glue, right?


N(Posted 2010) [#4]
The only thing I worry about atm is refcount.. do you know of a way to manually deal with it?
You should use the BBRETAIN/BBRELEASE macros, although I believe there are also bbObjectRetain and bbObjectRelease functions that use the macro themselves. Calling these would be a better idea, since it should mean (ideally, at least) that your code does not need to be recompiled if BlitzMax makes changes to its garbage collector.

You would have to import <blitz.h>, right?
Yes, you have to include that. More specifically, you have to include brl.mod/blitz.mod/blitz.h.

Edit: Edited for grammar a few times.


Brucey(Posted 2010) [#5]
I usually just Import BRL.Blitz
and include "blitz.h"


beanage(Posted 2010) [#6]
:) Awesome stuff :)