Ver código fonte

support explicit default constructors for linmath

David Rose 18 anos atrás
pai
commit
4cd6d03cb5

+ 6 - 1
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) {
   } else if (_type == T_constructor) {
     // A special case for constructors.
     // 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) {
     if (_void_return) {
       nout << "Error, constructor for " << *_cpptype << " returning void.\n";
       nout << "Error, constructor for " << *_cpptype << " returning void.\n";
       return_expr = "";
       return_expr = "";

+ 39 - 0
dtool/src/interrogate/interrogateBuilder.cxx

@@ -182,6 +182,28 @@ do_command(const string &command, const string &params) {
       _ignoretype.insert(type->get_local_name(&parser));
       _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") {
   } else if (command == "ignoreinvolved") {
     _ignoreinvolved.insert(params);
     _ignoreinvolved.insert(params);
 
 
@@ -783,6 +805,23 @@ in_ignoretype(const string &name) const {
   return (_ignoretype.count(name) != 0);
   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
 //     Function: InterrogateBuilder::in_ignoreinvolved
 //       Access: Private
 //       Access: Private

+ 2 - 0
dtool/src/interrogate/interrogateBuilder.h

@@ -80,6 +80,7 @@ public:
   bool in_forcetype(const string &name) const;
   bool in_forcetype(const string &name) const;
   string in_renametype(const string &name) const;
   string in_renametype(const string &name) const;
   bool in_ignoretype(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(const string &name) const;
   bool in_ignoreinvolved(CPPType *type) const;
   bool in_ignoreinvolved(CPPType *type) const;
   bool in_ignorefile(const string &name) const;
   bool in_ignorefile(const string &name) const;
@@ -141,6 +142,7 @@ public:
   Commands _forcetype;
   Commands _forcetype;
   CommandParams _renametype;
   CommandParams _renametype;
   Commands _ignoretype;
   Commands _ignoretype;
+  CommandParams _defconstruct;
   Commands _ignoreinvolved;
   Commands _ignoreinvolved;
   Commands _ignorefile;
   Commands _ignorefile;
   Commands _ignoremember;
   Commands _ignoremember;