Browse Source

add TypeHandle::make()

David Rose 16 years ago
parent
commit
fc74ddace3
2 changed files with 28 additions and 0 deletions
  1. 20 0
      dtool/src/dtoolbase/typeHandle.cxx
  2. 8 0
      dtool/src/dtoolbase/typeHandle.h

+ 20 - 0
dtool/src/dtoolbase/typeHandle.cxx

@@ -19,6 +19,26 @@
 // This is initialized to zero by static initialization.
 // This is initialized to zero by static initialization.
 TypeHandle TypeHandle::_none;
 TypeHandle TypeHandle::_none;
 
 
+#ifdef HAVE_PYTHON
+////////////////////////////////////////////////////////////////////
+//     Function: TypeHandle::make
+//       Access: Published
+//  Description: This special method allows coercion to a TypeHandle
+//               from a Python class object or instance.  It simply
+//               attempts to call classobj.getClassType(), and returns
+//               that value (or raises an exception if that method
+//               doesn't work).
+//
+//               This method allows a Python class object to be used
+//               anywhere a TypeHandle is expected by the C++
+//               interface.
+////////////////////////////////////////////////////////////////////
+PyObject *TypeHandle::
+make(PyObject *classobj) {
+  return PyObject_CallMethod(classobj, (char *)"getClassType", (char *)"");
+}
+#endif  // HAVE_PYTHON
+
 #ifdef DO_MEMORY_USAGE
 #ifdef DO_MEMORY_USAGE
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypeHandle::get_memory_usage
 //     Function: TypeHandle::get_memory_usage

+ 8 - 0
dtool/src/dtoolbase/typeHandle.h

@@ -65,6 +65,10 @@
 
 
 class TypedObject;
 class TypedObject;
 
 
+#ifdef HAVE_PYTHON
+#include "Python.h"
+#endif
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : TypeHandle
 //       Class : TypeHandle
 // Description : TypeHandle is the identifier used to differentiate
 // Description : TypeHandle is the identifier used to differentiate
@@ -98,6 +102,10 @@ PUBLISHED:
   INLINE TypeHandle();
   INLINE TypeHandle();
   INLINE TypeHandle(const TypeHandle &copy);
   INLINE TypeHandle(const TypeHandle &copy);
 
 
+#ifdef HAVE_PYTHON
+  static PyObject *make(PyObject *classobj);
+#endif  // HAVE_PYTHON
+
   INLINE bool operator == (const TypeHandle &other) const;
   INLINE bool operator == (const TypeHandle &other) const;
   INLINE bool operator != (const TypeHandle &other) const;
   INLINE bool operator != (const TypeHandle &other) const;
   INLINE bool operator < (const TypeHandle &other) const;
   INLINE bool operator < (const TypeHandle &other) const;