Created
July 21, 2018 02:06
-
-
Save linkviii/d7a83361dfe14f1b4779f089f308cb05 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mex.h" | |
/* | |
* Doing this works and is easy enough to maintain but it is kind of annoying. | |
* There are code generators and argument parsers out there, | |
* but they can complicate the code even more. | |
* Named optional parameters and other complexities should just be handled by a matlab wrapper. | |
*/ | |
// I meant this string was useless but it also describes this file | |
#define MEX_ERR_ID "pointless:boilerplate" | |
bool assert_type(bool correct_type, int index, char *name, char *should_be){ | |
if (!correct_type){ | |
// index + 1 because matlab starts at 1 | |
mexErrMsgIdAndTxt(MEX_ERR_ID, "Argument %d (%s) should be %s.", index + 1, name, should_be); | |
} | |
} | |
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ | |
int arg_count = -1; | |
const int foo_index = ++arg_count; | |
const int bar_index = ++arg_count; | |
const int optional_index = ++arg_count; | |
int required_arguments = bar_index + 1; | |
if (nrhs < required_arguments || nrhs > arg_count){ | |
usage_error(); | |
} | |
const mxArray *foo_arg = prhs[foo_index]; | |
const mxArray *bar_arg = prhs[bar_index]; | |
const mxArray *optional_arg = optional_index < nrhs ? prhs[optional_index] : NULL; | |
assert_type(mxIsDouble(foo_arg), foo_index, "foo", "a double array"); | |
assert_type(is_scalar_double(bar_arg), bar_index, "bar", "a number"); | |
if (optional_arg) assert_type(mxIsDouble(optional_arg), optional_index, "optional", "a double array"); | |
const double *foo = mxGetPr(foo_arg); | |
int bar = (int) mxGetScalar(bar) | |
... optional ... | |
plhs ... out | |
do_thing_better_than_matlab(foo, bar, optional, out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment