Writing S-Functions | ![]() ![]() |
Making C++ Objects Persistent
Your C++ callback methods might need to create persistent C++ objects, that is, objects that continue to exist after the method exits. For example, a callback method might need to access an object created during a previous invocation. Or one callback method might need to access an object created by another callback method. To create persistent C++ objects in your S-function:
static void mdlInitializeSizes(SimStruct *S) { ... ssSetNumPWork(S, 1); // reserve element in the pointers vector // to store a C++ object ... }
static void mdlStart(SimStruct *S) { ssGetPWork(S)[0] = (void *) new counter; // store new C++ object in the } // pointers vector
static void mdlOutputs(SimStruct *S, int_T tid) { counter *c = (counter *) ssGetPWork(S)[0]; // retrieve C++ object from real_T *y = ssGetOutputPortRealSignal(S,0); // the pointers vector and use y[0] = c->output(); // member functions of the } // object
![]() | Source File Format | Building C++ S-Functions | ![]() |