diff --git a/wrap/Constructor.cpp b/wrap/Constructor.cpp index 169f07f8f..545bc406a 100644 --- a/wrap/Constructor.cpp +++ b/wrap/Constructor.cpp @@ -139,10 +139,15 @@ void Constructor::emit_cython_pxd(FileWriter& pxdFile, Str className) const { for (size_t i = 0; i < nrOverloads(); i++) { ArgumentList args = argumentList(i); - // ignore copy constructor, it's generated by default above + // ignore copy constructor, it's generated above by default if (args.size() == 1 && args[0].is_const && args[0].is_ref && - !args[0].is_ptr && args[0].type.cythonClass() == className) - continue; + !args[0].is_ptr) { + cout << args[0].type.cythonClass() << " vs " << className << endl; + if (args[0].type.cythonClass() == className || + args[0].type.cythonClass() == "This") + continue; + } + // generate the constructor pxdFile.oss << "\t\t" << className << "("; args.emit_cython_pxd(pxdFile, className); diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 6976c9c89..c4894c8ce 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -64,6 +64,13 @@ static void handle_possible_template(vector& classes, } } +static void push_typedef_pair(vector& typedefs, + const Qualified& oldType, + const Qualified& newType, + const string& includeFile) { + typedefs.push_back(TypedefPair(oldType, newType, includeFile)); +} + /* ************************************************************************* */ Module::Module(const std::string& moduleName, bool enable_verbose) : name(moduleName), verbose(enable_verbose) @@ -130,7 +137,17 @@ void Module::parseMarkup(const std::string& data) { [assign_a(singleInstantiation.namespaces_, namespaces)] [push_back_a(templateInstantiationTypedefs, singleInstantiation)] [assign_a(singleInstantiation, singleInstantiation0)]; - + + Qualified oldType, newType; + TypeGrammar typedefOldClass_g(oldType), typedefNewClass_g(newType); + Rule typedef_p = + (str_p("typedef") >> typedefOldClass_g >> typedefNewClass_g >> + ';') + [assign_a(oldType.namespaces_, namespaces)] + [assign_a(newType.namespaces_, namespaces)] + [bl::bind(&push_typedef_pair, bl::var(typedefs), bl::var(oldType), + bl::var(newType), bl::var(currentInclude))]; + // Create grammar for global functions GlobalFunctionGrammar global_function_g(global_functions,namespaces); @@ -148,7 +165,7 @@ void Module::parseMarkup(const std::string& data) { (str_p("namespace") >> basic.namespace_p[push_back_a(namespaces)] >> ch_p('{') - >> *(include_p | class_p | templateSingleInstantiation_p | global_function_g | namespace_def_p | basic.comments_p) + >> *(include_p | class_p | templateSingleInstantiation_p | typedef_p | global_function_g | namespace_def_p | basic.comments_p) >> ch_p('}')) [pop_a(namespaces)]; @@ -323,11 +340,15 @@ void Module::emit_cython_pxd(FileWriter& pxdFile) const { "\t\tT* get()\n" "\t\tT& operator*()\n\n"; + for(const TypedefPair& types: typedefs) + types.emit_cython_pxd(pxdFile); + //... wrap all classes for(const Class& cls: uninstantiatedClasses) cls.emit_cython_pxd(pxdFile, uninstantiatedClasses); + //... ctypedef for template instantiations - // TODO: put them in the correct place! + // TODO: put them in the correct place!!! for(const Class& cls: expandedClasses) { if (cls.templateClass) { pxdFile.oss << "ctypedef " << cls.templateClass->cythonClass() << "["; diff --git a/wrap/Module.h b/wrap/Module.h index 64ce5dea1..986340946 100644 --- a/wrap/Module.h +++ b/wrap/Module.h @@ -22,6 +22,7 @@ #include "GlobalFunction.h" #include "TemplateInstantiationTypedef.h" #include "ForwardDeclaration.h" +#include "TypedefPair.h" #include #include @@ -43,6 +44,7 @@ struct Module { std::vector forward_declarations; std::vector includes; ///< Include statements GlobalFunctions global_functions; + std::vector typedefs; // After parsing: std::vector expandedClasses; diff --git a/wrap/TypedefPair.h b/wrap/TypedefPair.h new file mode 100644 index 000000000..4f36464a0 --- /dev/null +++ b/wrap/TypedefPair.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Qualified.h" + +namespace wrap { +struct TypedefPair { + Qualified oldType, newType; + std::string includeFile; + + TypedefPair() {} + TypedefPair(const Qualified& oldType, const Qualified& newType, + const std::string& includeFile) + : oldType(oldType), newType(newType), includeFile(includeFile) {} + + void emit_cython_pxd(FileWriter& file) const { + file.oss << "cdef extern from \"" << includeFile << "\" namespace \"" + << oldType.qualifiedNamespaces("::") << "\":\n"; + file.oss << "\tctypedef " << oldType.cythonClass() << " " + << newType.cythonClass() << "\n"; + } +}; +} \ No newline at end of file diff --git a/wrap/tests/cythontest.h b/wrap/tests/cythontest.h index 9181d9e6c..d0b718eb9 100644 --- a/wrap/tests/cythontest.h +++ b/wrap/tests/cythontest.h @@ -1,16 +1,23 @@ namespace gtsam { +#include +typedef size_t Key; + #include -template class FastVector{}; -typedef gtsam::FastVector KeyVector; +template class FastVector { + FastVector(); + FastVector(const This& f); +}; + +typedef gtsam::FastVector KeyVector; #include template class FastList{}; -typedef gtsam::FastList KeyList; +typedef gtsam::FastList KeyList; #include template class FastSet{}; -typedef gtsam::FastSet KeySet; +typedef gtsam::FastSet KeySet; #include template class FastMap{}; @@ -687,6 +694,7 @@ template class PinholeCamera { // Standard Constructors and Named Constructors PinholeCamera(); + PinholeCamera(const This& cam); PinholeCamera(const gtsam::Pose3& pose); PinholeCamera(const gtsam::Pose3& pose, const CALIBRATION& K); static This Level(const CALIBRATION& K, const gtsam::Pose2& pose, double height); @@ -843,13 +851,13 @@ virtual class SymbolicFactor { SymbolicFactor(size_t j1, size_t j2, size_t j3, size_t j4); SymbolicFactor(size_t j1, size_t j2, size_t j3, size_t j4, size_t j5); SymbolicFactor(size_t j1, size_t j2, size_t j3, size_t j4, size_t j5, size_t j6); - // static gtsam::SymbolicFactor FromKeys(const gtsam::KeyVector& js); + static gtsam::SymbolicFactor FromKeys(const gtsam::KeyVector& js); // From Factor size_t size() const; void print(string s) const; bool equals(const gtsam::SymbolicFactor& other, double tol) const; - // gtsam::KeyVector keys(); + gtsam::KeyVector keys(); }; #include @@ -884,19 +892,19 @@ virtual class SymbolicFactorGraph { gtsam::SymbolicBayesTree* eliminateMultifrontal(const gtsam::Ordering& ordering); pair eliminatePartialSequential( const gtsam::Ordering& ordering); - // pair eliminatePartialSequential( - // const gtsam::KeyVector& keys); + pair eliminatePartialSequential( + const gtsam::KeyVector& keys); pair eliminatePartialMultifrontal( const gtsam::Ordering& ordering); - // pair eliminatePartialMultifrontal( - // const gtsam::KeyVector& keys); + pair eliminatePartialMultifrontal( + const gtsam::KeyVector& keys); gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::Ordering& variables); - // gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::KeyVector& variables); + gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::KeyVector& variables); gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::Ordering& variables, const gtsam::Ordering& marginalizedVariableOrdering); - // gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::KeyVector& variables, - // const gtsam::Ordering& marginalizedVariableOrdering); - // gtsam::SymbolicFactorGraph* marginal(const gtsam::KeyVector& variables); + gtsam::SymbolicBayesNet* marginalMultifrontalBayesNet(const gtsam::KeyVector& variables, + const gtsam::Ordering& marginalizedVariableOrdering); + gtsam::SymbolicFactorGraph* marginal(const gtsam::KeyVector& variables); }; #include @@ -908,7 +916,7 @@ virtual class SymbolicConditional : gtsam::SymbolicFactor { SymbolicConditional(size_t key, size_t parent); SymbolicConditional(size_t key, size_t parent1, size_t parent2); SymbolicConditional(size_t key, size_t parent1, size_t parent2, size_t parent3); - // static gtsam::SymbolicConditional FromKeys(const gtsam::KeyVector& keys, size_t nrFrontals); + static gtsam::SymbolicConditional FromKeys(const gtsam::KeyVector& keys, size_t nrFrontals); // Testable void print(string s) const;