/* * MsSubscriberAv_mex.cpp * * Created on: Oct 19, 2012 * Author: jose */ #define MEX #include #include "../../../octave/localization/mex-workspace/mexhandle/ObjectHandle.h" #include #include #define MEX //This should go before the inclusion of sm2oct.h #include void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { char * operation=NULL; int n=0; //The first argument is the handle, the second the operation, the third and so on the required arguments for //the specific operation if (nrhs >= 2 && mxIsChar (prhs[1])) { operation = mxGetChars (prhs[1]); n=mxGetNumberOfElements(prhs[1]); //number of chars in operation } else mexErrMsgTxt("Not enough arguments"); if (nrhs == 4 && n==6 && strncmp(operation,"create",6)==0) { //The third argument is the storage size if (!mxIsClass(prhs[2],"double")) mexErrMsgTxt("Wrong args. Expecting storage size (double)"); double* size=mxGetPr(prhs[2]); //The forth argument is the averaging distance if (!mxIsClass(prhs[3],"double")) mexErrMsgTxt("Wrong args. Expecting averaging distance (double)"); double* avDistance=mxGetPr(prhs[3]); MsSubscriberAv *mSync=NULL; try{ mSync = new MsSubscriberAv(*size,*avDistance); }catch(std::runtime_error& err){ //From MeasurementsSynchronizer mexErrMsgTxt(err.what()); }catch(std::string& err){ //From subscribers mexErrMsgTxt(err.c_str()); }catch(...){ mexErrMsgTxt("Unknnown Exception\n"); } plhs[0]=create_handle(mSync); } else if (nrhs == 3 && n==20 && strncmp(operation,"setAveragingDistance",20)==0) //subscribe method with no arguments { MsSubscriberAv& mSync = get_object(prhs[0]); if (!mxIsClass(prhs[2],"double")) mexErrMsgTxt("Wrong args. Expecting averaging distance (double)"); double* avDist=mxGetPr(prhs[2]); mSync.setAveragingDistance(*avDist); } else if (nrhs == 2 && n==20 && strncmp(operation,"getAveragingDistance",20)==0) //subscribe method with no arguments { MsSubscriberAv& mSync = get_object(prhs[0]); plhs[0]=mxCreateDoubleScalar(mSync.getAveragingDistance()); } else if (nrhs == 2 && n==9 && strncmp(operation,"subscribe",9)==0) //subscribe method with no arguments { MsSubscriberAv& mSync = get_object(prhs[0]); try{ mSync.subscribe(); }catch(std::runtime_error& err){ //From MeasurementsSynchronizer mexErrMsgTxt(err.what()); }catch(std::string& err){ //From subscribers mexErrMsgTxt(err.c_str()); }catch(...){ mexErrMsgTxt("Unknnown Exception\n"); } plhs[0]=mxCreateDoubleScalar((double)mSync.isSubscribed()); } else if (nrhs == 2 && n==11 && strncmp(operation,"unsubscribe",11)==0) //subscribe method with no arguments { MsSubscriberAv& mSync = get_object(prhs[0]); if (mSync.isSubscribed()){ mSync.unsubscribe(); } plhs[0]=mxCreateDoubleScalar((double)mSync.isSubscribed()); } else if (nrhs == 2 && n==12 && strncmp(operation,"isSubscribed",12)==0) //handle,operation { MsSubscriberAv& mSync = get_object(prhs[0]); plhs[0]=mxCreateDoubleScalar((double)mSync.isSubscribed()); } else if ((nrhs == 2 || nrhs == 3) && n==5 && strncmp(operation,"getSm",5)==0) //Get Synchronized Measurement { double blocking=1; MsSubscriberAv& mSync = get_object(prhs[0]); if (nrhs == 3) blocking=*mxGetPr(prhs[2]); try{ const SmPtr sm=mSync.getSynchronizedMeasurement(blocking>=1); if(sm) plhs[0]=Sm2MatStruct(sm); else plhs[0]=mxCreateDoubleScalar(0); }catch(std::runtime_error& err){ //From MeasurementsSynchronizer mexErrMsgTxt(err.what()); }catch(std::string& err){ //From subscribers mexErrMsgTxt(err.c_str()); }catch(...){ mexErrMsgTxt("Unknnown Exception\n"); } } else if (nrhs == 2 && n==6 && strncmp(operation,"delete",6)==0) //handle,operation { MsSubscriberAv& mSync = get_object(prhs[0]); if (mSync.isSubscribed()){ mSync.unsubscribe(); } destroy_object(prhs[0]); } else if (nrhs == 2 && n==5 && strncmp(operation,"flush",5)==0) //handle,operation { MsSubscriberAv& mSync = get_object(prhs[0]); mSync.flush(); } else mexErrMsgTxt("Bad input."); }