From 06dbc2b650b851a224a330f49d4cd26f06811e32 Mon Sep 17 00:00:00 2001 From: Alex Cunningham Date: Fri, 9 Dec 2011 20:29:47 +0000 Subject: [PATCH] Changed namespace mechanism in wrap to "using namespace gtsam;" inside gtsam.h --- gtsam.h | 3 ++ wrap/Class.cpp | 12 ++++---- wrap/Class.h | 6 ++-- wrap/Constructor.cpp | 14 +++------ wrap/Constructor.h | 3 +- wrap/Makefile.am | 3 +- wrap/Method.cpp | 12 ++------ wrap/Method.h | 3 +- wrap/Module.cpp | 12 ++++---- wrap/Module.h | 2 +- wrap/StaticMethod.cpp | 13 +++------ wrap/StaticMethod.h | 3 +- wrap/tests/expected/@Point2/dim.cpp | 3 +- .../expected/@Point2/vectorConfusion.cpp | 11 +++++++ wrap/tests/expected/@Point2/vectorConfusion.m | 4 +++ wrap/tests/expected/@Point2/x.cpp | 3 +- wrap/tests/expected/@Point2/y.cpp | 3 +- wrap/tests/expected/@Point3/norm.cpp | 3 +- .../expected/@Test/arg_EigenConstRef.cpp | 3 +- wrap/tests/expected/@Test/arg_EigenConstRef.m | 4 +++ .../tests/expected/@Test/create_MixedPtrs.cpp | 3 +- wrap/tests/expected/@Test/create_ptrs.cpp | 3 +- wrap/tests/expected/@Test/print.cpp | 3 +- .../tests/expected/@Test/return_Point2Ptr.cpp | 3 +- wrap/tests/expected/@Test/return_Test.cpp | 3 +- wrap/tests/expected/@Test/return_TestPtr.cpp | 3 +- wrap/tests/expected/@Test/return_bool.cpp | 3 +- wrap/tests/expected/@Test/return_double.cpp | 3 +- wrap/tests/expected/@Test/return_field.cpp | 3 +- wrap/tests/expected/@Test/return_int.cpp | 3 +- wrap/tests/expected/@Test/return_matrix1.cpp | 3 +- wrap/tests/expected/@Test/return_matrix2.cpp | 3 +- wrap/tests/expected/@Test/return_pair.cpp | 3 +- wrap/tests/expected/@Test/return_ptrs.cpp | 3 +- wrap/tests/expected/@Test/return_size_t.cpp | 3 +- wrap/tests/expected/@Test/return_string.cpp | 3 +- wrap/tests/expected/@Test/return_vector1.cpp | 3 +- wrap/tests/expected/@Test/return_vector2.cpp | 3 +- .../expected/Point3_StaticFunctionRet.cpp | 8 +++-- wrap/tests/expected/Point3_staticFunction.cpp | 3 +- wrap/tests/expected/new_Point2_.cpp | 3 +- wrap/tests/expected/new_Point2_dd.cpp | 3 +- wrap/tests/expected/new_Point3_ddd.cpp | 3 +- wrap/tests/expected/new_Test_.cpp | 3 +- wrap/tests/expected/new_Test_dM.cpp | 3 +- wrap/tests/geometry.h | 4 +++ wrap/tests/testWrap.cpp | 10 +++++-- wrap/utilities.cpp | 29 +++++++++++++++---- wrap/utilities.h | 13 +++++++++ wrap/wrap.cpp | 6 ++-- 50 files changed, 170 insertions(+), 92 deletions(-) create mode 100644 wrap/tests/expected/@Point2/vectorConfusion.cpp create mode 100644 wrap/tests/expected/@Point2/vectorConfusion.m create mode 100644 wrap/tests/expected/@Test/arg_EigenConstRef.m diff --git a/gtsam.h b/gtsam.h index 819d56c5b..071ababc8 100644 --- a/gtsam.h +++ b/gtsam.h @@ -47,6 +47,9 @@ * - TODO: Handle Rot3M conversions to quaternions */ +// Everything is in the gtsam namespace, so we avoid copying everything in +using namespace gtsam; + class Point2 { Point2(); Point2(double x, double y); diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 76554e608..82245bd26 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -57,28 +57,28 @@ void Class::matlab_proxy(const string& classFile) { } /* ************************************************************************* */ -void Class::matlab_constructors(const string& toolboxPath,const string& nameSpace) { +void Class::matlab_constructors(const string& toolboxPath, const vector& using_namespaces) { BOOST_FOREACH(Constructor c, constructors) { c.matlab_mfile (toolboxPath, qualifiedName()); - c.matlab_wrapper(toolboxPath, qualifiedName("::"), qualifiedName(), nameSpace, includes); + c.matlab_wrapper(toolboxPath, qualifiedName("::"), qualifiedName(), using_namespaces, includes); } } /* ************************************************************************* */ -void Class::matlab_methods(const string& classPath, const string& nameSpace) { +void Class::matlab_methods(const string& classPath, const vector& using_namespaces) { string matlabName = qualifiedName(), cppName = qualifiedName("::"); BOOST_FOREACH(Method m, methods) { m.matlab_mfile (classPath); - m.matlab_wrapper(classPath, name, cppName, matlabName, nameSpace, includes); + m.matlab_wrapper(classPath, name, cppName, matlabName, using_namespaces, includes); } } /* ************************************************************************* */ -void Class::matlab_static_methods(const string& toolboxPath, const string& nameSpace) { +void Class::matlab_static_methods(const string& toolboxPath, const vector& using_namespaces) { string matlabName = qualifiedName(), cppName = qualifiedName("::"); BOOST_FOREACH(StaticMethod& m, static_methods) { m.matlab_mfile (toolboxPath, qualifiedName()); - m.matlab_wrapper(toolboxPath, name, matlabName, cppName, nameSpace, includes); + m.matlab_wrapper(toolboxPath, name, matlabName, cppName, using_namespaces, includes); } } diff --git a/wrap/Class.h b/wrap/Class.h index db9c14c8c..ac23102b9 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -42,11 +42,11 @@ struct Class { // And finally MATLAB code is emitted, methods below called by Module::matlab_code void matlab_proxy(const std::string& classFile); ///< emit proxy class void matlab_constructors(const std::string& toolboxPath, - const std::string& nameSpace); ///< emit constructor wrappers + const std::vector& using_namespaces); ///< emit constructor wrappers void matlab_methods(const std::string& classPath, - const std::string& nameSpace); ///< emit method wrappers + const std::vector& using_namespaces); ///< emit method wrappers void matlab_static_methods(const std::string& classPath, - const std::string& nameSpace); ///< emit static method wrappers + const std::vector& using_namespaces); ///< emit static method wrappers void matlab_make_fragment(std::ofstream& ofs, const std::string& toolboxPath, const std::string& mexFlags); ///< emit make fragment for global make script diff --git a/wrap/Constructor.cpp b/wrap/Constructor.cpp index 4bc233ca2..04a873547 100644 --- a/wrap/Constructor.cpp +++ b/wrap/Constructor.cpp @@ -71,9 +71,8 @@ void Constructor::matlab_mfile(const string& toolboxPath, const string& qualifie void Constructor::matlab_wrapper(const string& toolboxPath, const string& cppClassName, const string& matlabClassName, - const string& nameSpace, const vector& includes) + const vector& using_namespaces, const vector& includes) { - string matlabName = matlab_wrapper_name(matlabClassName); // open destination wrapperFile @@ -84,14 +83,9 @@ void Constructor::matlab_wrapper(const string& toolboxPath, // generate code emit_header_comment(ofs, "//"); - ofs << "#include " << endl; - if (includes.empty()) // add a default include - ofs << "#include <" << name << ".h>" << endl; - else { - BOOST_FOREACH(const string& s, includes) - ofs << "#include <" << s << ">" << endl; - } - if (!nameSpace.empty()) ofs << "using namespace " << nameSpace << ";" << endl; + generateIncludes(ofs, name, includes); + generateUsingNamespace(ofs, using_namespaces); + ofs << "void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])" << endl; ofs << "{" << endl; ofs << " checkArguments(\"" << matlabName << "\",nargout,nargin," << args.size() << ");" << endl; diff --git a/wrap/Constructor.h b/wrap/Constructor.h index 08906dbbd..5409ed1f6 100644 --- a/wrap/Constructor.h +++ b/wrap/Constructor.h @@ -55,7 +55,8 @@ struct Constructor { void matlab_wrapper(const std::string& toolboxPath, const std::string& cppClassName, const std::string& matlabClassName, - const std::string& nameSpace, const std::vector& includes); + const std::vector& using_namespaces, + const std::vector& includes); }; } // \namespace wrap diff --git a/wrap/Makefile.am b/wrap/Makefile.am index 3dd54942a..8b3e4f493 100644 --- a/wrap/Makefile.am +++ b/wrap/Makefile.am @@ -57,7 +57,6 @@ LDADD = libwrap.la ../CppUnitLite/libCppUnitLite.a interfacePath = $(top_srcdir) moduleName = gtsam toolboxpath = ../toolbox -nameSpace = "gtsam" # Set flags to pass to mex mexFlags = @@ -87,7 +86,7 @@ endif # Linux all: generate_toolbox generate_toolbox: $(top_srcdir)/gtsam.h - ./wrap ${mexextension} ${interfacePath} ${moduleName} ${toolboxpath} ${nameSpace} ${mexFlags} + ./wrap ${mexextension} ${interfacePath} ${moduleName} ${toolboxpath} ${mexFlags} source_mode = -m 644 diff --git a/wrap/Method.cpp b/wrap/Method.cpp index c5f01abcf..b9f10a9d4 100644 --- a/wrap/Method.cpp +++ b/wrap/Method.cpp @@ -52,7 +52,7 @@ void Method::matlab_wrapper(const string& classPath, const string& className, const string& cppClassName, const string& matlabClassName, - const string& nameSpace, const std::vector& includes) + const vector& using_namespaces, const std::vector& includes) { // open destination wrapperFile string wrapperFile = classPath + "/" + name + ".cpp"; @@ -64,14 +64,8 @@ void Method::matlab_wrapper(const string& classPath, // header wrap::emit_header_comment(ofs, "//"); - ofs << "#include \n"; - if (includes.empty()) // add a default include - ofs << "#include <" << className << ".h>" << endl; - else { - BOOST_FOREACH(const string& s, includes) - ofs << "#include <" << s << ">" << endl; - } - if (!nameSpace.empty()) ofs << "using namespace " << nameSpace << ";" << endl; + generateIncludes(ofs, className, includes); + generateUsingNamespace(ofs, using_namespaces); // call ofs << "void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; diff --git a/wrap/Method.h b/wrap/Method.h index 95dc83924..57bd1c043 100644 --- a/wrap/Method.h +++ b/wrap/Method.h @@ -46,7 +46,8 @@ struct Method { void matlab_wrapper(const std::string& classPath, const std::string& className, const std::string& cppClassName, - const std::string& matlabClassname,const std::string& nameSpace, + const std::string& matlabClassname, + const std::vector& using_namespaces, const std::vector& includes); ///< cpp wrapper }; diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 9c0feff69..2fd8c9747 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -184,7 +184,10 @@ Module::Module(const string& interfacePath, str_p("}///\\namespace") >> !namespace_name_p // end namespace, avoid confusion with classes [pop_a(namespaces)]; - Rule module_content_p = comments_p | class_p | namespace_def_p ; + Rule using_namespace_p = str_p("using") >> str_p("namespace") + >> namespace_name_p[push_back_a(using_namespaces)] >> ch_p(';'); + + Rule module_content_p = comments_p | using_namespace_p | class_p | namespace_def_p ; Rule module_p = *module_content_p >> !end_p; @@ -238,7 +241,6 @@ void verifyArguments(const vector& validArgs, const vector& vt) { /* ************************************************************************* */ void Module::matlab_code(const string& toolboxPath, - const string& nameSpace, const string& mexExt, const string& mexFlags) { @@ -306,9 +308,9 @@ void Module::matlab_code(const string& toolboxPath, verifyArguments(validArgs, cls.methods); // create constructor and method wrappers - cls.matlab_constructors(toolboxPath,nameSpace); - cls.matlab_static_methods(toolboxPath,nameSpace); - cls.matlab_methods(classPath,nameSpace); + cls.matlab_constructors(toolboxPath,using_namespaces); + cls.matlab_static_methods(toolboxPath,using_namespaces); + cls.matlab_methods(classPath,using_namespaces); // add lines to make m-file ofs << "%% " << cls.qualifiedName() << endl; diff --git a/wrap/Module.h b/wrap/Module.h index 6ae9202cb..3c294f380 100644 --- a/wrap/Module.h +++ b/wrap/Module.h @@ -31,6 +31,7 @@ struct Module { std::string name; ///< module name std::vector classes; ///< list of classes bool verbose; ///< verbose flag + std::vector using_namespaces; ///< all default namespaces /// constructor that parses interface file Module(const std::string& interfacePath, @@ -39,7 +40,6 @@ struct Module { /// MATLAB code generation: void matlab_code(const std::string& path, - const std::string& nameSpace, const std::string& mexExt, const std::string& mexFlags); }; diff --git a/wrap/StaticMethod.cpp b/wrap/StaticMethod.cpp index 17f3e2a60..02a2aaf76 100644 --- a/wrap/StaticMethod.cpp +++ b/wrap/StaticMethod.cpp @@ -50,7 +50,8 @@ void StaticMethod::matlab_mfile(const string& toolboxPath, const string& classNa /* ************************************************************************* */ void StaticMethod::matlab_wrapper(const string& toolboxPath, const string& className, - const string& matlabClassName, const string& cppClassName, const string& nameSpace, + const string& matlabClassName, const string& cppClassName, + const vector& using_namespaces, const std::vector& includes) { // open destination wrapperFile @@ -64,14 +65,8 @@ void StaticMethod::matlab_wrapper(const string& toolboxPath, const string& class // header wrap::emit_header_comment(ofs, "//"); - ofs << "#include \n"; - if (includes.empty()) // add a default include - ofs << "#include <" << className << ".h>" << endl; - else { - BOOST_FOREACH(const string& s, includes) - ofs << "#include <" << s << ">" << endl; - } - if (!nameSpace.empty()) ofs << "using namespace " << nameSpace << ";" << endl; + generateIncludes(ofs, className, includes); + generateUsingNamespace(ofs, using_namespaces); // call ofs << "void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; diff --git a/wrap/StaticMethod.h b/wrap/StaticMethod.h index 4038a398e..429e24183 100644 --- a/wrap/StaticMethod.h +++ b/wrap/StaticMethod.h @@ -47,7 +47,8 @@ struct StaticMethod { void matlab_mfile(const std::string& toolboxPath, const std::string& className); ///< m-file void matlab_wrapper(const std::string& toolboxPath, const std::string& className, const std::string& matlabClassName, - const std::string& cppClassName, const std::string& nameSpace, + const std::string& cppClassName, + const std::vector& using_namespaces, const std::vector& includes); ///< cpp wrapper }; diff --git a/wrap/tests/expected/@Point2/dim.cpp b/wrap/tests/expected/@Point2/dim.cpp index 1d6486356..e845db94a 100644 --- a/wrap/tests/expected/@Point2/dim.cpp +++ b/wrap/tests/expected/@Point2/dim.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("dim",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Point2/vectorConfusion.cpp b/wrap/tests/expected/@Point2/vectorConfusion.cpp new file mode 100644 index 000000000..cdb06b2df --- /dev/null +++ b/wrap/tests/expected/@Point2/vectorConfusion.cpp @@ -0,0 +1,11 @@ +// automatically generated by wrap on 2011-Dec-09 +#include +#include +using namespace geometry; +void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) +{ + checkArguments("vectorConfusion",nargout,nargin-1,0); + shared_ptr self = unwrap_shared_ptr< Point2 >(in[0],"Point2"); + VectorNotEigen result = self->vectorConfusion(); + out[0] = wrap_shared_ptr(make_shared< VectorNotEigen >(result),"VectorNotEigen"); +} diff --git a/wrap/tests/expected/@Point2/vectorConfusion.m b/wrap/tests/expected/@Point2/vectorConfusion.m new file mode 100644 index 000000000..d141e8085 --- /dev/null +++ b/wrap/tests/expected/@Point2/vectorConfusion.m @@ -0,0 +1,4 @@ +function result = vectorConfusion(obj) +% usage: obj.vectorConfusion() + error('need to compile vectorConfusion.cpp'); +end diff --git a/wrap/tests/expected/@Point2/x.cpp b/wrap/tests/expected/@Point2/x.cpp index 4c2ef7b33..d245748d0 100644 --- a/wrap/tests/expected/@Point2/x.cpp +++ b/wrap/tests/expected/@Point2/x.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("x",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Point2/y.cpp b/wrap/tests/expected/@Point2/y.cpp index 525238ea9..6342d6238 100644 --- a/wrap/tests/expected/@Point2/y.cpp +++ b/wrap/tests/expected/@Point2/y.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("y",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Point3/norm.cpp b/wrap/tests/expected/@Point3/norm.cpp index 4c494a5f8..ad36e32b8 100644 --- a/wrap/tests/expected/@Point3/norm.cpp +++ b/wrap/tests/expected/@Point3/norm.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("norm",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Test/arg_EigenConstRef.cpp b/wrap/tests/expected/@Test/arg_EigenConstRef.cpp index b2b779c9f..e44b74f1b 100644 --- a/wrap/tests/expected/@Test/arg_EigenConstRef.cpp +++ b/wrap/tests/expected/@Test/arg_EigenConstRef.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("arg_EigenConstRef",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/arg_EigenConstRef.m b/wrap/tests/expected/@Test/arg_EigenConstRef.m new file mode 100644 index 000000000..e353faa29 --- /dev/null +++ b/wrap/tests/expected/@Test/arg_EigenConstRef.m @@ -0,0 +1,4 @@ +function result = arg_EigenConstRef(obj,value) +% usage: obj.arg_EigenConstRef(value) + error('need to compile arg_EigenConstRef.cpp'); +end diff --git a/wrap/tests/expected/@Test/create_MixedPtrs.cpp b/wrap/tests/expected/@Test/create_MixedPtrs.cpp index 19a0eb740..a2c237c05 100644 --- a/wrap/tests/expected/@Test/create_MixedPtrs.cpp +++ b/wrap/tests/expected/@Test/create_MixedPtrs.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("create_MixedPtrs",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Test/create_ptrs.cpp b/wrap/tests/expected/@Test/create_ptrs.cpp index 26e901123..33722bd14 100644 --- a/wrap/tests/expected/@Test/create_ptrs.cpp +++ b/wrap/tests/expected/@Test/create_ptrs.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("create_ptrs",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Test/print.cpp b/wrap/tests/expected/@Test/print.cpp index 90e1b0965..e92a58b10 100644 --- a/wrap/tests/expected/@Test/print.cpp +++ b/wrap/tests/expected/@Test/print.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("print",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Test/return_Point2Ptr.cpp b/wrap/tests/expected/@Test/return_Point2Ptr.cpp index 400a0f9df..99cf67f0b 100644 --- a/wrap/tests/expected/@Test/return_Point2Ptr.cpp +++ b/wrap/tests/expected/@Test/return_Point2Ptr.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_Point2Ptr",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_Test.cpp b/wrap/tests/expected/@Test/return_Test.cpp index c7a91cbe3..19256f9ac 100644 --- a/wrap/tests/expected/@Test/return_Test.cpp +++ b/wrap/tests/expected/@Test/return_Test.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_Test",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_TestPtr.cpp b/wrap/tests/expected/@Test/return_TestPtr.cpp index 2f2fd7ef4..39ed01f15 100644 --- a/wrap/tests/expected/@Test/return_TestPtr.cpp +++ b/wrap/tests/expected/@Test/return_TestPtr.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_TestPtr",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_bool.cpp b/wrap/tests/expected/@Test/return_bool.cpp index 6d57ad149..016bf2934 100644 --- a/wrap/tests/expected/@Test/return_bool.cpp +++ b/wrap/tests/expected/@Test/return_bool.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_bool",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_double.cpp b/wrap/tests/expected/@Test/return_double.cpp index adf1847b3..c087c194b 100644 --- a/wrap/tests/expected/@Test/return_double.cpp +++ b/wrap/tests/expected/@Test/return_double.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_double",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_field.cpp b/wrap/tests/expected/@Test/return_field.cpp index f4de80c8a..3c2ea29b0 100644 --- a/wrap/tests/expected/@Test/return_field.cpp +++ b/wrap/tests/expected/@Test/return_field.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_field",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_int.cpp b/wrap/tests/expected/@Test/return_int.cpp index 736b9c981..c9bada69e 100644 --- a/wrap/tests/expected/@Test/return_int.cpp +++ b/wrap/tests/expected/@Test/return_int.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_int",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_matrix1.cpp b/wrap/tests/expected/@Test/return_matrix1.cpp index 9dfc56332..acd39c9f5 100644 --- a/wrap/tests/expected/@Test/return_matrix1.cpp +++ b/wrap/tests/expected/@Test/return_matrix1.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_matrix1",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_matrix2.cpp b/wrap/tests/expected/@Test/return_matrix2.cpp index c55b3c56c..50e2ee462 100644 --- a/wrap/tests/expected/@Test/return_matrix2.cpp +++ b/wrap/tests/expected/@Test/return_matrix2.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_matrix2",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_pair.cpp b/wrap/tests/expected/@Test/return_pair.cpp index a629cd757..a10e79109 100644 --- a/wrap/tests/expected/@Test/return_pair.cpp +++ b/wrap/tests/expected/@Test/return_pair.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_pair",nargout,nargin-1,2); diff --git a/wrap/tests/expected/@Test/return_ptrs.cpp b/wrap/tests/expected/@Test/return_ptrs.cpp index bcd24536d..6c2ab46a6 100644 --- a/wrap/tests/expected/@Test/return_ptrs.cpp +++ b/wrap/tests/expected/@Test/return_ptrs.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_ptrs",nargout,nargin-1,2); diff --git a/wrap/tests/expected/@Test/return_size_t.cpp b/wrap/tests/expected/@Test/return_size_t.cpp index f0a05cdcc..af1524ec7 100644 --- a/wrap/tests/expected/@Test/return_size_t.cpp +++ b/wrap/tests/expected/@Test/return_size_t.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_size_t",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_string.cpp b/wrap/tests/expected/@Test/return_string.cpp index 28dab1870..71a86e63b 100644 --- a/wrap/tests/expected/@Test/return_string.cpp +++ b/wrap/tests/expected/@Test/return_string.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_string",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_vector1.cpp b/wrap/tests/expected/@Test/return_vector1.cpp index 38b8f1b0b..df8779989 100644 --- a/wrap/tests/expected/@Test/return_vector1.cpp +++ b/wrap/tests/expected/@Test/return_vector1.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_vector1",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Test/return_vector2.cpp b/wrap/tests/expected/@Test/return_vector2.cpp index c629d2eb2..ac87ab83a 100644 --- a/wrap/tests/expected/@Test/return_vector2.cpp +++ b/wrap/tests/expected/@Test/return_vector2.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("return_vector2",nargout,nargin-1,1); diff --git a/wrap/tests/expected/Point3_StaticFunctionRet.cpp b/wrap/tests/expected/Point3_StaticFunctionRet.cpp index 1877b7e99..657f90bb1 100644 --- a/wrap/tests/expected/Point3_StaticFunctionRet.cpp +++ b/wrap/tests/expected/Point3_StaticFunctionRet.cpp @@ -1,9 +1,11 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { - checkArguments("Point3_StaticFunctionRet",nargout,nargin,0); - Point3 result = Point3::StaticFunctionRet(); + checkArguments("Point3_StaticFunctionRet",nargout,nargin,1); + double z = unwrap< double >(in[0]); + Point3 result = Point3::StaticFunctionRet(z); out[0] = wrap_shared_ptr(make_shared< Point3 >(result),"Point3"); } diff --git a/wrap/tests/expected/Point3_staticFunction.cpp b/wrap/tests/expected/Point3_staticFunction.cpp index 869810709..e0519e20e 100644 --- a/wrap/tests/expected/Point3_staticFunction.cpp +++ b/wrap/tests/expected/Point3_staticFunction.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("Point3_staticFunction",nargout,nargin,0); diff --git a/wrap/tests/expected/new_Point2_.cpp b/wrap/tests/expected/new_Point2_.cpp index 34ef89203..5cfcad238 100644 --- a/wrap/tests/expected/new_Point2_.cpp +++ b/wrap/tests/expected/new_Point2_.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("new_Point2_",nargout,nargin,0); diff --git a/wrap/tests/expected/new_Point2_dd.cpp b/wrap/tests/expected/new_Point2_dd.cpp index 61d4d1535..6ab5721a1 100644 --- a/wrap/tests/expected/new_Point2_dd.cpp +++ b/wrap/tests/expected/new_Point2_dd.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("new_Point2_dd",nargout,nargin,2); diff --git a/wrap/tests/expected/new_Point3_ddd.cpp b/wrap/tests/expected/new_Point3_ddd.cpp index f9bed0d45..285ed9ca6 100644 --- a/wrap/tests/expected/new_Point3_ddd.cpp +++ b/wrap/tests/expected/new_Point3_ddd.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-06 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("new_Point3_ddd",nargout,nargin,3); diff --git a/wrap/tests/expected/new_Test_.cpp b/wrap/tests/expected/new_Test_.cpp index d3cf0d658..9c9aaab88 100644 --- a/wrap/tests/expected/new_Test_.cpp +++ b/wrap/tests/expected/new_Test_.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("new_Test_",nargout,nargin,0); diff --git a/wrap/tests/expected/new_Test_dM.cpp b/wrap/tests/expected/new_Test_dM.cpp index 94c473061..4179921da 100644 --- a/wrap/tests/expected/new_Test_dM.cpp +++ b/wrap/tests/expected/new_Test_dM.cpp @@ -1,6 +1,7 @@ -// automatically generated by wrap on 2011-Dec-08 +// automatically generated by wrap on 2011-Dec-09 #include #include +using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("new_Test_dM",nargout,nargin,2); diff --git a/wrap/tests/geometry.h b/wrap/tests/geometry.h index f4adb8eb6..d77d6ee77 100644 --- a/wrap/tests/geometry.h +++ b/wrap/tests/geometry.h @@ -1,5 +1,9 @@ // comments! +// set the default namespace +// location of namespace isn't significant +using namespace geometry; + class Point2 { Point2(); Point2(double x, double y); diff --git a/wrap/tests/testWrap.cpp b/wrap/tests/testWrap.cpp index d94834aed..d4277533a 100644 --- a/wrap/tests/testWrap.cpp +++ b/wrap/tests/testWrap.cpp @@ -56,7 +56,7 @@ TEST( wrap, check_exception ) { string path = topdir + "/wrap/tests"; Module module(path.c_str(), "testWrap1",enable_verbose); - CHECK_EXCEPTION(module.matlab_code("actual", "", "mexa64", "-O5"), DependencyMissing); + CHECK_EXCEPTION(module.matlab_code("actual", "mexa64", "-O5"), DependencyMissing); } /* ************************************************************************* */ @@ -65,6 +65,10 @@ TEST( wrap, parse ) { Module module(header_path.c_str(), "geometry",enable_verbose); EXPECT_LONGS_EQUAL(3, module.classes.size()); + // check using declarations + EXPECT_LONGS_EQUAL(1, module.using_namespaces.size()); + EXPECT(assert_equal("geometry", module.using_namespaces.front())); + // check first class, Point2 { Class cls = module.classes.at(0); @@ -173,7 +177,7 @@ TEST( wrap, matlab_code_namespaces ) { // emit MATLAB code string exp_path = path + "/tests/expected_namespaces/"; string act_path = "actual_namespaces/"; - module.matlab_code("actual_namespaces", "", "mexa64", "-O5"); + module.matlab_code("actual_namespaces", "mexa64", "-O5"); EXPECT(files_equal(exp_path + "make_testNamespaces.m", act_path + "make_testNamespaces.m")); EXPECT(files_equal(exp_path + "Makefile" , act_path + "Makefile" )); @@ -192,7 +196,7 @@ TEST( wrap, matlab_code ) { // emit MATLAB code // make_geometry will not compile, use make testwrap to generate real make - module.matlab_code("actual", "", "mexa64", "-O5"); + module.matlab_code("actual", "mexa64", "-O5"); EXPECT(files_equal(path + "/tests/expected/@Point2/Point2.m" , "actual/@Point2/Point2.m" )); EXPECT(files_equal(path + "/tests/expected/@Point2/x.cpp" , "actual/@Point2/x.cpp" )); diff --git a/wrap/utilities.cpp b/wrap/utilities.cpp index 2dc93b49d..6877b29c2 100644 --- a/wrap/utilities.cpp +++ b/wrap/utilities.cpp @@ -15,8 +15,8 @@ **/ #include -#include +#include #include #include "utilities.h" @@ -42,12 +42,12 @@ string file_contents(const string& filename, bool skipheader) { } /* ************************************************************************* */ -bool assert_equal(const std::string& expected, const std::string& actual) { +bool assert_equal(const string& expected, const string& actual) { if (expected == actual) return true; printf("Not equal:\n"); - std::cout << "expected: [" << expected << "]\n"; - std::cout << "actual: [" << actual << "]" << std::endl; + cout << "expected: [" << expected << "]\n"; + cout << "actual: [" << actual << "]" << endl; return false; } @@ -82,13 +82,32 @@ void emit_header_comment(ofstream& ofs, const string& delimiter) { } /* ************************************************************************* */ -std::string maybe_shared_ptr(bool add, const std::string& type) { +string maybe_shared_ptr(bool add, const string& type) { string str = add? "shared_ptr<" : ""; str += type; if (add) str += ">"; return str; } +/* ************************************************************************* */ +void generateUsingNamespace(ofstream& ofs, const vector& using_namespaces) { + if (using_namespaces.empty()) return; + BOOST_FOREACH(const string& s, using_namespaces) + ofs << "using namespace " << s << ";" << endl; +} + +/* ************************************************************************* */ +void generateIncludes(std::ofstream& ofs, const std::string& class_name, + const std::vector& includes) { + ofs << "#include " << endl; + if (includes.empty()) // add a default include + ofs << "#include <" << class_name << ".h>" << endl; + else { + BOOST_FOREACH(const string& s, includes) + ofs << "#include <" << s << ">" << endl; + } +} + /* ************************************************************************* */ } // \namespace wrap diff --git a/wrap/utilities.h b/wrap/utilities.h index f43b9d9fb..f31dd7fd3 100644 --- a/wrap/utilities.h +++ b/wrap/utilities.h @@ -16,7 +16,9 @@ #pragma once +#include #include +#include #include namespace wrap { @@ -85,4 +87,15 @@ void emit_header_comment(std::ofstream& ofs, const std::string& delimiter); // auxiliary function to wrap an argument into a shared_ptr template std::string maybe_shared_ptr(bool add, const std::string& type); +/** + * Creates the "using namespace [name];" declarations + */ +void generateUsingNamespace(std::ofstream& ofs, const std::vector& using_namespaces); + +/** + * Creates the #include statements + */ +void generateIncludes(std::ofstream& ofs, const std::string& class_name, + const std::vector& includes); + } // \namespace wrap diff --git a/wrap/wrap.cpp b/wrap/wrap.cpp index 4fdd2d3fa..e2aaa6aed 100644 --- a/wrap/wrap.cpp +++ b/wrap/wrap.cpp @@ -35,7 +35,6 @@ void generate_matlab_toolbox(const string& mexExt, const string& interfacePath, const string& moduleName, const string& toolboxPath, - const string& nameSpace, const string& mexFlags) { // Parse interface file into class object @@ -43,7 +42,7 @@ void generate_matlab_toolbox(const string& mexExt, wrap::Module module(interfacePath, moduleName, true); // Then emit MATLAB code - module.matlab_code(toolboxPath,nameSpace,mexExt,mexFlags); + module.matlab_code(toolboxPath,mexExt,mexFlags); } /** @@ -58,10 +57,9 @@ int main(int argc, const char* argv[]) { cerr << " interfacePath : *absolute* path to directory of module interface file" << endl; cerr << " moduleName : the name of the module, interface file must be called moduleName.h" << endl; cerr << " toolboxPath : the directory in which to generate the wrappers" << endl; - cerr << " nameSpace : namespace to use, pass empty string if none" << endl; cerr << " [mexFlags] : extra flags for the mex command" << endl; } else - generate_matlab_toolbox(argv[1],argv[2],argv[3],argv[4],argv[5],argc==6 ? " " : argv[6]); + generate_matlab_toolbox(argv[1],argv[2],argv[3],argv[4],argc==5 ? " " : argv[5]); return 0; }