From 6068166d2b9cb6a61e60c3e9590b4b73bdcb493d Mon Sep 17 00:00:00 2001 From: dellaert Date: Sun, 6 Aug 2017 12:21:26 -0700 Subject: [PATCH] Moved wrapper class signature to Class.cpp --- wrap/Class.cpp | 13 ++++++++++++ wrap/Class.h | 1 + wrap/Module.cpp | 54 ++++++++++++++++++++----------------------------- 3 files changed, 36 insertions(+), 32 deletions(-) diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 5192ee4d3..ed777b563 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -764,6 +764,19 @@ void Class::emit_cython_pxd(FileWriter& pxdFile) const { if (numMethods == 0) pxdFile.oss << " pass\n"; } +/* ************************************************************************* */ +void Class::emit_cython_wrapper_pxd(FileWriter& pxdFile) const { + pxdFile.oss << "cdef class " << pyxClassName(); + if (getParent()) + pxdFile.oss << "(" << getParent()->pyxClassName() << ")"; + pxdFile.oss << ":\n"; + pxdFile.oss << " cdef " << shared_pxd_class_in_pyx() << " " + << shared_pxd_obj_in_pyx() << "\n"; + // cyCreateFromShared + pxdFile.oss << " @staticmethod\n"; + pxdFile.oss << " cdef " << pyxClassName() << " cyCreateFromShared(const " + << shared_pxd_class_in_pyx() << "& other)\n"; +} /* ************************************************************************* */ void Class::pyxInitParentObj(FileWriter& pyxFile, const std::string& pyObj, diff --git a/wrap/Class.h b/wrap/Class.h index 1a8092228..910ecde57 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -165,6 +165,7 @@ public: // emit cython wrapper void emit_cython_pxd(FileWriter& pxdFile) const; + void emit_cython_wrapper_pxd(FileWriter& pxdFile) const; void emit_cython_pyx(FileWriter& pyxFile, const std::vector& allClasses) const; void pyxInitParentObj(FileWriter& pyxFile, const std::string& pyObj, diff --git a/wrap/Module.cpp b/wrap/Module.cpp index ac822e64b..995d7a52b 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -357,41 +357,31 @@ void Module::emit_cython_pxd(FileWriter& pxdFile) const { //... wrap all classes for (const Class& cls : uninstantiatedClasses) { - cls.emit_cython_pxd(pxdFile); + cls.emit_cython_pxd(pxdFile); - for (const Class& expCls : expandedClasses) { - //... ctypedef for template instantiations - if (expCls.templateClass && - expCls.templateClass->pxdClassName() == cls.pxdClassName()) { - pxdFile.oss << "ctypedef " << expCls.templateClass->pxdClassName() - << "["; - for (size_t i = 0; i < expCls.templateInstTypeList.size(); ++i) - pxdFile.oss << expCls.templateInstTypeList[i].pxdClassName() - << ((i == expCls.templateInstTypeList.size() - 1) - ? "" - : ", "); - pxdFile.oss << "] " << expCls.pxdClassName() << "\n"; - } + for (const Class& expanded : expandedClasses) { + bool matchingNonTemplated = !expanded.templateClass + && expanded.pxdClassName() == cls.pxdClassName(); + bool isTemplatedFromCls = expanded.templateClass + && expanded.templateClass->pxdClassName() == cls.pxdClassName(); - if ((!expCls.templateClass && - expCls.pxdClassName() == cls.pxdClassName()) || - (expCls.templateClass && - expCls.templateClass->pxdClassName() == cls.pxdClassName())) { - pxdFile.oss << "\n"; - pxdFile.oss << "cdef class " << expCls.pyxClassName(); - if (expCls.getParent()) - pxdFile.oss << "(" << expCls.getParent()->pyxClassName() << ")"; - pxdFile.oss << ":\n"; - pxdFile.oss << " cdef " << expCls.shared_pxd_class_in_pyx() - << " " << expCls.shared_pxd_obj_in_pyx() << "\n"; - // cyCreateFromShared - pxdFile.oss << " @staticmethod\n"; - pxdFile.oss << " cdef " << expCls.pyxClassName() - << " cyCreateFromShared(const " - << expCls.shared_pxd_class_in_pyx() << "& other)\n"; - } + // ctypedef for template instantiations + if (isTemplatedFromCls) { + pxdFile.oss << "\n"; + pxdFile.oss << "ctypedef " << expanded.templateClass->pxdClassName() + << "["; + for (size_t i = 0; i < expanded.templateInstTypeList.size(); ++i) + pxdFile.oss << expanded.templateInstTypeList[i].pxdClassName() + << ((i == expanded.templateInstTypeList.size() - 1) ? "" : ", "); + pxdFile.oss << "] " << expanded.pxdClassName() << "\n"; } - pxdFile.oss << "\n\n"; + + // Python wrapper class + if (isTemplatedFromCls || matchingNonTemplated) { + expanded.emit_cython_wrapper_pxd(pxdFile); + } + } + pxdFile.oss << "\n\n"; } //... wrap global functions