From b9880d4257ececaa7fbed9e01cf61f54acf3c80d Mon Sep 17 00:00:00 2001 From: Duy-Nguyen Ta Date: Fri, 9 Sep 2016 07:28:13 -0400 Subject: [PATCH] emit template class to Cython pxd with test Cython allows template class. --- wrap/Module.cpp | 27 +++++++++++++++++---------- wrap/Module.h | 1 + wrap/tests/cythontest.h | 10 ++++++++++ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 70e8176b7..1036ee8ca 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -45,8 +45,10 @@ namespace fs = boost::filesystem; /* ************************************************************************* */ // If a number of template arguments were given, generate a number of expanded // class names, e.g., PriorFactor -> PriorFactorPose2, and add those classes -static void handle_possible_template(vector& classes, const Class& cls, - const Template& t) { +static void handle_possible_template(vector& classes, + vector& uninstantiatedClasses, + const Class& cls, const Template& t) { + uninstantiatedClasses.push_back(cls); if (cls.templateArgs.empty() || t.empty()) { classes.push_back(cls); } else { @@ -106,13 +108,13 @@ void Module::parseMarkup(const std::string& data) { Class cls0(verbose),cls(verbose); Template classTemplate; ClassGrammar class_g(cls,classTemplate); - Rule class_p = class_g // + Rule class_p = class_g // [assign_a(cls.namespaces_, namespaces)] - [assign_a(cls.includeFile, currentInclude)] - [bl::bind(&handle_possible_template, bl::var(classes), bl::var(cls), - bl::var(classTemplate))] - [clear_a(classTemplate)] // - [assign_a(cls,cls0)]; + [assign_a(cls.includeFile, currentInclude)][bl::bind( + &handle_possible_template, bl::var(classes), + bl::var(uninstantiatedClasses), bl::var(cls), + bl::var(classTemplate))][clear_a(classTemplate)] // + [assign_a(cls, cls0)]; // parse "gtsam::Pose2" and add to singleInstantiation.typeList TemplateInstantiationTypedef singleInstantiation, singleInstantiation0; @@ -184,6 +186,9 @@ void Module::parseMarkup(const std::string& data) { for(Class& cls: classes) cls.erase_serialization(); + for(Class& cls: uninstantiatedClasses) + cls.erase_serialization(); + // Explicitly add methods to the classes from parents so it shows in documentation for(Class& cls: classes) cls.appendInheritedMethods(cls, classes); @@ -290,7 +295,7 @@ void Module::cython_code(const string& toolboxPath) const { FileWriter pyxFile(pyxFileName, verbose, "#"); // create proxy class and wrapper code - for(const Class& cls: expandedClasses) + for(const Class& cls: uninstantiatedClasses) cls.cython_wrapper(pxdFile, pyxFile); // finish wrapper file @@ -483,8 +488,10 @@ void Module::python_wrapper(const string& toolboxPath) const { wrapperFile.oss << "{\n"; // write out classes - for(const Class& cls: expandedClasses) + for(const Class& cls: classes) { + cout << "tmpl args:" << cls.templateArgs.size() << endl; cls.python_wrapper(wrapperFile); + } // write out global functions for(const GlobalFunctions::value_type& p: global_functions) diff --git a/wrap/Module.h b/wrap/Module.h index 0a040535e..163860e93 100644 --- a/wrap/Module.h +++ b/wrap/Module.h @@ -38,6 +38,7 @@ struct Module { std::string name; ///< module name bool verbose; ///< verbose flag std::vector classes; ///< list of classes + std::vector uninstantiatedClasses; ///< list of template classes after instantiated std::vector templateInstantiationTypedefs; ///< list of template instantiations std::vector forward_declarations; std::vector includes; ///< Include statements diff --git a/wrap/tests/cythontest.h b/wrap/tests/cythontest.h index 77aa2568c..56835657b 100644 --- a/wrap/tests/cythontest.h +++ b/wrap/tests/cythontest.h @@ -320,6 +320,16 @@ virtual class BetweenFactor : gtsam::NoiseModelFactor { void serialize() const; }; +#include +template +virtual class BearingFactor : gtsam::NoiseModelFactor { + BearingFactor(size_t key1, size_t key2, const BEARING& measured, const gtsam::noiseModel::Base* noiseModel); + + // enabling serialization functionality + void serialize() const; +}; + +typedef gtsam::BearingFactor BearingFactor2D; } \ No newline at end of file