Writing S-Functions | ![]() ![]() |
Making C++ Objects Persistent
Your C++ callback methods may need to create persistent C++ objects, that is, objects that continue to exist after the method exits. For example, a callback method may need to access an object created during a previous invocation. Or one callback method may 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
static void mdlTerminate(SimStruct *S) { counter *c = (counter *) ssGetPWork(S)[0]; // retrieve and destroy C++ delete c; // object in the termination } // function
![]() | Source File Format | Building C++ S-Functions | ![]() |