From 2496de85a9469f4e528c81a487433c51ee75fbbe Mon Sep 17 00:00:00 2001 From: Duy-Nguyen Ta Date: Sat, 10 Sep 2016 19:44:53 -0400 Subject: [PATCH] check if default constructor exists. Autogenerate copy constructor by default --- wrap/Constructor.cpp | 19 +++++++++++++++++++ wrap/Constructor.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/wrap/Constructor.cpp b/wrap/Constructor.cpp index 53fe31e5b..17c708cda 100644 --- a/wrap/Constructor.cpp +++ b/wrap/Constructor.cpp @@ -121,10 +121,29 @@ void Constructor::python_wrapper(FileWriter& wrapperFile, Str className) const { << ");\n"; } +/* ************************************************************************* */ +bool Constructor::hasDefaultConstructor() const { + for (size_t i = 0; i < nrOverloads(); i++) { + if (argumentList(i).size() == 0) return true; + } + return false; +} + /* ************************************************************************* */ void Constructor::emit_cython_pxd(FileWriter& pxdFile, Str className) const { + // if it can ever be constructed, add the default copy constructor by default + if (nrOverloads() > 0) { + pxdFile.oss << "\t\t" << className << "(const " << className + << "&) except +\n"; + } + for (size_t i = 0; i < nrOverloads(); i++) { ArgumentList args = argumentList(i); + // ignore copy constructor, it's generated by default above + if (args.size() == 1 && args[0].is_const && args[0].is_ref && + !args[0].is_ptr && args[0].type.cythonClassName() == className) + continue; + // generate the constructor pxdFile.oss << "\t\t" << className << "("; args.emit_cython_pxd(pxdFile); pxdFile.oss << ") " << "except +\n"; diff --git a/wrap/Constructor.h b/wrap/Constructor.h index aca6abb9d..8a4259a09 100644 --- a/wrap/Constructor.h +++ b/wrap/Constructor.h @@ -45,6 +45,9 @@ struct Constructor: public OverloadedFunction { return inst; } + /// return true if the default constructor exists + bool hasDefaultConstructor() const; + // MATLAB code generation // toolboxPath is main toolbox directory, e.g., ../matlab // classFile is class proxy file, e.g., ../matlab/@Point2/Point2.m