From 09e3c7df9fb162cad91ccac3abc032d4364c701a Mon Sep 17 00:00:00 2001 From: dellaert Date: Thu, 13 Nov 2014 21:34:59 +0100 Subject: [PATCH] struct Constructor: public ArgumentOverloads --- wrap/Class.cpp | 20 +++++++------------- wrap/Class.h | 5 ++++- wrap/Constructor.cpp | 11 ----------- wrap/Constructor.h | 30 ++++++++++++++++++++++++------ wrap/Module.cpp | 2 +- wrap/StaticMethod.h | 6 ++++++ wrap/tests/testWrap.cpp | 8 ++++---- 7 files changed, 46 insertions(+), 36 deletions(-) diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 7ab618f0f..316f65da2 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -73,13 +73,15 @@ void Class::matlab_proxy(Str toolboxPath, Str wrapperName, pointer_constructor_fragments(proxyFile, wrapperFile, wrapperName, functionNames); wrapperFile.oss << "\n"; + // Regular constructors - BOOST_FOREACH(ArgumentList a, constructor.args_list) { + for (size_t i = 0; i < constructor.nrOverloads(); i++) { + ArgumentList args = constructor.argumentList(i); const int id = (int) functionNames.size(); constructor.proxy_fragment(proxyFile, wrapperName, !qualifiedParent.empty(), - id, a); + id, args); const string wrapFunctionName = constructor.wrapper_fragment(wrapperFile, - cppName, matlabUniqueName, cppBaseName, id, a); + cppName, matlabUniqueName, cppBaseName, id, args); wrapperFile.oss << "\n"; functionNames.push_back(wrapFunctionName); } @@ -244,8 +246,7 @@ Class Class::expandTemplate(const TemplateSubstitution& ts) const { Class inst = *this; inst.methods = expandMethodTemplate(methods, ts); inst.static_methods = expandMethodTemplate(static_methods, ts); - inst.constructor.args_list = inst.constructor.expandArgumentListsTemplate(ts); - inst.constructor.name = inst.name; + inst.constructor = constructor.expandTemplate(ts); inst.deconstructor.name = inst.name; cout << inst << endl; return inst; @@ -374,19 +375,12 @@ string Class::getTypedef() const { } /* ************************************************************************* */ - void Class::comment_fragment(FileWriter& proxyFile) const { proxyFile.oss << "%class " << name << ", see Doxygen page for details\n"; proxyFile.oss << "%at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html\n"; - if (!constructor.args_list.empty()) - proxyFile.oss << "%\n%-------Constructors-------\n"; - BOOST_FOREACH(ArgumentList argList, constructor.args_list) { - proxyFile.oss << "%"; - argList.emit_prototype(proxyFile, name); - proxyFile.oss << "\n"; - } + constructor.comment_fragment(proxyFile); if (!methods.empty()) proxyFile.oss << "%\n%-------Methods-------\n"; diff --git a/wrap/Class.h b/wrap/Class.h index 2c2de49fc..6b9119316 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -112,7 +112,10 @@ public: Str wrapperName, std::vector& functionNames) const; friend std::ostream& operator<<(std::ostream& os, const Class& cls) { - os << "class " << cls.name << "{\n"; + os << "class " << cls.name << "{\n"; + os << cls.constructor << ";\n"; + BOOST_FOREACH(const StaticMethod& m, cls.static_methods | boost::adaptors::map_values) + os << m << ";\n"; BOOST_FOREACH(const Method& m, cls.methods | boost::adaptors::map_values) os << m << ";\n"; os << "};" << std::endl; diff --git a/wrap/Constructor.cpp b/wrap/Constructor.cpp index 98a689ced..fdbbf0e42 100644 --- a/wrap/Constructor.cpp +++ b/wrap/Constructor.cpp @@ -36,17 +36,6 @@ string Constructor::matlab_wrapper_name(const string& className) const { return str; } -/* ************************************************************************* */ -vector Constructor::expandArgumentListsTemplate( - const TemplateSubstitution& ts) const { - vector result; - BOOST_FOREACH(const ArgumentList& argList, args_list) { - ArgumentList instArgList = argList.expandTemplate(ts); - result.push_back(instArgList); - } - return result; -} - /* ************************************************************************* */ void Constructor::proxy_fragment(FileWriter& file, const std::string& wrapperName, bool hasParent, const int id, const ArgumentList args) const { diff --git a/wrap/Constructor.h b/wrap/Constructor.h index 40bca549a..fd9e0d32c 100644 --- a/wrap/Constructor.h +++ b/wrap/Constructor.h @@ -18,7 +18,7 @@ #pragma once -#include "Argument.h" +#include "Function.h" #include #include @@ -26,7 +26,7 @@ namespace wrap { // Constructor class -struct Constructor { +struct Constructor: public ArgumentOverloads { /// Constructor creates an empty class Constructor(bool verbose = false) : @@ -34,13 +34,15 @@ struct Constructor { } // Then the instance variables are set directly by the Module constructor - std::vector args_list; std::string name; bool verbose_; - // TODO eliminate copy/paste with function - std::vector expandArgumentListsTemplate( - const TemplateSubstitution& ts) const; + Constructor expandTemplate(const TemplateSubstitution& ts) const { + Constructor inst = *this; + inst.argLists_ = expandArgumentListsTemplate(ts); + inst.name = inst.name; + return inst; + } // MATLAB code generation // toolboxPath is main toolbox directory, e.g., ../matlab @@ -49,6 +51,16 @@ struct Constructor { /// wrapper name std::string matlab_wrapper_name(const std::string& className) const; + void comment_fragment(FileWriter& proxyFile) const { + if (nrOverloads() > 0) + proxyFile.oss << "%\n%-------Constructors-------\n"; + for (size_t i = 0; i < nrOverloads(); i++) { + proxyFile.oss << "%"; + argumentList(i).emit_prototype(proxyFile, name); + proxyFile.oss << "\n"; + } + } + /** * Create fragment to select constructor in proxy class, e.g., * if nargin == 2, obj.self = new_Pose3_RP(varargin{1},varargin{2}); end @@ -66,6 +78,12 @@ struct Constructor { void generate_construct(FileWriter& file, const std::string& cppClassName, std::vector& args_list) const; + friend std::ostream& operator<<(std::ostream& os, const Constructor& m) { + for (size_t i = 0; i < m.nrOverloads(); i++) + os << m.name << m.argLists_[i]; + return os; + } + }; } // \namespace wrap diff --git a/wrap/Module.cpp b/wrap/Module.cpp index f75e1d683..2fc8f92bc 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -217,7 +217,7 @@ void Module::parseMarkup(const std::string& data) { Constructor constructor0(verbose), constructor(verbose); Rule constructor_p = (className_p >> '(' >> argumentList_p >> ')' >> ';' >> !comments_p) - [push_back_a(constructor.args_list, args)] + [bl::bind(&Constructor::addOverload, bl::var(constructor), bl::var(args))] [clear_a(args)]; vector namespaces_return; /// namespace for current return type diff --git a/wrap/StaticMethod.h b/wrap/StaticMethod.h index cab440ef1..524dbb3ae 100644 --- a/wrap/StaticMethod.h +++ b/wrap/StaticMethod.h @@ -43,6 +43,12 @@ struct StaticMethod: public Function, public SignatureOverloads { Str wrapperName, const TypeAttributesTable& typeAttributes, std::vector& functionNames) const; + friend std::ostream& operator<<(std::ostream& os, const StaticMethod& m) { + for (size_t i = 0; i < m.nrOverloads(); i++) + os << "static " << m.returnVals_[i] << " " << m.name_ << m.argLists_[i]; + return os; + } + protected: virtual void proxy_header(FileWriter& proxyFile) const; diff --git a/wrap/tests/testWrap.cpp b/wrap/tests/testWrap.cpp index 02e618668..f26244772 100644 --- a/wrap/tests/testWrap.cpp +++ b/wrap/tests/testWrap.cpp @@ -184,7 +184,7 @@ TEST( wrap, Geometry ) { Class cls = module.classes.at(0); EXPECT(assert_equal("Point2", cls.name)); - EXPECT_LONGS_EQUAL(2, cls.constructor.args_list.size()); + EXPECT_LONGS_EQUAL(2, cls.constructor.nrOverloads()); EXPECT_LONGS_EQUAL(7, cls.nrMethods()); { @@ -229,13 +229,13 @@ TEST( wrap, Geometry ) { { Class cls = module.classes.at(1); EXPECT(assert_equal("Point3", cls.name)); - EXPECT_LONGS_EQUAL(1, cls.constructor.args_list.size()); + EXPECT_LONGS_EQUAL(1, cls.constructor.nrOverloads()); EXPECT_LONGS_EQUAL(1, cls.nrMethods()); EXPECT_LONGS_EQUAL(2, cls.static_methods.size()); EXPECT_LONGS_EQUAL(1, cls.namespaces.size()); // first constructor takes 3 doubles - ArgumentList c1 = cls.constructor.args_list.front(); + ArgumentList c1 = cls.constructor.argumentList(0); EXPECT_LONGS_EQUAL(3, c1.size()); // check first double argument @@ -266,7 +266,7 @@ TEST( wrap, Geometry ) { // Test class is the third one { Class testCls = module.classes.at(2); - EXPECT_LONGS_EQUAL( 2, testCls.constructor.args_list.size()); + EXPECT_LONGS_EQUAL( 2, testCls.constructor.nrOverloads()); EXPECT_LONGS_EQUAL(19, testCls.nrMethods()); EXPECT_LONGS_EQUAL( 0, testCls.static_methods.size()); EXPECT_LONGS_EQUAL( 0, testCls.namespaces.size());