Browse Source

interrogate: fix ability to return ReferenceCount-like classes

Classes with virtual ref(), unref() and get_ref_count() methods, like RecorderBase, could not be returned by PT() from methods because they didn't inherit from ReferenceCount.  However, classes do not need to inherit ReferenceCount to be able to be tracked by a PointerTo, and defining an abstract base class with pure virtual ref()/unref()/get_ref_count() is a way to avoid dual inheritance of ReferenceCount.
rdb 5 years ago
parent
commit
4ef8e5228e
1 changed files with 9 additions and 1 deletions
  1. 9 1
      dtool/src/interrogate/typeManager.cxx

+ 9 - 1
dtool/src/interrogate/typeManager.cxx

@@ -1442,7 +1442,7 @@ is_void(CPPType *type) {
 
 /**
  * Returns true if the indicated type is some class that derives from
- * ReferenceCount, or false otherwise.
+ * ReferenceCount, or defines ref and unref(), or false otherwise.
  */
 bool TypeManager::
 is_reference_count(CPPType *type) {
@@ -1459,6 +1459,14 @@ is_reference_count(CPPType *type) {
   case CPPDeclaration::ST_struct:
     {
       CPPStructType *stype = type->as_struct_type();
+
+      // If we have methods named ref() and unref(), this is good enough.
+      if (stype->_scope->_functions.count("ref") &&
+          stype->_scope->_functions.count("unref") &&
+          stype->_scope->_functions.count("get_ref_count")) {
+        return true;
+      }
+
       CPPStructType::Derivation::const_iterator di;
       for (di = stype->_derivation.begin();
            di != stype->_derivation.end();