Skip to content

Commit

Permalink
Several organizational and logical changes to matlab wrapper code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lior Ramati committed Jun 4, 2018
1 parent 5c29eea commit aed88df
Show file tree
Hide file tree
Showing 7 changed files with 413 additions and 583 deletions.
34 changes: 26 additions & 8 deletions wrappers/matlab/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,35 @@

typedef void mxFunc(int, mxArray*[], int, const mxArray*[]);

struct func_data {
std::function<mxFunc> f;
int out, in_min, in_max;
func_data() : f(), out(0), in_min(0), in_max(0) {};
func_data(std::function<mxFunc> function, int out_args, int in_args) : func_data(function, out_args, in_args, in_args) {}
func_data(std::function<mxFunc> function, int out_args, int in_min_args, int in_max_args) : f(function), out(out_args), in_min(in_min_args), in_max(in_max_args) {}
};

class ClassFactory
{
private:
std::map<std::string, std::function<mxFunc>> funcs;
std::string name;
std::map<std::string, func_data> funcs;
public:
ClassFactory() = default;
void record(std::string name, std::function<mxFunc> func) { funcs.emplace(name, func); }
ClassFactory(std::string n) : name(n), funcs() {}
void record(std::string fname, int out, int in, std::function<mxFunc> func)
{
funcs.emplace(fname, func_data(func, out, in));
}
void record(std::string fname, int out, int in_min, int in_max, std::function<mxFunc> func)
{
funcs.emplace(fname, func_data(func, out, in_min, in_max));
}

std::string get_name() { return name; }

std::function<mxFunc> get(std::string f){
func_data get(std::string f){
auto func = funcs.find(f);
if (func == funcs.end()) return std::function<mxFunc>();
if (func == funcs.end()) return func_data();
return func->second;
}
};
Expand All @@ -29,11 +47,11 @@ class Factory
std::map<std::string, ClassFactory> classes;
public:
Factory() = default;
void record(std::string name, ClassFactory cls) { classes.emplace(name, cls); }
void record(ClassFactory cls) { classes.emplace(cls.get_name(), cls); }

std::function<mxFunc> get(std::string c, std::string f){
func_data get(std::string c, std::string f){
auto cls = classes.find(c);
if (cls == classes.end()) return std::function<mxFunc>();
if (cls == classes.end()) return func_data();
return cls->second.get(f);
}
};
Expand Down
3 changes: 2 additions & 1 deletion wrappers/matlab/MatlabParamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class MatlabParamParser
MatlabParamParser() {};
~MatlabParamParser() {};

// TODO: try/catch->err msg?
template <typename T> static T parse(const mxArray* cell) { return mx_wrapper_fns<T>::parse(cell); }
template <typename T> static mxArray* wrap(T&& var) { return mx_wrapper_fns<T>::wrap(std::move(var)); };
template <typename T> static void destroy(const mxArray* cell) { return mx_wrapper_fns<T>::destroy(cell); }
Expand Down Expand Up @@ -159,7 +160,7 @@ template <typename T> static mxArray* MatlabParamParser::wrap_array(const T* var
static_assert(!std::is_same<ty<T>, std::false_type>::value, "Not a supported array type");
auto cells = mxCreateNumericArray(/*ndims*/1, dims, ty<T>::value, mxREAL);
auto ptr = static_cast<T*>(mxGetData(cells));
// todo: generalize to more dimensions. maybe nested helper functions?
// TODO: generalize to more dimensions. maybe nested helper functions?
for (int x = 0; x < dims[0]; ++x)
// or something...
ptr[x] = (contiguous) ? var[x/*+y*dims[0]*/] : var[x]/*[y]*/;
Expand Down
4 changes: 2 additions & 2 deletions wrappers/matlab/device.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ function do_init(this)
function delete(this)
if (this.objectHandle ~= 0)
if (this.id < 0) % still device list
realsense.librealsense_mex('rs2::device', 'delete_uninit', this.objectHandle);
realsense.librealsense_mex('rs2::device', 'delete#uninit', this.objectHandle);
else
realsense.librealsense_mex('rs2::device', 'delete_init', this.objectHandle);
realsense.librealsense_mex('rs2::device', 'delete#init', this.objectHandle);
end
end
end
Expand Down
Loading

0 comments on commit aed88df

Please sign in to comment.