From 4cd6d03cb5033d9143fed9cc7239f9da2fb7a6e9 Mon Sep 17 00:00:00 2001 From: David Rose Date: Tue, 25 Mar 2008 23:53:27 +0000 Subject: [PATCH] support explicit default constructors for linmath --- dtool/src/interrogate/functionRemap.cxx | 7 +++- dtool/src/interrogate/interrogateBuilder.cxx | 39 ++++++++++++++++++++ dtool/src/interrogate/interrogateBuilder.h | 2 + 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/dtool/src/interrogate/functionRemap.cxx b/dtool/src/interrogate/functionRemap.cxx index 4469b38806..97803712e3 100644 --- a/dtool/src/interrogate/functionRemap.cxx +++ b/dtool/src/interrogate/functionRemap.cxx @@ -158,7 +158,12 @@ string FunctionRemap::call_function(ostream &out, int indent_level, bool convert } else if (_type == T_constructor) { // A special case for constructors. - return_expr = "new " + get_call_str(container, pexprs); + string defconstruct = builder.in_defconstruct(_cpptype->get_local_name(&parser)); + if (pexprs.empty() && !defconstruct.empty()) { + return_expr = defconstruct; + } else { + return_expr = "new " + get_call_str(container, pexprs); + } if (_void_return) { nout << "Error, constructor for " << *_cpptype << " returning void.\n"; return_expr = ""; diff --git a/dtool/src/interrogate/interrogateBuilder.cxx b/dtool/src/interrogate/interrogateBuilder.cxx index 1886eef974..9b7f9a6061 100644 --- a/dtool/src/interrogate/interrogateBuilder.cxx +++ b/dtool/src/interrogate/interrogateBuilder.cxx @@ -182,6 +182,28 @@ do_command(const string &command, const string ¶ms) { _ignoretype.insert(type->get_local_name(&parser)); } + } else if (command == "defconstruct") { + // defining the parameters that are implicitly supplied to the + // generated default constructor. Especially useful for linmath + // objects, whose default constructor in C++ is uninitialized, but + // whose Python-level constructor should initialize to 0. + + size_t space = params.find(' '); + if (space == string::npos) { + nout << "No constructor specified for defconstruct " << params << "\n"; + } else { + string class_name = params.substr(0, space); + string constructor = params.substr(space + 1); + + CPPType *type = parser.parse_type(class_name); + if (type == (CPPType *)NULL) { + nout << "Unknown type: defconstruct " << class_name << "\n"; + } else { + type = type->resolve_type(&parser, &parser); + _defconstruct[type->get_local_name(&parser)] = constructor; + } + } + } else if (command == "ignoreinvolved") { _ignoreinvolved.insert(params); @@ -783,6 +805,23 @@ in_ignoretype(const string &name) const { return (_ignoretype.count(name) != 0); } +//////////////////////////////////////////////////////////////////// +// Function: InterrogateBuilder::in_defconstruct +// Access: Private +// Description: If the user requested an explicit default constructor +// for this type via the defconstruct command, returns +// that string; otherwise, returns the empty string. +//////////////////////////////////////////////////////////////////// +string InterrogateBuilder:: +in_defconstruct(const string &name) const { + CommandParams::const_iterator pi; + pi = _defconstruct.find(name); + if (pi != _defconstruct.end()) { + return (*pi).second; + } + return string(); +} + //////////////////////////////////////////////////////////////////// // Function: InterrogateBuilder::in_ignoreinvolved // Access: Private diff --git a/dtool/src/interrogate/interrogateBuilder.h b/dtool/src/interrogate/interrogateBuilder.h index ee1b342bd4..42878fbf6b 100644 --- a/dtool/src/interrogate/interrogateBuilder.h +++ b/dtool/src/interrogate/interrogateBuilder.h @@ -80,6 +80,7 @@ public: bool in_forcetype(const string &name) const; string in_renametype(const string &name) const; bool in_ignoretype(const string &name) const; + string in_defconstruct(const string &name) const; bool in_ignoreinvolved(const string &name) const; bool in_ignoreinvolved(CPPType *type) const; bool in_ignorefile(const string &name) const; @@ -141,6 +142,7 @@ public: Commands _forcetype; CommandParams _renametype; Commands _ignoretype; + CommandParams _defconstruct; Commands _ignoreinvolved; Commands _ignorefile; Commands _ignoremember;