Panda3D
|
00001 // Filename: typeHandle.I 00002 // Created by: drose (22Feb00) 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 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: TypeHandle::Constructor 00018 // Access: Published 00019 // Description: The default constructor must do nothing, because we 00020 // can't guarantee ordering of static initializers. If 00021 // the constructor tried to initialize its value, it 00022 // might happen after the value had already been set 00023 // previously by another static initializer! 00024 //////////////////////////////////////////////////////////////////// 00025 INLINE TypeHandle:: 00026 TypeHandle() { 00027 } 00028 00029 //////////////////////////////////////////////////////////////////// 00030 // Function: TypeHandle::Copy Constructor 00031 // Access: Published 00032 // Description: 00033 //////////////////////////////////////////////////////////////////// 00034 INLINE TypeHandle:: 00035 TypeHandle(const TypeHandle ©) : _index(copy._index) { 00036 } 00037 00038 //////////////////////////////////////////////////////////////////// 00039 // Function: TypeHandle::Equality Operator 00040 // Access: Published 00041 // Description: 00042 //////////////////////////////////////////////////////////////////// 00043 INLINE bool TypeHandle:: 00044 operator == (const TypeHandle &other) const { 00045 return (_index == other._index); 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: TypeHandle::Inequality Operator 00050 // Access: Published 00051 // Description: 00052 //////////////////////////////////////////////////////////////////// 00053 INLINE bool TypeHandle:: 00054 operator != (const TypeHandle &other) const { 00055 return (_index != other._index); 00056 } 00057 00058 //////////////////////////////////////////////////////////////////// 00059 // Function: TypeHandle::Ordering Operator 00060 // Access: Published 00061 // Description: 00062 //////////////////////////////////////////////////////////////////// 00063 INLINE bool TypeHandle:: 00064 operator < (const TypeHandle &other) const { 00065 return (_index < other._index); 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: TypeHandle::Ordering Operator 00070 // Access: Published 00071 // Description: 00072 //////////////////////////////////////////////////////////////////// 00073 INLINE bool TypeHandle:: 00074 operator <= (const TypeHandle &other) const { 00075 return (_index <= other._index); 00076 } 00077 00078 //////////////////////////////////////////////////////////////////// 00079 // Function: TypeHandle::Ordering Operator 00080 // Access: Published 00081 // Description: 00082 //////////////////////////////////////////////////////////////////// 00083 INLINE bool TypeHandle:: 00084 operator > (const TypeHandle &other) const { 00085 return (_index > other._index); 00086 } 00087 00088 //////////////////////////////////////////////////////////////////// 00089 // Function: TypeHandle::Ordering Operator 00090 // Access: Published 00091 // Description: 00092 //////////////////////////////////////////////////////////////////// 00093 INLINE bool TypeHandle:: 00094 operator >= (const TypeHandle &other) const { 00095 return (_index >= other._index); 00096 } 00097 00098 //////////////////////////////////////////////////////////////////// 00099 // Function: TypeHandle::compare_to 00100 // Access: Published 00101 // Description: Sorts TypeHandles arbitrarily (according to <, >, 00102 // etc.). Returns a number less than 0 if this type 00103 // sorts before the other one, greater than zero if it 00104 // sorts after, 0 if they are equivalent. 00105 //////////////////////////////////////////////////////////////////// 00106 INLINE int TypeHandle:: 00107 compare_to(const TypeHandle &other) const { 00108 return _index - other._index; 00109 } 00110 00111 //////////////////////////////////////////////////////////////////// 00112 // Function: TypeHandle::get_hash 00113 // Access: Published 00114 // Description: Returns a hash code suitable for phash_map. 00115 //////////////////////////////////////////////////////////////////// 00116 INLINE size_t TypeHandle:: 00117 get_hash() const { 00118 return (size_t)_index; 00119 } 00120 00121 //////////////////////////////////////////////////////////////////// 00122 // Function: TypeHandle::get_name 00123 // Access: Published 00124 // Description: Returns the name of the type. 00125 // 00126 // The "object" pointer is an optional pointer to the 00127 // TypedObject class that owns this TypeHandle. It is 00128 // only used in case the TypeHandle is inadvertantly 00129 // undefined. 00130 //////////////////////////////////////////////////////////////////// 00131 INLINE string TypeHandle:: 00132 get_name(TypedObject *object) const { 00133 if ((*this) == TypeHandle::none()) { 00134 return "none"; 00135 } else { 00136 return TypeRegistry::ptr()->get_name(*this, object); 00137 } 00138 } 00139 00140 //////////////////////////////////////////////////////////////////// 00141 // Function: TypeHandle::is_derived_from 00142 // Access: Published 00143 // Description: Returns true if this type is derived from the 00144 // indicated type, false otherwise. 00145 // 00146 // The "object" pointer is an optional pointer to the 00147 // TypedObject class that owns this TypeHandle. It is 00148 // only used in case the TypeHandle is inadvertantly 00149 // undefined. 00150 //////////////////////////////////////////////////////////////////// 00151 INLINE bool TypeHandle:: 00152 is_derived_from(TypeHandle parent, TypedObject *object) const { 00153 return TypeRegistry::ptr()->is_derived_from(*this, parent, object); 00154 } 00155 00156 //////////////////////////////////////////////////////////////////// 00157 // Function: TypeHandle::get_num_parent_classes 00158 // Access: Published 00159 // Description: Returns the number of parent classes that this 00160 // type is known to have. This may then be used to 00161 // index into get_parent_class(). The result will be 0 00162 // if this class does not inherit from any other 00163 // classes, 1 if normal, single inheritance is in 00164 // effect, or greater than one if multiple inheritance 00165 // is in effect. 00166 // 00167 // The "object" pointer is an optional pointer to the 00168 // TypedObject class that owns this TypeHandle. It is 00169 // only used in case the TypeHandle is inadvertantly 00170 // undefined. 00171 //////////////////////////////////////////////////////////////////// 00172 INLINE int TypeHandle:: 00173 get_num_parent_classes(TypedObject *object) const { 00174 return TypeRegistry::ptr()->get_num_parent_classes(*this, object); 00175 } 00176 00177 //////////////////////////////////////////////////////////////////// 00178 // Function: TypeHandle::get_num_parent_classes 00179 // Access: Published 00180 // Description: Returns the nth parent class of this type. The index 00181 // should be in the range 0 <= index < 00182 // get_num_parent_classes(). 00183 //////////////////////////////////////////////////////////////////// 00184 INLINE TypeHandle TypeHandle:: 00185 get_parent_class(int index) const { 00186 return TypeRegistry::ptr()->get_parent_class(*this, index); 00187 } 00188 00189 //////////////////////////////////////////////////////////////////// 00190 // Function: TypeHandle::get_num_child_classes 00191 // Access: Published 00192 // Description: Returns the number of child classes that this 00193 // type is known to have. This may then be used to 00194 // index into get_child_class(). 00195 // 00196 // The "object" pointer is an optional pointer to the 00197 // TypedObject class that owns this TypeHandle. It is 00198 // only used in case the TypeHandle is inadvertantly 00199 // undefined. 00200 //////////////////////////////////////////////////////////////////// 00201 INLINE int TypeHandle:: 00202 get_num_child_classes(TypedObject *object) const { 00203 return TypeRegistry::ptr()->get_num_child_classes(*this, object); 00204 } 00205 00206 //////////////////////////////////////////////////////////////////// 00207 // Function: TypeHandle::get_num_child_classes 00208 // Access: Published 00209 // Description: Returns the nth child class of this type. The index 00210 // should be in the range 0 <= index < 00211 // get_num_child_classes(). 00212 //////////////////////////////////////////////////////////////////// 00213 INLINE TypeHandle TypeHandle:: 00214 get_child_class(int index) const { 00215 return TypeRegistry::ptr()->get_child_class(*this, index); 00216 } 00217 00218 //////////////////////////////////////////////////////////////////// 00219 // Function: TypeHandle::get_parent_towards 00220 // Access: Published 00221 // Description: Returns the parent class that is in a direct line of 00222 // inheritance to the indicated ancestor class. This is 00223 // useful in the presence of multiple inheritance to try 00224 // to determine what properties an unknown type may 00225 // have. 00226 // 00227 // The return value is TypeHandle::none() if the type 00228 // does not inherit from the ancestor. If ancestor is 00229 // the same as this type, the return value is this type. 00230 // 00231 // The "object" pointer is an optional pointer to the 00232 // TypedObject class that owns this TypeHandle. It is 00233 // only used in case the TypeHandle is inadvertantly 00234 // undefined. 00235 //////////////////////////////////////////////////////////////////// 00236 INLINE TypeHandle TypeHandle:: 00237 get_parent_towards(TypeHandle ancestor, TypedObject *object) const { 00238 return TypeRegistry::ptr()->get_parent_towards(*this, ancestor, object); 00239 } 00240 00241 //////////////////////////////////////////////////////////////////// 00242 // Function: TypeHandle::get_index 00243 // Access: Published 00244 // Description: Returns the integer index associated with this 00245 // TypeHandle. Each different TypeHandle will have a 00246 // different index. However, you probably shouldn't be 00247 // using this method; you should just treat the 00248 // TypeHandles as opaque classes. This is provided for 00249 // the convenience of non-C++ scripting languages to 00250 // build a hashtable of TypeHandles. 00251 //////////////////////////////////////////////////////////////////// 00252 INLINE int TypeHandle:: 00253 get_index() const { 00254 return _index; 00255 } 00256 00257 //////////////////////////////////////////////////////////////////// 00258 // Function: TypeHandle::output 00259 // Access: Published 00260 // Description: 00261 //////////////////////////////////////////////////////////////////// 00262 INLINE void TypeHandle:: 00263 output(ostream &out) const { 00264 out << get_name(); 00265 } 00266 00267 //////////////////////////////////////////////////////////////////// 00268 // Function: TypeHandle::none 00269 // Access: Published, Static 00270 // Description: Returns a special zero-valued TypeHandle that is used 00271 // to indicate no type. 00272 //////////////////////////////////////////////////////////////////// 00273 INLINE TypeHandle TypeHandle:: 00274 none() { 00275 return _none; 00276 } 00277 00278 //////////////////////////////////////////////////////////////////// 00279 // Function: get_best_parent_from_Set 00280 // Access: Published 00281 // Description: Return the Index of the BEst fit Classs from a set 00282 //////////////////////////////////////////////////////////////////// 00283 INLINE int TypeHandle::get_best_parent_from_Set(const std::set< int > &legal_vals) const 00284 { 00285 if(legal_vals.find(_index) != legal_vals.end()) 00286 return _index; 00287 00288 for(int pi = 0; pi < get_num_parent_classes() ; pi++) 00289 { 00290 TypeHandle ph = get_parent_class(pi); 00291 int val = ph.get_best_parent_from_Set(legal_vals); 00292 if(val > 0) 00293 return val; 00294 00295 } 00296 return -1; 00297 } 00298