00001 // Filename: typeHandle.h 00002 // Created by: drose (23Oct98) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #ifndef TYPEHANDLE_H 00020 #define TYPEHANDLE_H 00021 00022 #include "pandabase.h" 00023 00024 #include "notify.h" 00025 00026 #include "config_express.h" 00027 00028 // The following illustrates the convention for declaring a type that 00029 // uses TypeHandle. In this example, ThisThingie inherits from 00030 // TypedObject, which automatically supplies some type-differentiation 00031 // functions at the cost of one virtual function, get_type(); however, 00032 // this inheritance is optional, and may be omitted to avoid the 00033 // virtual function pointer overhead. (If you do use TypedObject, be 00034 // sure to consider whether your destructor should also be virtual.) 00035 00036 // 00037 // class ThatThingie : public SimpleTypedObject { 00038 // public: 00039 // static TypeHandle get_class_type() { 00040 // return _type_handle; 00041 // } 00042 // static void init_type() { 00043 // register_type(_type_handle, "ThatThingie"); 00044 // } 00045 // 00046 // private: 00047 // static TypeHandle _type_handle; 00048 // }; 00049 // 00050 // class ThisThingie : public ThatThingie, publid TypedObject { 00051 // public: 00052 // static TypeHandle get_class_type() { 00053 // return _type_handle; 00054 // } 00055 // static void init_type() { 00056 // ThatThingie::init_type(); 00057 // TypedObject::init_type(); 00058 // register_type(_type_handle, "ThisThingie", 00059 // ThatThingie::get_class_type(), 00060 // TypedObject::get_class_type()); 00061 // } 00062 // virtual TypeHandle get_type() const { 00063 // return get_class_type(); 00064 // } 00065 // 00066 // private: 00067 // static TypeHandle _type_handle; 00068 // }; 00069 // 00070 00071 class TypedObject; 00072 00073 //////////////////////////////////////////////////////////////////// 00074 // Class : TypeHandle 00075 // Description : TypeHandle is the identifier used to differentiate 00076 // C++ class types. Any C++ classes that inherit from 00077 // some base class, and must be differentiated at run 00078 // time, should store a static TypeHandle object that 00079 // can be queried through a static member function 00080 // named get_class_type(). Most of the time, it is also 00081 // desirable to inherit from TypedObject, which provides 00082 // some virtual functions to return the TypeHandle for a 00083 // particular instance. 00084 // 00085 // At its essence, a TypeHandle is simply a unique 00086 // identifier that is assigned by the TypeRegistry. The 00087 // TypeRegistry stores a tree of TypeHandles, so that 00088 // ancestry of a particular type may be queried, and the 00089 // type name may be retrieved for run-time display. 00090 //////////////////////////////////////////////////////////////////// 00091 class EXPCL_PANDAEXPRESS TypeHandle { 00092 PUBLISHED: 00093 INLINE TypeHandle(); 00094 INLINE TypeHandle(const TypeHandle ©); 00095 00096 INLINE bool operator == (const TypeHandle &other) const; 00097 INLINE bool operator != (const TypeHandle &other) const; 00098 INLINE bool operator < (const TypeHandle &other) const; 00099 INLINE bool operator <= (const TypeHandle &other) const; 00100 INLINE bool operator > (const TypeHandle &other) const; 00101 INLINE bool operator >= (const TypeHandle &other) const; 00102 INLINE int compare_to(const TypeHandle &other) const; 00103 00104 INLINE string get_name(TypedObject *object = (TypedObject *)NULL) const; 00105 INLINE bool is_derived_from(TypeHandle parent, 00106 TypedObject *object = (TypedObject *)NULL) const; 00107 00108 INLINE int get_num_parent_classes(TypedObject *object = (TypedObject *)NULL) const; 00109 INLINE TypeHandle get_parent_class(int index) const; 00110 00111 INLINE int get_num_child_classes(TypedObject *object = (TypedObject *)NULL) const; 00112 INLINE TypeHandle get_child_class(int index) const; 00113 00114 INLINE TypeHandle get_parent_towards(TypeHandle ancestor, 00115 TypedObject *object = (TypedObject *)NULL) const; 00116 00117 INLINE int get_index() const; 00118 INLINE void output(ostream &out) const; 00119 INLINE static TypeHandle none(); 00120 00121 private: 00122 int _index; 00123 static TypeHandle _none; 00124 00125 friend class TypeRegistry; 00126 }; 00127 00128 00129 // It's handy to be able to output a TypeHandle directly, and see the 00130 // type name. 00131 INLINE ostream &operator << (ostream &out, TypeHandle type) { 00132 type.output(out); 00133 return out; 00134 } 00135 00136 // We must include typeRegistry at this point so we can call it from 00137 // our inline functions. This is a circular include that is 00138 // strategically placed to do no harm. 00139 /* okcircular */ 00140 #include "typeRegistry.h" 00141 00142 #include "typeHandle.I" 00143 00144 #endif 00145