Panda3D
typeHandle.I
1 // Filename: typeHandle.I
2 // Created by: drose (22Feb00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: TypeHandle::Constructor
18 // Access: Published
19 // Description: The default constructor must do nothing, because we
20 // can't guarantee ordering of static initializers. If
21 // the constructor tried to initialize its value, it
22 // might happen after the value had already been set
23 // previously by another static initializer!
24 ////////////////////////////////////////////////////////////////////
25 INLINE TypeHandle::
27 }
28 
29 ////////////////////////////////////////////////////////////////////
30 // Function: TypeHandle::Copy Constructor
31 // Access: Published
32 // Description:
33 ////////////////////////////////////////////////////////////////////
34 INLINE TypeHandle::
35 TypeHandle(const TypeHandle &copy) : _index(copy._index) {
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: TypeHandle::Equality Operator
40 // Access: Published
41 // Description:
42 ////////////////////////////////////////////////////////////////////
43 INLINE bool TypeHandle::
44 operator == (const TypeHandle &other) const {
45  return (_index == other._index);
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: TypeHandle::Inequality Operator
50 // Access: Published
51 // Description:
52 ////////////////////////////////////////////////////////////////////
53 INLINE bool TypeHandle::
54 operator != (const TypeHandle &other) const {
55  return (_index != other._index);
56 }
57 
58 ////////////////////////////////////////////////////////////////////
59 // Function: TypeHandle::Ordering Operator
60 // Access: Published
61 // Description:
62 ////////////////////////////////////////////////////////////////////
63 INLINE bool TypeHandle::
64 operator < (const TypeHandle &other) const {
65  return (_index < other._index);
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: TypeHandle::Ordering Operator
70 // Access: Published
71 // Description:
72 ////////////////////////////////////////////////////////////////////
73 INLINE bool TypeHandle::
74 operator <= (const TypeHandle &other) const {
75  return (_index <= other._index);
76 }
77 
78 ////////////////////////////////////////////////////////////////////
79 // Function: TypeHandle::Ordering Operator
80 // Access: Published
81 // Description:
82 ////////////////////////////////////////////////////////////////////
83 INLINE bool TypeHandle::
84 operator > (const TypeHandle &other) const {
85  return (_index > other._index);
86 }
87 
88 ////////////////////////////////////////////////////////////////////
89 // Function: TypeHandle::Ordering Operator
90 // Access: Published
91 // Description:
92 ////////////////////////////////////////////////////////////////////
93 INLINE bool TypeHandle::
94 operator >= (const TypeHandle &other) const {
95  return (_index >= other._index);
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: TypeHandle::compare_to
100 // Access: Published
101 // Description: Sorts TypeHandles arbitrarily (according to <, >,
102 // etc.). Returns a number less than 0 if this type
103 // sorts before the other one, greater than zero if it
104 // sorts after, 0 if they are equivalent.
105 ////////////////////////////////////////////////////////////////////
106 INLINE int TypeHandle::
107 compare_to(const TypeHandle &other) const {
108  return _index - other._index;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function: TypeHandle::get_hash
113 // Access: Published
114 // Description: Returns a hash code suitable for phash_map.
115 ////////////////////////////////////////////////////////////////////
116 INLINE size_t TypeHandle::
117 get_hash() const {
118  return (size_t)_index;
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function: TypeHandle::get_name
123 // Access: Published
124 // Description: Returns the name of the type.
125 //
126 // The "object" pointer is an optional pointer to the
127 // TypedObject class that owns this TypeHandle. It is
128 // only used in case the TypeHandle is inadvertantly
129 // undefined.
130 ////////////////////////////////////////////////////////////////////
131 INLINE string TypeHandle::
132 get_name(TypedObject *object) const {
133  if ((*this) == TypeHandle::none()) {
134  return "none";
135  } else {
136  return TypeRegistry::ptr()->get_name(*this, object);
137  }
138 }
139 
140 ////////////////////////////////////////////////////////////////////
141 // Function: TypeHandle::is_derived_from
142 // Access: Published
143 // Description: Returns true if this type is derived from the
144 // indicated type, false otherwise.
145 //
146 // The "object" pointer is an optional pointer to the
147 // TypedObject class that owns this TypeHandle. It is
148 // only used in case the TypeHandle is inadvertantly
149 // undefined.
150 ////////////////////////////////////////////////////////////////////
151 INLINE bool TypeHandle::
152 is_derived_from(TypeHandle parent, TypedObject *object) const {
153  return TypeRegistry::ptr()->is_derived_from(*this, parent, object);
154 }
155 
156 ////////////////////////////////////////////////////////////////////
157 // Function: TypeHandle::get_num_parent_classes
158 // Access: Published
159 // Description: Returns the number of parent classes that this
160 // type is known to have. This may then be used to
161 // index into get_parent_class(). The result will be 0
162 // if this class does not inherit from any other
163 // classes, 1 if normal, single inheritance is in
164 // effect, or greater than one if multiple inheritance
165 // is in effect.
166 //
167 // The "object" pointer is an optional pointer to the
168 // TypedObject class that owns this TypeHandle. It is
169 // only used in case the TypeHandle is inadvertantly
170 // undefined.
171 ////////////////////////////////////////////////////////////////////
172 INLINE int TypeHandle::
174  return TypeRegistry::ptr()->get_num_parent_classes(*this, object);
175 }
176 
177 ////////////////////////////////////////////////////////////////////
178 // Function: TypeHandle::get_num_parent_classes
179 // Access: Published
180 // Description: Returns the nth parent class of this type. The index
181 // should be in the range 0 <= index <
182 // get_num_parent_classes().
183 ////////////////////////////////////////////////////////////////////
185 get_parent_class(int index) const {
186  return TypeRegistry::ptr()->get_parent_class(*this, index);
187 }
188 
189 ////////////////////////////////////////////////////////////////////
190 // Function: TypeHandle::get_num_child_classes
191 // Access: Published
192 // Description: Returns the number of child classes that this
193 // type is known to have. This may then be used to
194 // index into get_child_class().
195 //
196 // The "object" pointer is an optional pointer to the
197 // TypedObject class that owns this TypeHandle. It is
198 // only used in case the TypeHandle is inadvertantly
199 // undefined.
200 ////////////////////////////////////////////////////////////////////
201 INLINE int TypeHandle::
203  return TypeRegistry::ptr()->get_num_child_classes(*this, object);
204 }
205 
206 ////////////////////////////////////////////////////////////////////
207 // Function: TypeHandle::get_num_child_classes
208 // Access: Published
209 // Description: Returns the nth child class of this type. The index
210 // should be in the range 0 <= index <
211 // get_num_child_classes().
212 ////////////////////////////////////////////////////////////////////
214 get_child_class(int index) const {
215  return TypeRegistry::ptr()->get_child_class(*this, index);
216 }
217 
218 ////////////////////////////////////////////////////////////////////
219 // Function: TypeHandle::get_parent_towards
220 // Access: Published
221 // Description: Returns the parent class that is in a direct line of
222 // inheritance to the indicated ancestor class. This is
223 // useful in the presence of multiple inheritance to try
224 // to determine what properties an unknown type may
225 // have.
226 //
227 // The return value is TypeHandle::none() if the type
228 // does not inherit from the ancestor. If ancestor is
229 // the same as this type, the return value is this type.
230 //
231 // The "object" pointer is an optional pointer to the
232 // TypedObject class that owns this TypeHandle. It is
233 // only used in case the TypeHandle is inadvertantly
234 // undefined.
235 ////////////////////////////////////////////////////////////////////
237 get_parent_towards(TypeHandle ancestor, TypedObject *object) const {
238  return TypeRegistry::ptr()->get_parent_towards(*this, ancestor, object);
239 }
240 
241 ////////////////////////////////////////////////////////////////////
242 // Function: TypeHandle::get_index
243 // Access: Published
244 // Description: Returns the integer index associated with this
245 // TypeHandle. Each different TypeHandle will have a
246 // different index. However, you probably shouldn't be
247 // using this method; you should just treat the
248 // TypeHandles as opaque classes. This is provided for
249 // the convenience of non-C++ scripting languages to
250 // build a hashtable of TypeHandles.
251 ////////////////////////////////////////////////////////////////////
252 INLINE int TypeHandle::
253 get_index() const {
254  return _index;
255 }
256 
257 ////////////////////////////////////////////////////////////////////
258 // Function: TypeHandle::output
259 // Access: Published
260 // Description:
261 ////////////////////////////////////////////////////////////////////
262 INLINE void TypeHandle::
263 output(ostream &out) const {
264  out << get_name();
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function: TypeHandle::none
269 // Access: Published, Static
270 // Description: Returns a special zero-valued TypeHandle that is used
271 // to indicate no type.
272 ////////////////////////////////////////////////////////////////////
274 none() {
275  return _none;
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function: TypeHandle::operator bool
280 // Access: Published
281 // Description: TypeHandle::none() evaluates to false, everything
282 // else evaluates to true.
283 ////////////////////////////////////////////////////////////////////
284 INLINE TypeHandle::
285 operator bool () const {
286  return (_index != 0);
287 }
288 
289 ////////////////////////////////////////////////////////////////////
290 // Function: get_best_parent_from_Set
291 // Access: Published
292 // Description: Return the Index of the BEst fit Classs from a set
293 ////////////////////////////////////////////////////////////////////
294 INLINE int TypeHandle::get_best_parent_from_Set(const std::set< int > &legal_vals) const
295 {
296  if(legal_vals.find(_index) != legal_vals.end())
297  return _index;
298 
299  for(int pi = 0; pi < get_num_parent_classes() ; pi++)
300  {
301  TypeHandle ph = get_parent_class(pi);
302  int val = ph.get_best_parent_from_Set(legal_vals);
303  if(val > 0)
304  return val;
305 
306  }
307  return -1;
308 }
bool is_derived_from(TypeHandle child, TypeHandle base, TypedObject *child_object)
Returns true if the first type is derived from the second type, false otherwise.
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
int get_best_parent_from_Set(const std::set< int > &legal_vals) const
Return the Index of the BEst fit Classs from a set.
Definition: typeHandle.I:294
bool is_derived_from(TypeHandle parent, TypedObject *object=(TypedObject *) NULL) const
Returns true if this type is derived from the indicated type, false otherwise.
Definition: typeHandle.I:152
TypeHandle()
The default constructor must do nothing, because we can&#39;t guarantee ordering of static initializers...
Definition: typeHandle.I:26
string get_name(TypeHandle type, TypedObject *object) const
Returns the name of the indicated type.
int get_num_child_classes(TypedObject *object=(TypedObject *) NULL) const
Returns the number of child classes that this type is known to have.
Definition: typeHandle.I:202
size_t get_hash() const
Returns a hash code suitable for phash_map.
Definition: typeHandle.I:117
int get_num_child_classes(TypeHandle child, TypedObject *child_object) const
Returns the number of child classes that the indicated type is known to have.
int get_index() const
Returns the integer index associated with this TypeHandle.
Definition: typeHandle.I:253
string get_name(TypedObject *object=(TypedObject *) NULL) const
Returns the name of the type.
Definition: typeHandle.I:132
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
TypeHandle get_parent_towards(TypeHandle ancestor, TypedObject *object=(TypedObject *) NULL) const
Returns the parent class that is in a direct line of inheritance to the indicated ancestor class...
Definition: typeHandle.I:237
TypeHandle get_parent_class(TypeHandle child, int index) const
Returns the nth parent class of this type.
TypeHandle get_parent_towards(TypeHandle child, TypeHandle base, TypedObject *child_object)
Returns the parent of the indicated child class that is in a direct line of inheritance to the indica...
int get_num_parent_classes(TypeHandle child, TypedObject *child_object) const
Returns the number of parent classes that the indicated type is known to have.
TypeHandle get_child_class(TypeHandle child, int index) const
Returns the nth child class of this type.
int compare_to(const TypeHandle &other) const
Sorts TypeHandles arbitrarily (according to <, >, etc.).
Definition: typeHandle.I:107
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
int get_num_parent_classes(TypedObject *object=(TypedObject *) NULL) const
Returns the number of parent classes that this type is known to have.
Definition: typeHandle.I:173
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
TypeHandle get_parent_class(int index) const
Returns the nth parent class of this type.
Definition: typeHandle.I:185
TypeHandle get_child_class(int index) const
Returns the nth child class of this type.
Definition: typeHandle.I:214