00001 #ifndef _GLOBAL_MEX_DEFS_HH
00002 #define _GLOBAL_MEX_DEFS_HH
00003
00004 #include "matrix"
00005 #include "trace"
00006 #include "mex_convert"
00007 #include "mexpointer"
00008
00009 extern "C" {
00010 #include <mex.h>
00011 }
00012
00013
00014 typedef Matrix MatrixType;
00015
00016
00017 const char ctrace_classname[] = "ctrace";
00018 const char amatrix_classname[] = "amatrix";
00019
00020
00021 template <class T>
00022 struct mexConvert<Trace::CompTrace<T>&> {
00023 Trace::CompTrace<T>& mx2T(const mxArray *m) const;
00024 mxArray* T2mx(const Trace::CompTrace<T>& t) const;
00025 };
00026
00027
00028 template <class T>
00029 struct mexConvert< Trace::OpArg<T> > {
00030 Trace::OpArg<T> mx2T(const mxArray *m) const;
00031 mxArray* T2mx(const Trace::OpArg<T>& a) const;
00032 };
00033
00034
00035 template <class T>
00036 struct mexConvert< Trace::TraceVar<T> > {
00037 Trace::TraceVar<T> mx2T(const mxArray *m) const;
00038 mxArray* T2mx(const Trace::TraceVar<T>& a) const;
00039 };
00040
00041 class InvalidTraceObjectException : public StandardException {
00042 public:
00043 InvalidTraceObjectException() : StandardException() {};
00044 InvalidTraceObjectException(const string& fname, int ln,
00045 const string& msg = string())
00046 : StandardException(fname, ln, head()+msg) {}
00047 const char *head() const {return "Invalid ctrace object";};
00048 };
00049
00050 template <class T>
00051 Trace::CompTrace<T>&
00052 mexConvert<Trace::CompTrace<T>&>::mx2T(const mxArray *m) const {
00053 if (mxIsClass(m,ctrace_classname)) {
00054 mxArray *fieldptr;
00055 mexConvert<Trace::CompTrace<T>*> p_conv;
00056 Trace::CompTrace<T>* traceptr;
00057
00058 fieldptr = mxGetField(m,0,"traceptr");
00059 if (fieldptr == NULL)
00060 handle_exception(InvalidmxArrayException(__FILE__,__LINE__,"Can't convert struct to CompTrace*. No field named traceptr."));
00061
00062 traceptr = p_conv.mx2T(fieldptr);
00063 if (traceptr != NULL)
00064 return *(traceptr);
00065 else
00066 handle_exception(InvalidTraceObjectException(__FILE__,__LINE__));
00067 }
00068 else
00069 handle_exception(InvalidmxArrayException(__FILE__,__LINE__,"Can't convert struct to CompTrace*."));
00070 }
00071
00072 template <class T>
00073 inline mxArray*
00074 mexConvert<Trace::CompTrace<T>&>::T2mx(const Trace::CompTrace<T>& t) const {
00075 mexConvert<Trace::CompTrace<T>*> p_conv;
00076
00077 return p_conv.T2mx(&t);
00078 }
00079
00080 template <class T>
00081 Trace::OpArg<T>
00082 mexConvert< Trace::OpArg<T> >::mx2T(const mxArray *m) const {
00083
00084 if (mxIsClass(m,amatrix_classname)) {
00085 mxArray *fieldptr;
00086 Trace::AbstractElement<T> *el;
00087 int val_index;
00088 mexConvert<Trace::AbstractElement<T>*> el_conv;
00089 mexConvert<int> val_index_conv;
00090
00091 fieldptr = mxGetField(m,0,"idx");
00092 if (fieldptr == NULL)
00093 handle_exception(InvalidmxArrayException(__FILE__,__LINE__,"Can't convert struct to OpArg. No field named idx."));
00094 el = el_conv.mx2T(fieldptr);
00095
00096 fieldptr = mxGetField(m,0,"val");
00097 if (fieldptr == NULL)
00098 handle_exception(InvalidmxArrayException(__FILE__,__LINE__,"Can't convert struct to OpArg. No field named val."));
00099 val_index = val_index_conv.mx2T(fieldptr);
00100
00101 return Trace::OpArg<T>(el,val_index);
00102 }
00103 else
00104 handle_exception(InvalidmxArrayException(__FILE__,__LINE__,"Can't convert struct to OpArg."));
00105 }
00106
00107 template <class T>
00108 mxArray*
00109 mexConvert< Trace::OpArg<T> >::T2mx(const Trace::OpArg<T>& a) const {
00110 mxArray *m;
00111 const char *fieldnames[] = {"idx","val"};
00112 mexConvert<Trace::AbstractElement<T>*> el_conv;
00113 mexConvert<int> val_index_conv;
00114
00115 m = mxCreateStructMatrix(1,1,2,fieldnames);
00116 mxSetField(m,0,"idx",el_conv.T2mx(a.Element()));
00117 mxSetField(m,0,"val",val_index_conv.T2mx(a.Index()));
00118
00119 return m;
00120 }
00121
00122 template <class T>
00123 inline Trace::TraceVar<T>
00124 mexConvert< Trace::TraceVar<T> >::mx2T(const mxArray *m) const {
00125 mexConvert< Trace::OpArg<T> > arg_conv;
00126 return Trace::TraceVar<T>(arg_conv.mx2T(m));
00127 }
00128
00129 template <class T>
00130 mxArray*
00131 mexConvert< Trace::TraceVar<T> >::T2mx(const Trace::TraceVar<T>& tv) const {
00132 mxArray *mx[1], *amatrix_data[1];
00133 mexConvert< Trace::OpArg<T> > arg_conv;
00134
00135 amatrix_data[0] = arg_conv.T2mx(tv.GetArg());
00136 mexCallMATLAB(1, mx, 1, amatrix_data, amatrix_classname);
00137 return mx[0];
00138 }
00139
00140 #endif