Browse Source

use as_typed_object()

David Rose 19 years ago
parent
commit
616de46c25

+ 1 - 1
dtool/src/interrogate/interfaceMakerPythonNative.cxx

@@ -429,7 +429,7 @@ void InterfaceMakerPythonNative::WriteReturnInstance(ostream &out, int indent_le
 
 
         if(IsPandaTypedObject(ctype->as_struct_type()))
         if(IsPandaTypedObject(ctype->as_struct_type()))
         {
         {
-            std::string typestr = "((TypedObject *)" + return_expr + ")->get_type_index()";
+            std::string typestr = "(" + return_expr + ")->as_typed_object()->get_type_index()";
 
 
             indent(out, indent_level)<<"return DTool_CreatePyInstanceTyped((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<< ows_memory_flag<<","<<typestr<<");\n";
             indent(out, indent_level)<<"return DTool_CreatePyInstanceTyped((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<< ows_memory_flag<<","<<typestr<<");\n";
 
 

+ 30 - 9
dtool/src/interrogatedb/typedObject.I

@@ -46,7 +46,7 @@ operator = (const TypedObject &) {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypedObject::get_type_index
 //     Function: TypedObject::get_type_index
-//       Access: Public
+//       Access: Published
 //  Description: Returns the internal index number associated with
 //  Description: Returns the internal index number associated with
 //               this object's TypeHandle, a unique number for each
 //               this object's TypeHandle, a unique number for each
 //               different type.  This is equivalent to
 //               different type.  This is equivalent to
@@ -59,7 +59,7 @@ get_type_index() const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypedObject::is_of_type
 //     Function: TypedObject::is_of_type
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the current object is or derives from
 //  Description: Returns true if the current object is or derives from
 //               the indicated type.
 //               the indicated type.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -70,7 +70,7 @@ is_of_type(TypeHandle handle) const {
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypedObject::is_exact_type
 //     Function: TypedObject::is_exact_type
-//       Access: Public
+//       Access: Published
 //  Description: Returns true if the current object is the indicated
 //  Description: Returns true if the current object is the indicated
 //               type exactly.
 //               type exactly.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -84,13 +84,34 @@ is_exact_type(TypeHandle handle) const {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: TypedObject::is_exact_type
+//     Function: TypedObject::get_best_parent_from_Set
 //       Access: Public
 //       Access: Public
-//  Description: Returns true if the current object is the indicated
-//               type exactly.
+//  Description: 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE int TypedObject::
 INLINE int TypedObject::
-get_best_parent_from_Set(const std::set<int> &inset) const
-{
-    return get_type().get_best_parent_from_Set(inset);
+get_best_parent_from_Set(const std::set<int> &inset) const {
+  return get_type().get_best_parent_from_Set(inset);
 }
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: TypedObject::as_typed_object
+//       Access: Public
+//  Description: Returns the object, upcast (if necessary) to a
+//               TypedObject pointer.
+////////////////////////////////////////////////////////////////////
+INLINE TypedObject *TypedObject::
+as_typed_object() {
+  return this;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: TypedObject::as_typed_object
+//       Access: Public
+//  Description: Returns the object, upcast (if necessary) to a
+//               TypedObject pointer.
+////////////////////////////////////////////////////////////////////
+INLINE const TypedObject *TypedObject::
+as_typed_object() const {
+  return this;
+}
+

+ 2 - 2
dtool/src/interrogatedb/typedObject.cxx

@@ -24,7 +24,7 @@ TypeHandle TypedObject::_type_handle;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypedObject::Destructor
 //     Function: TypedObject::Destructor
-//       Access: Public, Virtual
+//       Access: Published, Virtual
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 TypedObject::
 TypedObject::
@@ -34,7 +34,7 @@ TypedObject::
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: TypedObject::get_type
 //     Function: TypedObject::get_type
-//       Access: Public, Virtual
+//       Access: Published, Virtual
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 TypeHandle TypedObject::
 TypeHandle TypedObject::

+ 11 - 1
dtool/src/interrogatedb/typedObject.h

@@ -121,14 +121,24 @@ PUBLISHED:
   INLINE int get_type_index() const;
   INLINE int get_type_index() const;
   INLINE bool is_of_type(TypeHandle handle) const;
   INLINE bool is_of_type(TypeHandle handle) const;
   INLINE bool is_exact_type(TypeHandle handle) const;
   INLINE bool is_exact_type(TypeHandle handle) const;
-  INLINE int  get_best_parent_from_Set(const std::set<int> &) const;
 
 
 public:
 public:
+  INLINE int get_best_parent_from_Set(const std::set<int> &) const;
+
   // Derived classes should override this function to call
   // Derived classes should override this function to call
   // init_type().  It will only be called in error situations when the
   // init_type().  It will only be called in error situations when the
   // type was for some reason not properly initialized.
   // type was for some reason not properly initialized.
   virtual TypeHandle force_init_type()=0;
   virtual TypeHandle force_init_type()=0;
 
 
+  // This pair of methods exists mainly for the convenience of
+  // unambiguous upcasting.  Interrogate generates code to call this
+  // method instead of making an explicit cast to (TypedObject *);
+  // this allows classes who multiply inherit from TypedObejct to
+  // override these methods and disambiguate the cast.  It doesn't
+  // have to be a virtual method, since this is just a static upcast.
+  INLINE TypedObject *as_typed_object();
+  INLINE const TypedObject *as_typed_object() const;
+
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {
     return _type_handle;
     return _type_handle;