Improved tempalte formatting

This commit is contained in:
Hilton Bristow 2013-03-18 14:48:10 +10:00 committed by hbristow
parent 66c40bee6f
commit eda32520e2
7 changed files with 59 additions and 56 deletions

View File

@ -14,25 +14,13 @@ ocv_add_module(matlab BINDINGS opencv_core opencv_imgproc
opencv_nonfree opencv_calib)
# Add all of the headers we wish to parse
set(opencv_hdrs
"${OPENCV_MODULE_opencv_core_LOCATION}/include/opencv2/core/core.hpp"
"${OPENCV_MODULE_opencv_flann_LOCATION}/include/opencv2/flann/miniflann.hpp"
"${OPENCV_MODULE_opencv_imgproc_LOCATION}/include/opencv2/imgproc/imgproc.hpp"
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/background_segm.hpp"
"${OPENCV_MODULE_opencv_video_LOCATION}/include/opencv2/video/tracking.hpp"
"${OPENCV_MODULE_opencv_photo_LOCATION}/include/opencv2/photo/photo.hpp"
"${OPENCV_MODULE_opencv_highgui_LOCATION}/include/opencv2/highgui/highgui.hpp"
"${OPENCV_MODULE_opencv_ml_LOCATION}/include/opencv2/ml/ml.hpp"
"${OPENCV_MODULE_opencv_features2d_LOCATION}/include/opencv2/features2d/features2d.hpp"
"${OPENCV_MODULE_opencv_calib3d_LOCATION}/include/opencv2/calib3d/calib3d.hpp"
"${OPENCV_MODULE_opencv_objdetect_LOCATION}/include/opencv2/objdetect/objdetect.hpp"
"${OPENCV_MODULE_opencv_softcascade_LOCATION}/include/opencv2/softcascade/softcascade.hpp"
"${OPENCV_MODULE_opencv_contrib_LOCATION}/include/opencv2/contrib/contrib.hpp")
if(HAVE_opencv_nonfree)
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/features2d.hpp"
"${OPENCV_MODULE_opencv_nonfree_LOCATION}/include/opencv2/nonfree/nonfree.hpp")
string(REPLACE "opencv_" "" OPENCV_MATLAB_MODULES "${OPENCV_MODULE_${the_module}_REQ_DEPS};
${OPENCV_MODULE_${the_module}_OPT_DEPS}")
foreach(module ${OPENCV_MATLAB_MODULES})
if (HAVE_opencv_${module})
list(APPEND opencv_hdrs "${OPENCV_MODULE_opencv_${module}_LOCATION}/include/opencv2/${module}/${module}.hpp")
endif()
endforeach()
# add the python generator to the python path
set(PYPATH_CACHE $ENV{PYTHONPATH})
@ -41,7 +29,7 @@ set(ENV{PYTHONPATH} ${OPENCV_MODULE_opencv_python_LOCATION}/src2:$ENV{PYTHONPATH
# synthesise the matlab sources
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generator/gen_matlab_caller.py
${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR}/src)
${opencv_hdrs} ${CMAKE_CURRENT_BINARY_DIR})
# compile the matlab sources
file(GLOB SOURCE_FILES ${CMAKE_CURRENT_BINARY_DIR}/src)
@ -49,7 +37,5 @@ foreach(SOURCE_FILE ${SOURCE_FILES})
# compile the source file using mex
endforeach()
# restore the pythonpath
set(ENV{PYTHONPATH} ${PYPATH_CACHE})

View File

@ -39,17 +39,34 @@ class MatlabWrapperGenerator(object):
tdoc = jtemplate.get_template('template_doc_base.m')
# create the build directory
if not os.path.isdir(output_dir):
os.mkdir(output_dir)
output_source_dir = output_dir+'/src'
output_private_dir = output_source_dir+'/private'
output_class_dir = output_dir+'/+cv'
if not os.path.isdir(output_source_dir):
os.mkdir(output_source_dir)
if not os.path.isdir(output_private_dir):
os.mkdir(output_private_dir)
if not os.path.isdir(output_class_dir):
os.mkdir(output_class_dir)
# populate!
function = parse_tree.namespaces[0].functions[0]
print function
populated = tfunction.render(fun=function, time=time)
with open(output_dir+'/'+function.name+'.cpp', 'wb') as f:
f.write(populated)
#for name, namespace in ns:
# for function in namespace.functions:
# print 'populating function tempaltes from '+name
# populated = tfunction.render(function)
# populate templates
for namespace in parse_tree.namespaces:
print 'populating function templates from '+namespace.name
# functions
for function in namespace.functions:
populated = tfunction.render(fun=function, time=time)
with open(output_source_dir+'/'+function.name+'.cpp', 'wb') as f:
f.write(populated)
# classes
for clss in namespace.classes:
# cpp converter
if len(clss.functions) > 2:
print clss.functions[1].__str__()
populated = tclassc.render(clss=clss, time=time)
with open(output_private_dir+'/'+clss.name+'Bridge.cpp', 'wb') as f:
f.write(populated)
# matlab classdef
populated = tclassm.render(clss=clss, time=time)
with open(output_class_dir+'/'+clss.name+'.m', 'wb') as f:
f.write(populated)

View File

@ -26,7 +26,6 @@ NEWLINE_SEQUENCE = '\n'
# default filters, tests and namespace
from jinja2.filters import FILTERS as DEFAULT_FILTERS
from jinja2.tests import TESTS as DEFAULT_TESTS
DEFAULT_NAMESPACE = {
'range': xrange,
'dict': lambda **kw: kw,

View File

@ -265,7 +265,6 @@ class Environment(object):
# defaults
self.filters = DEFAULT_FILTERS.copy()
self.tests = DEFAULT_TESTS.copy()
self.globals = DEFAULT_NAMESPACE.copy()
# set the loader provided

View File

@ -133,9 +133,9 @@ class Function(object):
self.opt = opt if opt else []
def __str__(self):
return fill((self.rtp+' ' if self.rtp else '')+self.name+'('+\
return (self.rtp+' ' if self.rtp else '')+self.name+'('+\
join((arg.__str__() for arg in self.req+self.opt), ', ')+\
')'+(' const' if self.const else '')+';', 80, subsequent_indent=('\t\t' if self.clss else '\t'))
')'+(' const' if self.const else '')+';'
class Argument(object):
def __init__(self, name='', tp='', const=False, ref='', default=''):

View File

@ -1,5 +1,5 @@
/*
* file: {{class.name}}Bridge.cpp
* file: {{clss.name}}Bridge.cpp
* author: A trusty code generator
* date: {{time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())}}
*
@ -18,34 +18,35 @@
namespace {
typedef std::unordered_map Map;
typedef std::vector<Bridge> (*)({{class.name}}&, const std::vector<Bridge>&) MethodSignature;
typedef std::vector<Bridge> (*)({{clss.name}}&, const std::vector<Bridge>&) MethodSignature;
{% for function in class.functions %}
{% for function in clss.functions %}
// wrapper for {{function.name}}() method
std::vector<Bridge> {{function.name}}({{class.name}}& inst, const std::vector<Bridge>& args) {
std::vector<Bridge> {{function.name}}({{clss.name}}& inst, const std::vector<Bridge>& args) {
// setup
// invoke
// setdown
}
{% endfor %}
map<std::string, MethodSignature> createMethodMap() {
Map<std::string, MethodSignature> m;
{% for function in class.functions %}
{% for function in clss.functions -%}
m["{{function.name}}"] = &{{function.name}};
{% endfor %}
return m;
}
static const Map<std::string, MethodSignature> methods = createMethodMap();
// map of created {{class.name}} instances. Don't trust the user to keep them safe...
static Map<void *, {{class.name}}> instances;
// map of created {{clss.name}} instances. Don't trust the user to keep them safe...
static Map<void *, {{clss.name}}> instances;
/*
* {{ class.name }}
* {{ clss.name }}
* Gateway routine
* nlhs - number of return arguments
* plhs - pointers to return arguments
@ -63,7 +64,7 @@ void mexFunction(int nlhs, mxArray* plhs[],
// retrieve the instance of interest
try {
{{class.name}}& inst = instances.at(handle.address());
{{clss.name}}& inst = instances.at(handle.address());
} catch (const std::out_of_range& e) {
mexErrMsgTxt("Invalid object instance provided");
}
@ -80,6 +81,7 @@ void mexFunction(int nlhs, mxArray* plhs[],
{% block cleanup %}
{% endblock %}
}
}; // end namespace

View File

@ -1,29 +1,29 @@
% {{class.name | upper}}
% Matlab handle class for OpenCV object classes
% {{clss.name | upper}}
% Matlab handle clss for OpenCV object clsses
%
% This file was autogenerated, do not modify.
% See LICENCE for full modification and redistribution details.
% Copyright {{time.strftime("%Y", time.localtime())}} The OpenCV Foundation
classdef {{class.name}} < handle
classdef {{clss.name}} < handle
properties (SetAccess = private, Hidden = true)
ptr_ = 0; % handle to the underlying c++ class instance
ptr_ = 0; % handle to the underlying c++ clss instance
end
methods
% constructor
function this = {{class.name}}(varargin)
this.ptr_ = {{class.name}}Bridge('new', varargin{:});
function this = {{clss.name}}(varargin)
this.ptr_ = {{clss.name}}Bridge('new', varargin{:});
end
% destructor
function delete(this)
{{className}}Bridge(this.ptr_, 'delete');
{{clss.name}}Bridge(this.ptr_, 'delete');
end
{% for function in class.functions %}
{% for function in clss.functions -%}
% {{function.__str__()}}
function varargout = {{function.name}}(this, varargin)
[varargout{1:nargout}] = {{class.name}}Bridge('{{function.name}}', this.ptr_, varargin{:});
[varargout{1:nargout}] = {{clss.name}}Bridge('{{function.name}}', this.ptr_, varargin{:});
end
{% endfor %}