Panda3D
 All Classes Functions Variables Enumerations
typeHandle.h
00001 // Filename: typeHandle.h
00002 // Created by:  drose (23Oct98)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef TYPEHANDLE_H
00016 #define TYPEHANDLE_H
00017 
00018 #include "dtoolbase.h"
00019 #include "typeRegistry.h"
00020 
00021 #include <set>
00022 
00023 // The following illustrates the convention for declaring a type that
00024 // uses TypeHandle.  In this example, ThisThingie inherits from
00025 // TypedObject, which automatically supplies some type-differentiation
00026 // functions at the cost of one virtual function, get_type(); however,
00027 // this inheritance is optional, and may be omitted to avoid the
00028 // virtual function pointer overhead.  (If you do use TypedObject, be
00029 // sure to consider whether your destructor should also be virtual.)
00030 
00031 //
00032 // class ThatThingie : public SimpleTypedObject {
00033 // public:
00034 //   static TypeHandle get_class_type() {
00035 //     return _type_handle;
00036 //   }
00037 //   static void init_type() {
00038 //     register_type(_type_handle, "ThatThingie");
00039 //   }
00040 //
00041 // private:
00042 //   static TypeHandle _type_handle;
00043 // };
00044 //
00045 // class ThisThingie : public ThatThingie, publid TypedObject {
00046 // public:
00047 //   static TypeHandle get_class_type() {
00048 //     return _type_handle;
00049 //   }
00050 //   static void init_type() {
00051 //     ThatThingie::init_type();
00052 //     TypedObject::init_type();
00053 //     register_type(_type_handle, "ThisThingie",
00054 //                  ThatThingie::get_class_type(),
00055 //                  TypedObject::get_class_type());
00056 //   }
00057 //   virtual TypeHandle get_type() const {
00058 //     return get_class_type();
00059 //   }
00060 //
00061 // private:
00062 //   static TypeHandle _type_handle;
00063 // };
00064 //
00065 
00066 class TypedObject;
00067 
00068 #ifdef HAVE_PYTHON
00069 #include "Python.h"
00070 #endif
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //       Class : TypeHandle
00074 // Description : TypeHandle is the identifier used to differentiate
00075 //               C++ class types.  Any C++ classes that inherit from
00076 //               some base class, and must be differentiated at run
00077 //               time, should store a static TypeHandle object that
00078 //               can be queried through a static member function
00079 //               named get_class_type().  Most of the time, it is also
00080 //               desirable to inherit from TypedObject, which provides
00081 //               some virtual functions to return the TypeHandle for a
00082 //               particular instance.
00083 //
00084 //               At its essence, a TypeHandle is simply a unique
00085 //               identifier that is assigned by the TypeRegistry.  The
00086 //               TypeRegistry stores a tree of TypeHandles, so that
00087 //               ancestry of a particular type may be queried, and the
00088 //               type name may be retrieved for run-time display.
00089 ////////////////////////////////////////////////////////////////////
00090 class EXPCL_DTOOL TypeHandle {
00091 PUBLISHED:
00092   enum MemoryClass {
00093     MC_singleton,
00094     MC_array,
00095     MC_deleted_chain_active,
00096     MC_deleted_chain_inactive,
00097 
00098     MC_limit  // Not a real value, just a placeholder for the maximum
00099               // enum value.
00100   };
00101 
00102   INLINE TypeHandle();
00103   INLINE TypeHandle(const TypeHandle &copy);
00104 
00105 #ifdef HAVE_PYTHON
00106   static PyObject *make(PyObject *classobj);
00107 #endif  // HAVE_PYTHON
00108 
00109   INLINE bool operator == (const TypeHandle &other) const;
00110   INLINE bool operator != (const TypeHandle &other) const;
00111   INLINE bool operator < (const TypeHandle &other) const;
00112   INLINE bool operator <= (const TypeHandle &other) const;
00113   INLINE bool operator > (const TypeHandle &other) const;
00114   INLINE bool operator >= (const TypeHandle &other) const;
00115   INLINE int compare_to(const TypeHandle &other) const;
00116   INLINE size_t get_hash() const;
00117 
00118   INLINE string get_name(TypedObject *object = (TypedObject *)NULL) const;
00119   INLINE bool is_derived_from(TypeHandle parent,
00120                               TypedObject *object = (TypedObject *)NULL) const;
00121 
00122   INLINE int get_num_parent_classes(TypedObject *object = (TypedObject *)NULL) const;
00123   INLINE TypeHandle get_parent_class(int index) const;
00124 
00125   INLINE int get_num_child_classes(TypedObject *object = (TypedObject *)NULL) const;
00126   INLINE TypeHandle get_child_class(int index) const;
00127 
00128   INLINE TypeHandle get_parent_towards(TypeHandle ancestor,
00129                                        TypedObject *object = (TypedObject *)NULL) const;
00130   
00131   INLINE  int get_best_parent_from_Set(const std::set< int > &legal_vals) const;
00132 
00133 #ifdef DO_MEMORY_USAGE
00134   int get_memory_usage(MemoryClass memory_class) const;
00135   void inc_memory_usage(MemoryClass memory_class, int size);
00136   void dec_memory_usage(MemoryClass memory_class, int size);
00137 #else
00138   INLINE int get_memory_usage(MemoryClass) const { return 0; }
00139   INLINE void inc_memory_usage(MemoryClass, int) { }
00140   INLINE void dec_memory_usage(MemoryClass, int) { }
00141 #endif  // DO_MEMORY_USAGE
00142 
00143   INLINE int get_index() const;
00144   INLINE void output(ostream &out) const;
00145   INLINE static TypeHandle none();
00146 
00147 private:
00148   int _index;
00149   static TypeHandle _none;
00150 
00151 friend class TypeRegistry;
00152 };
00153 
00154 
00155 // It's handy to be able to output a TypeHandle directly, and see the
00156 // type name.
00157 INLINE ostream &operator << (ostream &out, TypeHandle type) {
00158   type.output(out);
00159   return out;
00160 }
00161 
00162 EXPCL_DTOOL ostream &operator << (ostream &out, TypeHandle::MemoryClass mem_class);
00163 
00164 // We must include typeRegistry at this point so we can call it from
00165 // our inline functions.  This is a circular include that is
00166 // strategically placed to do no harm.
00167 /* okcircular */
00168 #include "typeRegistry.h"
00169 
00170 #include "typeHandle.I"
00171 
00172 #endif
00173 
 All Classes Functions Variables Enumerations