About Monkey 2 › Forums › Monkey 2 Programming Help › Tutorial using the GC with C++ objects
This topic contains 7 replies, has 2 voices, and was last updated by
codifies
2 years, 5 months ago.
-
AuthorPosts
-
November 19, 2016 at 12:03 am #5226
http://bedroomcoders.co.uk/monkey-2-wrapping-c-classes/
in case anyone is wondering I write these as much for myself as anyone else, writing up new things I’ve learnt helps me properly grok what is going on and also adds as my own reference – if it is also useful for someone else occasionally, then that make me happy.
November 19, 2016 at 12:24 am #5227Very nice, however one important issue that I should have picked up on earlier…
MXRectangle should be declared as a class not a struct, eg:
Monkey123456789101112ExternClass MXRectangle 'Note: No 'Extends Void' - this is a REAL mx2 object!Method New( w:Int,h:Int )EndPublicFunction Main()Local r:=New MXRectangle( 10,20 ) 'easy!EndThe problem with using a Struct Ptr is that GC does not track pointers – only class references (which, yes, are really pointers in C++, but you know what I mean…) – so assigning a ptr to a variable will NOT keep an object alive.
This in fact makes things easier though, as there is no need to use a factory method or bbGCNew() on the C++ side (you still can but you need to be a bit careful – more on this later) and there’s no need to have ‘Ptr’ everywhere in the mx2 code since it’s a real mx2 object.
Sorry about not making that clear earlier, I wasn’t look at all the code!
November 19, 2016 at 3:18 am #5228lol just as well i’m having dns issues….
I’ll correct it, thanks
websites off line for a bit – sleeeeeeep (please
)
new changes really made things a lot easier, now a proper 1st class citizen, with c++ methods called from the mx2 class wonderful!
November 19, 2016 at 12:51 pm #5242gah just when I thought I had it all,
if I try to extend MXRectangle (in MX2 code) (so I can add extra code to some of the methods) its asking for the default constructor of MXRectangle ?
I have callbacks (from C++ to MX2) working but then that would mean ctor / del callbacks and a callback for any method I wanted to intercept – doable but cumbersom
for example if I want each instance to keep a list of children to protect them from the GC (while the parent is still alive) ….
JNI (in Java) , Lua etc have a bunch of C routines to create language objects another way round would be if I could create a List<T> in C code without having to cope with all the MX2 name massaging (its not quite as bad as mangling!) but then that would probably be a bunch of work for something maybe not many people would use ??
November 19, 2016 at 7:33 pm #5250if I try to extend MXRectangle (in MX2 code) (so I can add extra code to some of the methods) its asking for the default constructor of MXRectangle ?
This is just normal OO behaviour. You can either added a default ctor to MXRectangle, or use Super.New(…) in dervided class ctors.
November 19, 2016 at 11:32 pm #5263I’m not sure what you mean, as Super.New(.. also doesn’t compile
I can’t see any way to extend an extern class at all
November 19, 2016 at 11:36 pm #5264Monkey1234567891011121314151617Externclass RectMX="RectangleMX"Method area:Float()="Area" ' order unimportantfield height:Float="Height"Method New( w:float,h:float )Field width:Float="Width"EndPublicClass exRectMX Extends RectMXmethod New(w:Float, h:Float)Super.New(w,h)EndEndwon’t compile for example
November 19, 2016 at 11:45 pm #5265Monkey1RectangleMX::RectangleMX():Rectangle(0,0)got it!
but what I don’t quite follow, if this never gets executed (and it doesn’t) why is it needed ?
-
AuthorPosts
You must be logged in to reply to this topic.