Panda3D
typeHandle.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file typeHandle.I
10  * @author drose
11  * @date 2000-02-22
12  */
13 
14 /**
15  *
16  */
17 INLINE bool TypeHandle::
18 operator == (const TypeHandle &other) const {
19  return (_index == other._index);
20 }
21 
22 /**
23  *
24  */
25 INLINE bool TypeHandle::
26 operator != (const TypeHandle &other) const {
27  return (_index != other._index);
28 }
29 
30 /**
31  *
32  */
33 INLINE bool TypeHandle::
34 operator < (const TypeHandle &other) const {
35  return (_index < other._index);
36 }
37 
38 /**
39  *
40  */
41 INLINE bool TypeHandle::
42 operator <= (const TypeHandle &other) const {
43  return (_index <= other._index);
44 }
45 
46 /**
47  *
48  */
49 INLINE bool TypeHandle::
50 operator > (const TypeHandle &other) const {
51  return (_index > other._index);
52 }
53 
54 /**
55  *
56  */
57 INLINE bool TypeHandle::
58 operator >= (const TypeHandle &other) const {
59  return (_index >= other._index);
60 }
61 
62 /**
63  * Sorts TypeHandles arbitrarily (according to <, >, etc.). Returns a number
64  * less than 0 if this type sorts before the other one, greater than zero if
65  * it sorts after, 0 if they are equivalent.
66  */
67 INLINE int TypeHandle::
68 compare_to(const TypeHandle &other) const {
69  return _index - other._index;
70 }
71 
72 /**
73  * Returns a hash code suitable for phash_map.
74  */
75 INLINE size_t TypeHandle::
76 get_hash() const {
77  return (size_t)_index;
78 }
79 
80 /**
81  * Returns the name of the type.
82  *
83  * The "object" pointer is an optional pointer to the TypedObject class that
84  * owns this TypeHandle. It is only used in case the TypeHandle is
85  * inadvertantly undefined.
86  */
87 INLINE std::string TypeHandle::
88 get_name(TypedObject *object) const {
89  if ((*this) == TypeHandle::none()) {
90  return "none";
91  } else {
92  return TypeRegistry::ptr()->get_name(*this, object);
93  }
94 }
95 
96 /**
97  * Returns true if this type is derived from the indicated type, false
98  * otherwise.
99  *
100  * The "object" pointer is an optional pointer to the TypedObject class that
101  * owns this TypeHandle. It is only used in case the TypeHandle is
102  * inadvertantly undefined.
103  */
104 INLINE bool TypeHandle::
105 is_derived_from(TypeHandle parent, TypedObject *object) const {
106  return TypeRegistry::ptr()->is_derived_from(*this, parent, object);
107 }
108 
109 /**
110  * Returns the number of parent classes that this type is known to have. This
111  * may then be used to index into get_parent_class(). The result will be 0 if
112  * this class does not inherit from any other classes, 1 if normal, single
113  * inheritance is in effect, or greater than one if multiple inheritance is in
114  * effect.
115  *
116  * The "object" pointer is an optional pointer to the TypedObject class that
117  * owns this TypeHandle. It is only used in case the TypeHandle is
118  * inadvertantly undefined.
119  */
120 INLINE int TypeHandle::
121 get_num_parent_classes(TypedObject *object) const {
122  return TypeRegistry::ptr()->get_num_parent_classes(*this, object);
123 }
124 
125 /**
126  * Returns the nth parent class of this type. The index should be in the
127  * range 0 <= index < get_num_parent_classes().
128  */
130 get_parent_class(int index) const {
131  return TypeRegistry::ptr()->get_parent_class(*this, index);
132 }
133 
134 /**
135  * Returns the number of child classes that this type is known to have. This
136  * may then be used to index into get_child_class().
137  *
138  * The "object" pointer is an optional pointer to the TypedObject class that
139  * owns this TypeHandle. It is only used in case the TypeHandle is
140  * inadvertantly undefined.
141  */
142 INLINE int TypeHandle::
143 get_num_child_classes(TypedObject *object) const {
144  return TypeRegistry::ptr()->get_num_child_classes(*this, object);
145 }
146 
147 /**
148  * Returns the nth child class of this type. The index should be in the range
149  * 0 <= index < get_num_child_classes().
150  */
152 get_child_class(int index) const {
153  return TypeRegistry::ptr()->get_child_class(*this, index);
154 }
155 
156 /**
157  * Returns the parent class that is in a direct line of inheritance to the
158  * indicated ancestor class. This is useful in the presence of multiple
159  * inheritance to try to determine what properties an unknown type may have.
160  *
161  * The return value is TypeHandle::none() if the type does not inherit from
162  * the ancestor. If ancestor is the same as this type, the return value is
163  * this type.
164  *
165  * The "object" pointer is an optional pointer to the TypedObject class that
166  * owns this TypeHandle. It is only used in case the TypeHandle is
167  * inadvertantly undefined.
168  */
170 get_parent_towards(TypeHandle ancestor, TypedObject *object) const {
171  return TypeRegistry::ptr()->get_parent_towards(*this, ancestor, object);
172 }
173 
174 /**
175  * Returns the integer index associated with this TypeHandle. Each different
176  * TypeHandle will have a different index. However, you probably shouldn't be
177  * using this method; you should just treat the TypeHandles as opaque classes.
178  * This is provided for the convenience of non-C++ scripting languages to
179  * build a hashtable of TypeHandles.
180  */
181 INLINE int TypeHandle::
182 get_index() const {
183  return _index;
184 }
185 
186 /**
187  *
188  */
189 INLINE void TypeHandle::
190 output(std::ostream &out) const {
191  out << get_name();
192 }
193 
194 /**
195  * TypeHandle::none() evaluates to false, everything else evaluates to true.
196  */
197 INLINE TypeHandle::
198 operator bool () const {
199  return (_index != 0);
200 }
201 
202 /**
203  * Private constructor for initializing a TypeHandle from an index, used by
204  * none() and by from_index().
205  */
206 constexpr TypeHandle::
207 TypeHandle(int index) : _index(index) {
208 }
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.
std::string get_name(TypeHandle type, TypedObject *object) const
Returns the name of the indicated type.
size_t get_hash() const
Returns a hash code suitable for phash_map.
Definition: typeHandle.I:76
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.
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:88
bool is_derived_from(TypeHandle parent, TypedObject *object=nullptr) const
Returns true if this type is derived from the indicated type, false otherwise.
Definition: typeHandle.I:105
get_name
Returns the name of the type.
Definition: typeHandle.h:136
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:68
get_num_child_classes
Returns the number of child classes that this type is known to have.
Definition: typeHandle.h:138
get_parent_class
Returns the nth parent class of this type.
Definition: typeHandle.h:137
static TypeRegistry * ptr()
Returns the pointer to the global TypeRegistry object.
Definition: typeRegistry.I:30
TypeHandle get_parent_towards(TypeHandle ancestor, TypedObject *object=nullptr) const
Returns the parent class that is in a direct line of inheritance to the indicated ancestor class.
Definition: typeHandle.I:170
get_child_class
Returns the nth child class of this type.
Definition: typeHandle.h:138
get_num_parent_classes
Returns the number of parent classes that this type is known to have.
Definition: typeHandle.h:137
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81