pyx wrapper for static methods

release/4.3a0
Duy-Nguyen Ta 2016-09-09 16:39:47 -04:00
parent d65d87072b
commit 56c0d2a65e
4 changed files with 33 additions and 8 deletions

View File

@ -736,7 +736,6 @@ void Class::emit_cython_pyx(FileWriter& pyxFile, const std::vector<Class>& allCl
"\t\tself." << pyxCythonObj() << " = "
<< pyxSharedCythonClass() << "(new " << pyxCythonClass() << "())\n";
pyxInitParentObj(pyxFile, "\t\tself", "self." + pyxCythonObj(), allClasses);
pyxFile.oss << "\n";
pyxFile.oss << "\t@staticmethod\n";
pyxFile.oss << "\tcdef " << pythonClassName() << " cyCreate(" << pyxSharedCythonClass() << " other):\n"
@ -744,13 +743,14 @@ void Class::emit_cython_pyx(FileWriter& pyxFile, const std::vector<Class>& allCl
<< "\t\tret." << pyxCythonObj() << " = other\n";
pyxInitParentObj(pyxFile, "\t\tret", "other", allClasses);
pyxFile.oss << "\t\treturn ret" << "\n";
pyxFile.oss << "\n";
constructor.emit_cython_pyx(pyxFile, *this);
// if (constructor.nrOverloads()>0) pyxFile.oss << "\n";
if (constructor.nrOverloads()>0) pyxFile.oss << "\n";
// for(const StaticMethod& m: static_methods | boost::adaptors::map_values)
// m.emit_cython_pxd(pyxFile);
// if (static_methods.size()>0) pyxFile.oss << "\n";
for(const StaticMethod& m: static_methods | boost::adaptors::map_values)
m.emit_cython_pyx(pyxFile, *this);
if (static_methods.size()>0) pyxFile.oss << "\n";
// for(const Method& m: nontemplateMethods_ | boost::adaptors::map_values)
// m.emit_cython_pxd(pyxFile);

View File

@ -48,6 +48,10 @@ struct ReturnValue {
isPair = false;
}
bool isVoid() const {
return !isPair && !type1.isPtr && (type1.name() == "void");
}
bool operator==(const ReturnValue& other) const {
return isPair == other.isPair && type1 == other.type1
&& type2 == other.type2;

View File

@ -18,6 +18,7 @@
#include "StaticMethod.h"
#include "utilities.h"
#include "Class.h"
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
@ -59,15 +60,31 @@ string StaticMethod::wrapper_call(FileWriter& wrapperFile, Str cppClassName,
void StaticMethod::emit_cython_pxd(FileWriter& file) const {
// don't support overloads for static method :-(
for(size_t i = 0; i < nrOverloads(); ++i) {
if (i>0) file.oss << "# ";
file.oss << "\t\t@staticmethod\n";
if (i>0) file.oss << "# ";
file.oss << "\t\t";
returnVals_[i].emit_cython_pxd(file);
file.oss << name_ << "(";
file.oss << name_ << ((i>0)?to_string(i):"") << "(";
argumentList(i).emit_cython_pxd(file);
file.oss << ")\n";
}
}
/* ************************************************************************* */
void StaticMethod::emit_cython_pyx(FileWriter& file, const Class& cls) const {
// don't support overloads for static method :-(
for(size_t i = 0; i < nrOverloads(); ++i) {
file.oss << "\t@staticmethod\n";
file.oss << "\tdef " << name_ << "(";
argumentList(i).emit_cython_pyx(file);
file.oss << "):\n";
file.oss << "\t\t";
if (!returnVals_[i].isVoid()) file.oss << "return ";
file.oss << cls.pythonClassName() << ".cyCreate("
<< cls.pyxCythonClass() << "." << name_
<< "(";
argumentList(i).emit_cython_pyx_asParams(file);
file.oss << "))\n";
}
}
/* ************************************************************************* */

View File

@ -23,6 +23,9 @@
namespace wrap {
// Forward declaration
class Class;
/// StaticMethod class
struct StaticMethod: public MethodBase {
@ -35,6 +38,7 @@ struct StaticMethod: public MethodBase {
}
void emit_cython_pxd(FileWriter& file) const;
void emit_cython_pyx(FileWriter& file, const Class& cls) const;
protected: