diff --git a/gtsam.h b/gtsam.h index 26f3457c7..0e4d70e16 100644 --- a/gtsam.h +++ b/gtsam.h @@ -32,6 +32,9 @@ * Namespace usage * - Namespaces can be specified for classes in arguments and return values * - In each case, the namespace must be fully specified, e.g., "namespace1::namespace2::ClassName" + * Using namespace + * - To use a namespace (e.g., generate a "using namespace x" line in cpp files), add "using namespace x;" + * - This declaration applies to all classes *after* the declaration, regardless of brackets * Methods must start with a lowercase letter * Static methods must start with a letter (upper or lowercase) and use the "static" keyword * Includes in C++ wrappers diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 45c2268ed..6f6f91b0a 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -61,7 +61,7 @@ void Class::matlab_proxy(const string& classFile) const { } /* ************************************************************************* */ -void Class::matlab_constructors(const string& toolboxPath, const vector& using_namespaces) const { +void Class::matlab_constructors(const string& toolboxPath) const { BOOST_FOREACH(Constructor c, constructors) { c.matlab_mfile (toolboxPath, qualifiedName()); c.matlab_wrapper(toolboxPath, qualifiedName("::"), qualifiedName(), using_namespaces, includes); @@ -69,12 +69,12 @@ void Class::matlab_constructors(const string& toolboxPath, const vector& } /* ************************************************************************* */ -void Class::matlab_deconstructor(const string& toolboxPath, const vector& using_namespaces) const { +void Class::matlab_deconstructor(const string& toolboxPath) const { d.matlab_mfile (toolboxPath, qualifiedName()); d.matlab_wrapper(toolboxPath, qualifiedName("::"), qualifiedName(), using_namespaces, includes); } /* ************************************************************************* */ -void Class::matlab_methods(const string& classPath, const vector& using_namespaces) const { +void Class::matlab_methods(const string& classPath) const { string matlabName = qualifiedName(), cppName = qualifiedName("::"); BOOST_FOREACH(Method m, methods) { m.matlab_mfile (classPath); @@ -83,7 +83,7 @@ void Class::matlab_methods(const string& classPath, const vector& using_ } /* ************************************************************************* */ -void Class::matlab_static_methods(const string& toolboxPath, const vector& using_namespaces) const { +void Class::matlab_static_methods(const string& toolboxPath) const { string matlabName = qualifiedName(), cppName = qualifiedName("::"); BOOST_FOREACH(const StaticMethod& m, static_methods) { m.matlab_mfile (toolboxPath, qualifiedName()); diff --git a/wrap/Class.h b/wrap/Class.h index 1fec927e8..875b3203e 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -37,20 +37,17 @@ struct Class { std::vector methods; ///< Class methods std::vector static_methods; ///< Static methods std::vector namespaces; ///< Stack of namespaces + std::vector using_namespaces; ///< default namespaces std::vector includes; ///< header include overrides Deconstructor d; bool verbose_; ///< verbose flag // And finally MATLAB code is emitted, methods below called by Module::matlab_code void matlab_proxy(const std::string& classFile) const; ///< emit proxy class - void matlab_constructors(const std::string& toolboxPath, - const std::vector& using_namespaces) const; ///< emit constructor wrappers - void matlab_deconstructor(const std::string& toolboxPath, - const std::vector& using_namespaces) const; - void matlab_methods(const std::string& classPath, - const std::vector& using_namespaces) const; ///< emit method wrappers - void matlab_static_methods(const std::string& classPath, - const std::vector& using_namespaces) const; ///< emit static method wrappers + void matlab_constructors(const std::string& toolboxPath) const; ///< emit constructor wrappers + void matlab_deconstructor(const std::string& toolboxPath) const; + void matlab_methods(const std::string& classPath) const; ///< emit method wrappers + void matlab_static_methods(const std::string& classPath) const; ///< emit static method wrappers void matlab_make_fragment(FileWriter& file, const std::string& toolboxPath, const std::string& mexFlags) const; ///< emit make fragment for global make script diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 179ee86ef..dfd781016 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -57,7 +57,8 @@ Module::Module(const string& interfacePath, Class cls0(enable_verbose),cls(enable_verbose); vector namespaces, /// current namespace tag namespace_includes, /// current set of includes - namespaces_return; /// namespace for current return type + namespaces_return, /// namespace for current return type + using_namespace_current; /// All namespaces from "using" declarations string include_path = ""; string class_name = ""; const string null_str = ""; @@ -182,6 +183,7 @@ Module::Module(const string& interfacePath, >> *(functions_p | comments_p) >> str_p("};")) [assign_a(cls.namespaces, namespaces)] + [assign_a(cls.using_namespaces, using_namespace_current)] [append_a(cls.includes, namespace_includes)] [assign_a(deconstructor.name,cls.name)] [assign_a(cls.d, deconstructor)] @@ -202,7 +204,7 @@ Module::Module(const string& interfacePath, Rule using_namespace_p = str_p("using") >> str_p("namespace") - >> namespace_name_p[push_back_a(using_namespaces)] >> ch_p(';'); + >> namespace_name_p[push_back_a(using_namespace_current)] >> ch_p(';'); Rule forward_declaration_p = str_p("class") >> @@ -342,12 +344,12 @@ void Module::matlab_code(const string& toolboxPath, verifyReturnTypes(validTypes, cls.methods); // create constructor and method wrappers - cls.matlab_constructors(toolboxPath,using_namespaces); - cls.matlab_static_methods(toolboxPath,using_namespaces); - cls.matlab_methods(classPath,using_namespaces); + cls.matlab_constructors(toolboxPath); + cls.matlab_static_methods(toolboxPath); + cls.matlab_methods(classPath); // create deconstructor - cls.matlab_deconstructor(toolboxPath,using_namespaces); + cls.matlab_deconstructor(toolboxPath); // add lines to make m-file makeModuleMfile.oss << "%% " << cls.qualifiedName() << endl; diff --git a/wrap/Module.h b/wrap/Module.h index a900310eb..3e97d9ba2 100644 --- a/wrap/Module.h +++ b/wrap/Module.h @@ -31,7 +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 +// std::vector using_namespaces; ///< all default namespaces std::vector forward_declarations; /// constructor that parses interface file diff --git a/wrap/tests/expected/@Point2/argChar.cpp b/wrap/tests/expected/@Point2/argChar.cpp index 16187cfec..017fe1761 100644 --- a/wrap/tests/expected/@Point2/argChar.cpp +++ b/wrap/tests/expected/@Point2/argChar.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #include #include -using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("argChar",nargout,nargin-1,1); diff --git a/wrap/tests/expected/@Point2/argUChar.cpp b/wrap/tests/expected/@Point2/argUChar.cpp new file mode 100644 index 000000000..341e4f8a7 --- /dev/null +++ b/wrap/tests/expected/@Point2/argUChar.cpp @@ -0,0 +1,10 @@ +// automatically generated by wrap +#include +#include +void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) +{ + checkArguments("argUChar",nargout,nargin-1,1); + shared_ptr self = unwrap_shared_ptr< Point2 >(in[0],"Point2"); + unsigned char a = unwrap< unsigned char >(in[1]); + self->argUChar(a); +} diff --git a/wrap/tests/expected/@Point2/argUChar.m b/wrap/tests/expected/@Point2/argUChar.m new file mode 100644 index 000000000..eedb50df0 --- /dev/null +++ b/wrap/tests/expected/@Point2/argUChar.m @@ -0,0 +1,5 @@ + automatically generated by wrap +function result = argUChar(obj,a) +% usage: obj.argUChar(a) + error('need to compile argUChar.cpp'); +end diff --git a/wrap/tests/expected/@Point2/dim.cpp b/wrap/tests/expected/@Point2/dim.cpp index f4fa9be65..454153690 100644 --- a/wrap/tests/expected/@Point2/dim.cpp +++ b/wrap/tests/expected/@Point2/dim.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #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/returnChar.cpp b/wrap/tests/expected/@Point2/returnChar.cpp index 8ec723283..3b7733862 100644 --- a/wrap/tests/expected/@Point2/returnChar.cpp +++ b/wrap/tests/expected/@Point2/returnChar.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #include #include -using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("returnChar",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Point2/vectorConfusion.cpp b/wrap/tests/expected/@Point2/vectorConfusion.cpp index cd8be15d2..90be4194b 100644 --- a/wrap/tests/expected/@Point2/vectorConfusion.cpp +++ b/wrap/tests/expected/@Point2/vectorConfusion.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #include #include -using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("vectorConfusion",nargout,nargin-1,0); diff --git a/wrap/tests/expected/@Point2/x.cpp b/wrap/tests/expected/@Point2/x.cpp index 2c994c8e2..8df8c6bbc 100644 --- a/wrap/tests/expected/@Point2/x.cpp +++ b/wrap/tests/expected/@Point2/x.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #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 d0b007125..14e3663ed 100644 --- a/wrap/tests/expected/@Point2/y.cpp +++ b/wrap/tests/expected/@Point2/y.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #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/delete_Point2.cpp b/wrap/tests/expected/delete_Point2.cpp index b24557215..bd8bd12eb 100644 --- a/wrap/tests/expected/delete_Point2.cpp +++ b/wrap/tests/expected/delete_Point2.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #include #include -using namespace geometry; void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[]) { checkArguments("delete_Point2",nargout,nargin,1); diff --git a/wrap/tests/expected/new_Point2_.cpp b/wrap/tests/expected/new_Point2_.cpp index 661307b60..a88076efb 100644 --- a/wrap/tests/expected/new_Point2_.cpp +++ b/wrap/tests/expected/new_Point2_.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #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 2c6e743ca..7c7f062b7 100644 --- a/wrap/tests/expected/new_Point2_dd.cpp +++ b/wrap/tests/expected/new_Point2_dd.cpp @@ -1,7 +1,6 @@ // automatically generated by wrap #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/geometry.h b/wrap/tests/geometry.h index 02502979a..a287c44e6 100644 --- a/wrap/tests/geometry.h +++ b/wrap/tests/geometry.h @@ -1,9 +1,5 @@ // comments! -// set the default namespace -// location of namespace isn't significant -using namespace geometry; - class VectorNotEigen; class ns::OtherClass; @@ -19,6 +15,9 @@ class Point2 { VectorNotEigen vectorConfusion(); }; +// flag a namespace as in use - only applies *after* the declaration +using namespace geometry; + class Point3 { Point3(double x, double y, double z); double norm() const; diff --git a/wrap/tests/testWrap.cpp b/wrap/tests/testWrap.cpp index 90ea65ebc..cdee692a5 100644 --- a/wrap/tests/testWrap.cpp +++ b/wrap/tests/testWrap.cpp @@ -72,8 +72,7 @@ TEST( wrap, parse ) { EXPECT_LONGS_EQUAL(3, module.classes.size()); // check using declarations - strvec exp_using; exp_using += "geometry"; - EXPECT(assert_equal(exp_using, module.using_namespaces)); + strvec exp_using1, exp_using2; exp_using2 += "geometry"; // forward declarations strvec exp_forward; exp_forward += "VectorNotEigen", "ns::OtherClass"; @@ -87,6 +86,7 @@ TEST( wrap, parse ) { EXPECT_LONGS_EQUAL(7, cls.methods.size()); EXPECT_LONGS_EQUAL(0, cls.static_methods.size()); EXPECT_LONGS_EQUAL(0, cls.namespaces.size()); + EXPECT(assert_equal(exp_using1, cls.using_namespaces)); } // check second class, Point3 @@ -97,6 +97,7 @@ TEST( wrap, parse ) { EXPECT_LONGS_EQUAL(1, cls.methods.size()); EXPECT_LONGS_EQUAL(2, cls.static_methods.size()); EXPECT_LONGS_EQUAL(0, cls.namespaces.size()); + EXPECT(assert_equal(exp_using2, cls.using_namespaces)); // first constructor takes 3 doubles Constructor c1 = cls.constructors.front(); @@ -125,6 +126,7 @@ TEST( wrap, parse ) { EXPECT_LONGS_EQUAL(19, testCls.methods.size()); EXPECT_LONGS_EQUAL( 0, testCls.static_methods.size()); EXPECT_LONGS_EQUAL( 0, testCls.namespaces.size()); + EXPECT(assert_equal(exp_using2, testCls.using_namespaces)); strvec exp_includes; exp_includes += "folder/path/to/Test.h"; EXPECT(assert_equal(exp_includes, testCls.includes));