[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-dev] SC -> c++ interface



   int ComplexStuff_setToArray(struct VMGlobals* g,
int )
  {
    PyrSlot* complexstuff_instance = g->sp - 2;
    PyrSlot* index = g->sp - 1;
    PyrSlot* basicTrumpetinstance = g->sp;


((ComplexStuff*)complexstuff_instance)->setToArray(
 *((int*)(index)) ,
*((BasicTrumpet*)(basicTrumpetinstace)) );
  }

you can't cast an sc object to a C++ object, if you want to store a C++ object in an sc object you have to do it in an sc instance variable (PyrSlot), and you have to make sure to destroy it manually on the sc side before the sc object is gc'd, there is no finalization mechanism. see class SCWindow, it stores a pointer to a C object in its first instance variable (dataptr). you can't just cast PyrSlots to integers or BasicTrumpets either. if you want an int you need to extract it from the slot, have a look in PyrSlot.h where there are plenty of helper macros.

if you have an already written C++ library and you need C++ objects to hang around longer than just on the stack, then you have a headache because there is no way to make deallocation of those objects easy for the user on the sc lang side (no dtors in sc lang), as far as i know they have to do it manually. if you don't already have a lot of code written, then you should implement it as far as possible in sc first and then flesh it out afterwards with primitives, avoiding heap allocated C++ objects altogether if possible.

some tips:

PyrSlot * slot = a slot that you got from somewhere or other

to get an sc object go

PyrObject * object = slot->uo;

to get a pointer to a previously allocated C++ object go:

MyObject * myObject = (MyObject*)slot->uptr;

to get an int you'll probably want to go:

int err, value;

err = slotIntVal( slot, &value );
if( err ) return err; // return from primitve with error code


definePrimitive(base, index++, "_ComplexStuff_setToArray" , ComplexStuff_setToArray , 2, 0);


should be:

definePrimitive(base, index++, "_ComplexStuff_setToArray" , ComplexStuff_setToArray , 3, 0);

two args + the receiver



hope that helps,
_c