From 8944f024012490b69d83c92925a2e36cbd505d64 Mon Sep 17 00:00:00 2001 From: Duy-Nguyen Ta Date: Fri, 9 Sep 2016 22:28:15 -0400 Subject: [PATCH] add headers, small refactor, test FastContainers --- wrap/Module.cpp | 44 ++++++++++++++++++++++++++++------- wrap/Module.h | 4 +++- wrap/tests/cythontest.h | 15 ++++++++++++ wrap/tests/testWrapCython.cpp | 2 +- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 3906b40a8..7c27f9170 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -284,13 +284,35 @@ void Module::matlab_code(const string& toolboxPath) const { } /* ************************************************************************* */ -void Module::cython_code(const string& toolboxPath) const { - +void Module::cython_wrapper(const string& toolboxPath) const { fs::create_directories(toolboxPath); - - /// Create cython pxd file string pxdFileName = toolboxPath + "/" + name + "_wrapper" + ".pxd"; FileWriter pxdFile(pxdFileName, verbose, "#"); + emit_cython_pxd(pxdFile); + string pyxFileName = toolboxPath + "/" + name + ".pyx"; + FileWriter pyxFile(pyxFileName, verbose, "#"); + emit_cython_pyx(pyxFile); +} + +/* ************************************************************************* */ +void Module::emit_cython_pxd(FileWriter& pxdFile) const { + // headers + pxdFile.oss << "from eigency.core cimport *\n" + "from libcpp.string cimport string\n" + "from libcpp.vector cimport vector\n" + "from libcpp.pair cimport pair\n" + "from libcpp.set cimport set\n" + "from libcpp.map cimport map\n" + "from libcpp cimport bool\n\n"; + + // boost shared_ptr + pxdFile.oss << "cdef extern from \"boost/shared_ptr.hpp\" namespace \"boost\":\n" + "\tcppclass shared_ptr[T]:\n" + "\t\tshared_ptr()\n" + "\t\tshared_ptr(T*)\n" + "\t\tT* get()\n" + "\t\tT& operator*()\n\n"; + //... wrap all classes for(const Class& cls: uninstantiatedClasses) cls.emit_cython_pxd(pxdFile); @@ -304,13 +326,19 @@ void Module::cython_code(const string& toolboxPath) const { pxdFile.oss << "] " << cls.cythonClassName() << "\n"; } } - // finish pxd wrapper file pxdFile.oss << "\n"; pxdFile.emit(true); +} - /// Create cython pyx file - string pyxFileName = toolboxPath + "/" + name + ".pyx"; - FileWriter pyxFile(pyxFileName, verbose, "#"); +/* ************************************************************************* */ +void Module::emit_cython_pyx(FileWriter& pyxFile) const { + // headers... + pyxFile.oss << "cimport numpy as np\n" + "cimport gtsam_wrapper as gtsam\n" + "from gtsam_wrapper cimport shared_ptr\n" + "from eigency.core cimport *\n" + "from libcpp.string cimport string\n" + "from cython.operator cimport dereference as deref\n\n\n"; for(const Class& cls: expandedClasses) cls.emit_cython_pyx(pyxFile, expandedClasses); pyxFile.oss << "\n"; diff --git a/wrap/Module.h b/wrap/Module.h index 163860e93..64ce5dea1 100644 --- a/wrap/Module.h +++ b/wrap/Module.h @@ -64,7 +64,9 @@ struct Module { void matlab_code(const std::string& path) const; /// Cython code generation: - void cython_code(const std::string& path) const; + void cython_wrapper(const std::string& path) const; + void emit_cython_pxd(FileWriter& file) const; + void emit_cython_pyx(FileWriter& file) const; void generateIncludes(FileWriter& file) const; diff --git a/wrap/tests/cythontest.h b/wrap/tests/cythontest.h index ee3ee5d13..62597bcd8 100644 --- a/wrap/tests/cythontest.h +++ b/wrap/tests/cythontest.h @@ -1,5 +1,20 @@ namespace gtsam { +#include +template class FastVector{}; +typedef gtsam::FastVector KeyVector; + +#include +template class FastList{}; +typedef gtsam::FastList KeyList; + +#include +template class FastSet{}; +typedef gtsam::FastSet KeySet; + +#include +template class FastMap{}; + #include class Point3 { // Standard Constructors diff --git a/wrap/tests/testWrapCython.cpp b/wrap/tests/testWrapCython.cpp index 2ced4d1ed..37141c4b0 100644 --- a/wrap/tests/testWrapCython.cpp +++ b/wrap/tests/testWrapCython.cpp @@ -51,7 +51,7 @@ TEST( wrap, cython_code_geometry ) { // emit MATLAB code // make_geometry will not compile, use make testwrap to generate real make - module.cython_code("actual-cython"); + module.cython_wrapper("actual-cython"); } /* ************************************************************************* */