diff --git a/wrap/Method.cpp b/wrap/Method.cpp index 183374463..41600395a 100644 --- a/wrap/Method.cpp +++ b/wrap/Method.cpp @@ -95,6 +95,7 @@ void Method::emit_cython_pyx(FileWriter& file, const Class& cls) const { string funcName = ((name_ == "print") ? "_print" : name_); size_t N = nrOverloads(); for(size_t i = 0; i < N; ++i) { + // Function definition file.oss << "\tdef " << funcName; // modify name of function instantiation as python doesn't allow overloads // e.g. template funcName(...) --> funcNameA, funcNameB, funcNameC @@ -109,25 +110,9 @@ void Method::emit_cython_pyx(FileWriter& file, const Class& cls) const { argumentList(i).emit_cython_pyx(file); file.oss << "):\n"; - /// Return part - file.oss << "\t\t"; - if (!returnVals_[i].isVoid()) { - file.oss << "cdef "; - returnVals_[i].emit_cython_pyx_return_type(file); - file.oss << " ret = "; - } - //... function call - file.oss << "self." << cls.pyxCythonObj() << ".get()." << funcName; - if (templateArgValue_) file.oss << "[" << templateArgValue_->pyxCythonClass() << "]"; - file.oss << "("; - argumentList(i).emit_cython_pyx_asParams(file); - file.oss << ")\n"; - - file.oss << "\t\t"; - if (!returnVals_[i].isVoid()) file.oss << "return "; - // ... casting return value - returnVals_[i].emit_cython_pyx_casting(file, "ret"); - file.oss << "\n"; + /// Call cython corresponding function and return + string caller = "self." + cls.pyxCythonObj() + ".get()"; + emit_cython_pyx_function_call(file, "\t\t", caller, funcName, i, cls); } } /* ************************************************************************* */ diff --git a/wrap/Method.h b/wrap/Method.h index da8d78c39..a984a4a5b 100644 --- a/wrap/Method.h +++ b/wrap/Method.h @@ -22,9 +22,6 @@ namespace wrap { -// Forward declaration -class Class; - /// Method class class Method: public MethodBase { diff --git a/wrap/MethodBase.cpp b/wrap/MethodBase.cpp index 84e6c67d9..142c17c2c 100644 --- a/wrap/MethodBase.cpp +++ b/wrap/MethodBase.cpp @@ -17,6 +17,7 @@ **/ #include "Method.h" +#include "Class.h" #include "utilities.h" #include @@ -138,3 +139,32 @@ void MethodBase::python_wrapper(FileWriter& wrapperFile, Str className) const { } /* ************************************************************************* */ +void MethodBase::emit_cython_pyx_function_call(FileWriter& file, + const std::string& indent, + const std::string& caller, + const std::string& funcName, + size_t iOverload, + const Class& cls) const { + file.oss << indent; + if (!returnVals_[iOverload].isVoid()) { + file.oss << "cdef "; + returnVals_[iOverload].emit_cython_pyx_return_type(file); + file.oss << " ret = "; + } + //... function call + file.oss << caller << "." << funcName; + if (templateArgValue_) file.oss << "[" << templateArgValue_->pyxCythonClass() << "]"; + file.oss << "("; + argumentList(iOverload).emit_cython_pyx_asParams(file); + file.oss << ")\n"; + + // ... casting return value + if (!returnVals_[iOverload].isVoid()) { + file.oss << indent; + file.oss << "return "; + returnVals_[iOverload].emit_cython_pyx_casting(file, "ret"); + } + file.oss << "\n"; +} + +/* ************************************************************************* */ diff --git a/wrap/MethodBase.h b/wrap/MethodBase.h index 903b89569..ef3def5fe 100644 --- a/wrap/MethodBase.h +++ b/wrap/MethodBase.h @@ -23,6 +23,9 @@ namespace wrap { +// Forward declaration +class Class; + /// MethodBase class struct MethodBase: public FullyOverloadedFunction { @@ -51,6 +54,14 @@ struct MethodBase: public FullyOverloadedFunction { // emit python wrapper void python_wrapper(FileWriter& wrapperFile, Str className) const; + // emit cython pyx function call + void emit_cython_pyx_function_call(FileWriter& file, + const std::string& indent, + const std::string& caller, + const std::string& funcName, + size_t iOverload, + const Class& cls) const; + protected: virtual void proxy_header(FileWriter& proxyFile) const = 0; diff --git a/wrap/StaticMethod.cpp b/wrap/StaticMethod.cpp index d5531ca2a..87b036cd3 100644 --- a/wrap/StaticMethod.cpp +++ b/wrap/StaticMethod.cpp @@ -75,26 +75,14 @@ void StaticMethod::emit_cython_pxd(FileWriter& file) const { 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) { + string funcName = name_ + ((i>0)? "_" + to_string(i):""); file.oss << "\t@staticmethod\n"; - file.oss << "\tdef " << name_ << ((i > 0) ? "_" + to_string(i) : "") - << "("; + file.oss << "\tdef " << funcName << "("; argumentList(i).emit_cython_pyx(file); file.oss << "):\n"; - file.oss << "\t\t"; - //... function call - if (!returnVals_[i].isVoid()) file.oss << "ret = "; - file.oss << cls.pyxCythonClass() << "." - << name_ << ((i>0)? "_" + to_string(i):""); - if (templateArgValue_) file.oss << "[" << templateArgValue_->pyxCythonClass() << "]"; - file.oss << "("; - argumentList(i).emit_cython_pyx_asParams(file); - file.oss << ")\n"; - //... casting return value - if (!returnVals_[i].isVoid()) { - file.oss << "\t\treturn "; - returnVals_[i].emit_cython_pyx_casting(file, "ret"); - } + /// Call cython corresponding function and return + emit_cython_pyx_function_call(file, "\t\t", cls.pyxCythonClass(), funcName, i, cls); file.oss << "\n"; } } diff --git a/wrap/StaticMethod.h b/wrap/StaticMethod.h index c04408daf..3b24afb08 100644 --- a/wrap/StaticMethod.h +++ b/wrap/StaticMethod.h @@ -23,9 +23,6 @@ namespace wrap { -// Forward declaration -class Class; - /// StaticMethod class struct StaticMethod: public MethodBase {