Tutorial using the GC with C++ objects

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.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #5226

    codifies
    Participant

    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.

    #5227

    Mark Sibly
    Keymaster

    Very nice, however one important issue that I should have picked up on earlier…

    MXRectangle should be declared as a class not a struct, eg:

    The 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!

    #5228

    codifies
    Participant

    lol 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!

    #5242

    codifies
    Participant

    gah 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 ??

    #5250

    Mark Sibly
    Keymaster

    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 ?

    This is just normal OO behaviour. You can either added a default ctor to MXRectangle, or use Super.New(…) in dervided class ctors.

    #5263

    codifies
    Participant

    I’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

    #5264

    codifies
    Participant

    won’t compile for example

    #5265

    codifies
    Participant

    got it!

    but what I don’t quite follow, if this never gets executed (and it doesn’t) why is it needed ?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.