From 09e294711d79be00f902034beaddea30c3170909 Mon Sep 17 00:00:00 2001 From: Hilton Bristow Date: Mon, 18 Mar 2013 10:35:43 +1000 Subject: [PATCH] Added class templates --- modules/matlab/generator/filters.py | 11 +++ .../templates/template_class_base.cpp | 85 +++++++++++++++++++ .../generator/templates/template_class_base.m | 27 ++++++ .../templates/template_function_base.cpp | 2 +- 4 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 modules/matlab/generator/templates/template_class_base.cpp create mode 100644 modules/matlab/generator/templates/template_class_base.m diff --git a/modules/matlab/generator/filters.py b/modules/matlab/generator/filters.py index 60eb3a937b..b69ef72d9c 100644 --- a/modules/matlab/generator/filters.py +++ b/modules/matlab/generator/filters.py @@ -1,6 +1,17 @@ from textwrap import TextWrapper from string import split, join +import re +def toUpperCamelCase(text): + return text[0].upper() + text[1:] + +def toLowerCamelCase(text): + return text[0].lower() + text[1:] + +def toUnderCase(text): + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', text) + return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower() + def comment(text, wrap=80, escape='% ', escape_first='', escape_last=''): '''comment filter Takes a string in text, and wraps it to wrap characters in length with diff --git a/modules/matlab/generator/templates/template_class_base.cpp b/modules/matlab/generator/templates/template_class_base.cpp new file mode 100644 index 0000000000..93b656083d --- /dev/null +++ b/modules/matlab/generator/templates/template_class_base.cpp @@ -0,0 +1,85 @@ +/* + * file: {{class.name}}Bridge.cpp + * author: A trusty code generator + * date: {{time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())}} + * + * This file was autogenerated, do not modify. + * See LICENCE for full modification and redistribution details. + * Copyright {{time.strftime("%Y", time.gmtime())}} The OpenCV Foundation + */ +#include "mex.h" +#include "bridge.hpp" +#include +#include +#include +{% block includes %} +{% endblock %} + +namespace { + +typedef std::unordered_map Map; +typedef std::vector (*)({{class.name}}&, const std::vector&) MethodSignature; + +{% for function in class.functions %} +// wrapper for {{function.name}}() method +std::vector {{function.name}}({{class.name}}& inst, const std::vector& args) { + // setup + + // invoke + + + // setdown +} +{% endfor %} + +map createMethodMap() { + Map m; + {% for function in class.functions %} + m["{{function.name}}"] = &{{function.name}}; + {% endfor %} + return m; +} +static const Map methods = createMethodMap(); + +// map of created {{class.name}} instances. Don't trust the user to keep them safe... +static Map instances; + +/* + * {{ class.name }} + * Gateway routine + * nlhs - number of return arguments + * plhs - pointers to return arguments + * nrhs - number of input arguments + * prhs - pointers to input arguments + */ +void mexFunction(int nlhs, mxArray* plhs[], + int nrhs, const mxArray* prhs[]) { + + // parse the inputs + Bridge method_name(prhs[0]); + + Bridge handle(prhs[1]); + std::vector brhs(prhs+2, prhs+nrhs); + + // retrieve the instance of interest + try { + {{class.name}}& inst = instances.at(handle.address()); + } catch (const std::out_of_range& e) { + mexErrMsgTxt("Invalid object instance provided"); + } + + // invoke the correct method on the data + try { + std::vector blhs = (*methods.at(method_name))(inst, brhs); + } catch (const std::out_of_range& e) { + mexErrMsgTxt("Unknown method specified"); + } + + {% block postfun %} + {% endblock %} + + {% block cleanup %} + {% endblock %} +} + +}; // end namespace diff --git a/modules/matlab/generator/templates/template_class_base.m b/modules/matlab/generator/templates/template_class_base.m new file mode 100644 index 0000000000..0347120e3d --- /dev/null +++ b/modules/matlab/generator/templates/template_class_base.m @@ -0,0 +1,27 @@ +% {{class.name | upper}} +% Matlab handle class for OpenCV object classes +classdef {{class.name}} < handle + properties (SetAccess = private, Hidden = true) + ptr_ = 0; % handle to the underlying c++ class instance + end + + methods + % constructor + function this = {{class.name}}(varargin) + this.ptr_ = {{class.name}}Bridge('new', varargin{:}); + end + + % destructor + function delete(this) + {{className}}Bridge(this.ptr_, 'delete'); + end + + {% for function in class.functions %} + % {{function.__str__()}} + function varargout = {{function.name}}(this, varargin) + [varargout{1:nargout}] = {{class.name}}Bridge('{{function.name}}', this.ptr_, varargin{:}); + end + + {% endfor %} + end +end diff --git a/modules/matlab/generator/templates/template_function_base.cpp b/modules/matlab/generator/templates/template_function_base.cpp index 8ab16a843f..7f34273210 100644 --- a/modules/matlab/generator/templates/template_function_base.cpp +++ b/modules/matlab/generator/templates/template_function_base.cpp @@ -15,7 +15,7 @@ /* * {{ fun.name }} - * Main mex entry point + * Gateway routine * nlhs - number of return arguments * plhs - pointers to return arguments * nrhs - number of input arguments