Re-wrote constructor overloading logic which saves a lot of overhead

release/4.3a0
dellaert 2017-08-06 14:58:23 -07:00
parent e02b504575
commit d752c9e249
4 changed files with 23 additions and 38 deletions

View File

@ -841,29 +841,19 @@ void Class::emit_cython_pyx(FileWriter& file, const std::vector<Class>& allClass
if (parentClass) file.oss << "(" << parentClass->pyxClassName() << ")";
file.oss << ":\n";
// shared variable of the corresponding cython object
// file.oss << " cdef " << shared_pxd_class_in_pyx() << " " << shared_pxd_obj_in_pyx() << "\n";
// __cinit___
file.oss << " def __init__(self, *args, **kwargs):\n"
" self." << shared_pxd_obj_in_pyx() << " = "
<< shared_pxd_class_in_pyx() << "()\n";
// __init___
file.oss << " def __init__(self, *args, **kwargs):\n";
file.oss << " cdef list __params\n";
file.oss << " self." << shared_pxd_obj_in_pyx() << " = " << shared_pxd_class_in_pyx() << "()\n";
file.oss << " if len(args)==0 and len(kwargs)==1 and kwargs.has_key('cyCreateFromShared'):\n return\n";
for (size_t i = 0; i<constructor.nrOverloads(); ++i) {
file.oss << " " << "elif" << " self."
<< pyxClassName() << "_" << i
<< "(args, kwargs):\n pass\n";
}
file.oss << " else:\n raise TypeError('" << pyxClassName()
<< " construction failed!')\n";
pyxInitParentObj(file, " self", "self." + shared_pxd_obj_in_pyx(), allClasses);
file.oss << "\n";
// Constructors
constructor.emit_cython_pyx(file, *this);
if (constructor.nrOverloads()>0) file.oss << "\n";
file.oss << " if (self." << shared_pxd_obj_in_pyx() << ".use_count()==0):\n";
file.oss << " raise TypeError('" << pyxClassName()
<< " construction failed!')\n";
pyxInitParentObj(file, " self", "self." + shared_pxd_obj_in_pyx(), allClasses);
file.oss << "\n";
// cyCreateFromShared
file.oss << " @staticmethod\n";

View File

@ -129,37 +129,31 @@ bool Constructor::hasDefaultConstructor() const {
}
/* ************************************************************************* */
void Constructor::emit_cython_pxd(FileWriter& pxdFile, const Class& cls) const {
void Constructor::emit_cython_pxd(FileWriter& file, const Class& cls) const {
for (size_t i = 0; i < nrOverloads(); i++) {
ArgumentList args = argumentList(i);
// generate the constructor
pxdFile.oss << " " << cls.pxdClassName() << "(";
args.emit_cython_pxd(pxdFile, cls.pxdClassName(), cls.templateArgs);
pxdFile.oss << ") "
<< "except +\n";
file.oss << " " << cls.pxdClassName() << "(";
args.emit_cython_pxd(file, cls.pxdClassName(), cls.templateArgs);
file.oss << ") " << "except +\n";
}
}
/* ************************************************************************* */
void Constructor::emit_cython_pyx(FileWriter& pyxFile, const Class& cls) const {
void Constructor::emit_cython_pyx(FileWriter& file, const Class& cls) const {
for (size_t i = 0; i < nrOverloads(); i++) {
ArgumentList args = argumentList(i);
pyxFile.oss
<< " def " + cls.pyxClassName() + "_" + to_string(i)
+ "(self, args, kwargs):\n";
pyxFile.oss << " cdef list __params\n";
pyxFile.oss << " try:\n";
pyxFile.oss << pyx_resolveOverloadParams(args, true, 3);
pyxFile.oss
file.oss << " try:\n";
file.oss << pyx_resolveOverloadParams(args, true, 3);
file.oss
<< argumentList(i).pyx_convertEigenTypeAndStorageOrder(" ");
pyxFile.oss << " self." << cls.shared_pxd_obj_in_pyx() << " = "
file.oss << " self." << cls.shared_pxd_obj_in_pyx() << " = "
<< cls.shared_pxd_class_in_pyx() << "(new " << cls.pxd_class_in_pyx()
<< "(" << args.pyx_asParams() << "))\n";
pyxFile.oss << " return True\n";
pyxFile.oss << " except:\n";
pyxFile.oss << " return False\n\n";
file.oss << " except:\n";
file.oss << " pass\n";
}
}

View File

@ -348,9 +348,10 @@ void Module::emit_cython_pxd(FileWriter& pxdFile) const {
" shared_ptr()\n"
" shared_ptr(T*)\n"
" T* get()\n"
" long use_count() const\n"
" T& operator*()\n\n"
" cdef shared_ptr[T] dynamic_pointer_cast[T,U](const shared_ptr[U]& r)\n"
" cdef shared_ptr[T] make_shared[T](const T& r)\n";
" cdef shared_ptr[T] make_shared[T](const T& r)\n\n";
for(const TypedefPair& types: typedefs)
types.emit_cython_pxd(pxdFile);

View File

@ -237,7 +237,7 @@ public:
/// the internal Cython shared obj in a Python class wrappper
std::string shared_pxd_obj_in_pyx() const {
return "shared_" + pxdClassName() + "_";
return pxdClassName() + "_";
}
std::string make_shared_pxd_class_in_pyx() const {