diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 6326ca2dd..2397899ab 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -309,6 +309,8 @@ vector Class::expandTemplate(Str templateArg, inst.templateArgs.clear(); inst.typedefName = qualifiedName("::") + "<" + instName.qualifiedName("::") + ">"; + inst.templateInstTypeList.push_back(instName); + inst.templateClass = *this; result.push_back(inst); } return result; @@ -702,9 +704,11 @@ void Class::emit_cython_pxd(FileWriter& pxdFile) const { methods_.size() + templateMethods_.size(); if (numMethods == 0) pxdFile.oss << "\t\tpass"; - pxdFile.oss << "\n\n"; + + pxdFile.oss << "\n\n"; } +/* ************************************************************************* */ void Class::pyxInitParentObj(FileWriter& pyxFile, const std::string& pyObj, const std::string& cySharedObj, const std::vector& allClasses) const { if (parentClass) { pyxFile.oss << pyObj << "." << parentClass->pyxCythonObj() << " = " @@ -725,6 +729,7 @@ void Class::pyxInitParentObj(FileWriter& pyxFile, const std::string& pyObj, cons } } +/* ************************************************************************* */ void Class::emit_cython_pyx(FileWriter& pyxFile, const std::vector& allClasses) const { pyxFile.oss << "cdef class " << pythonClassName(); if (parentClass) pyxFile.oss << "(" << parentClass->pythonClassName() << ")"; diff --git a/wrap/Class.h b/wrap/Class.h index 63f6389ba..46376e377 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -61,8 +61,8 @@ private: boost::optional parentClass; ///< The *single* parent Methods methods_; ///< Class methods, including all expanded/instantiated template methods - Methods nontemplateMethods_; - TemplateMethods templateMethods_; + Methods nontemplateMethods_; ///< only nontemplate methods + TemplateMethods templateMethods_; ///< only template methods // Method& mutableMethod(Str key); public: @@ -72,6 +72,10 @@ public: // Then the instance variables are set directly by the Module constructor std::vector templateArgs; ///< Template arguments std::string typedefName; ///< The name to typedef *from*, if this class is actually a typedef, i.e. typedef [typedefName] [name] + std::vector templateInstTypeList; ///< the original typelist used to instantiate this class from a template. + ///< Empty if it's not an instantiation + boost::optional templateClass = boost::none; ///< qualified name of the original template class from which this class was instantiated. + ///< boost::none if not an instantiation bool isVirtual; ///< Whether the class is part of a virtual inheritance chain bool isSerializable; ///< Whether we can use boost.serialization to serialize the class - creates exports bool hasSerialization; ///< Whether we should create the serialization functions diff --git a/wrap/Module.cpp b/wrap/Module.cpp index 118cee9d2..3906b40a8 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -288,20 +288,29 @@ void Module::cython_code(const string& toolboxPath) const { fs::create_directories(toolboxPath); - // create the unified .cpp switch file + /// Create cython pxd file string pxdFileName = toolboxPath + "/" + name + "_wrapper" + ".pxd"; - string pyxFileName = toolboxPath + "/" + name + ".pyx"; FileWriter pxdFile(pxdFileName, verbose, "#"); - FileWriter pyxFile(pyxFileName, verbose, "#"); - - // create cython pxd file + //... wrap all classes for(const Class& cls: uninstantiatedClasses) cls.emit_cython_pxd(pxdFile); - // finish wrapper file + //... ctypedef for template instantiations + for(const Class& cls: expandedClasses) { + if (cls.templateClass) { + pxdFile.oss << "ctypedef " << cls.templateClass->cythonClassName() << "["; + for (size_t i = 0; i Module::ExpandTypedefInstantiations(const vector& classes, // Remove all template classes for(size_t i = 0; i < expandedClasses.size(); ++i) - if(!expandedClasses[size_t(i)].templateArgs.empty()) { + if(!expandedClasses[i].templateArgs.empty()) { expandedClasses.erase(expandedClasses.begin() + size_t(i)); -- i; } - + return expandedClasses; } diff --git a/wrap/TemplateInstantiationTypedef.cpp b/wrap/TemplateInstantiationTypedef.cpp index 4c77d4e76..6aa35a5bd 100644 --- a/wrap/TemplateInstantiationTypedef.cpp +++ b/wrap/TemplateInstantiationTypedef.cpp @@ -60,6 +60,8 @@ Class TemplateInstantiationTypedef::findAndExpand( for (size_t i = 1; i < typeList.size(); ++i) classInst.typedefName += (", " + typeList[i].qualifiedName("::")); classInst.typedefName += ">"; + classInst.templateClass = *matchedClass; + classInst.templateInstTypeList = typeList; return classInst; }