emit template class to Cython pxd with test

Cython allows template class.
release/4.3a0
Duy-Nguyen Ta 2016-09-09 07:28:13 -04:00
parent 40da298f68
commit b9880d4257
3 changed files with 28 additions and 10 deletions

View File

@ -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<Class>& classes, const Class& cls,
const Template& t) {
static void handle_possible_template(vector<Class>& classes,
vector<Class>& 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)

View File

@ -38,6 +38,7 @@ struct Module {
std::string name; ///< module name
bool verbose; ///< verbose flag
std::vector<Class> classes; ///< list of classes
std::vector<Class> uninstantiatedClasses; ///< list of template classes after instantiated
std::vector<TemplateInstantiationTypedef> templateInstantiationTypedefs; ///< list of template instantiations
std::vector<ForwardDeclaration> forward_declarations;
std::vector<std::string> includes; ///< Include statements

View File

@ -320,6 +320,16 @@ virtual class BetweenFactor : gtsam::NoiseModelFactor {
void serialize() const;
};
#include <gtsam/sam/BearingFactor.h>
template<POSE, POINT, BEARING>
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<gtsam::Pose2, gtsam::Point2, gtsam::Rot2> BearingFactor2D;
}