ctypedefs for all instantiated classes

release/4.3a0
Duy-Nguyen Ta 2016-09-09 21:50:55 -04:00
parent 10f510119a
commit d719b9b7ae
4 changed files with 32 additions and 12 deletions

View File

@ -309,6 +309,8 @@ vector<Class> 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<Class>& 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<Class>& allClasses) const {
pyxFile.oss << "cdef class " << pythonClassName();
if (parentClass) pyxFile.oss << "(" << parentClass->pythonClassName() << ")";

View File

@ -61,8 +61,8 @@ private:
boost::optional<Qualified> 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<std::string> 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<Qualified> templateInstTypeList; ///< the original typelist used to instantiate this class from a template.
///< Empty if it's not an instantiation
boost::optional<Qualified> 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

View File

@ -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<cls.templateInstTypeList.size(); ++i)
pxdFile.oss << cls.templateInstTypeList[i].cythonClassName()
<< ((i==cls.templateInstTypeList.size()-1)?"":", ");
pxdFile.oss << "] " << cls.cythonClassName() << "\n";
}
}
// finish pxd wrapper file
pxdFile.oss << "\n";
pxdFile.emit(true);
// create cython pyx file
/// Create cython pyx file
string pyxFileName = toolboxPath + "/" + name + ".pyx";
FileWriter pyxFile(pyxFileName, verbose, "#");
for(const Class& cls: expandedClasses)
cls.emit_cython_pyx(pyxFile, expandedClasses);
pyxFile.oss << "\n";
@ -361,11 +370,11 @@ vector<Class> Module::ExpandTypedefInstantiations(const vector<Class>& 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;
}

View File

@ -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;
}