Panda3D
register_type.h
1 // Filename: register_type.h
2 // Created by: drose (06Aug01)
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 #ifndef REGISTER_TYPE_H
16 #define REGISTER_TYPE_H
17 
18 #include "dtoolbase.h"
19 
20 #include "typeHandle.h"
21 #include "typeRegistry.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function: register_type
25 // Description: This inline function is just a convenient way to call
26 // TypeRegistry::register_type(), along with zero to four
27 // record_derivation()s. If for some reason you have a
28 // class that has more than four base classes (you're
29 // insane!), then you will need to call Register() and
30 // record_derivation() yourself.
31 ////////////////////////////////////////////////////////////////////
32 INLINE void
33 register_type(TypeHandle &type_handle, const string &name);
34 
35 INLINE void
36 register_type(TypeHandle &type_handle, const string &name,
37  TypeHandle parent1);
38 
39 INLINE void
40 register_type(TypeHandle &type_handle, const string &name,
41  TypeHandle parent1, TypeHandle parent2);
42 
43 INLINE void
44 register_type(TypeHandle &type_handle, const string &name,
45  TypeHandle parent1, TypeHandle parent2,
46  TypeHandle parent3);
47 
48 INLINE void
49 register_type(TypeHandle &type_handle, const string &name,
50  TypeHandle parent1, TypeHandle parent2,
51  TypeHandle parent3, TypeHandle parent4);
52 
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function: register_dynamic_type
56 // Description: This is essentially similar to register_type(),
57 // except that it doesn't store a reference to any
58 // TypeHandle passed in and it therefore doesn't
59 // complain if the type is registered more than once to
60 // different TypeHandle reference.
61 ////////////////////////////////////////////////////////////////////
62 INLINE TypeHandle
63 register_dynamic_type(const string &name);
64 
65 INLINE TypeHandle
66 register_dynamic_type(const string &name, TypeHandle parent1);
67 
68 INLINE TypeHandle
69 register_dynamic_type(const string &name,
70  TypeHandle parent1, TypeHandle parent2);
71 
72 INLINE TypeHandle
73 register_dynamic_type(const string &name,
74  TypeHandle parent1, TypeHandle parent2,
75  TypeHandle parent3);
76 
77 INLINE TypeHandle
78 register_dynamic_type(const string &name,
79  TypeHandle parent1, TypeHandle parent2,
80  TypeHandle parent3, TypeHandle parent4);
81 
82 
83 // A few system-wide TypeHandles are defined for some basic types.
84 extern TypeHandle EXPCL_DTOOL long_type_handle;
85 extern TypeHandle EXPCL_DTOOL int_type_handle;
86 extern TypeHandle EXPCL_DTOOL uint_type_handle;
87 extern TypeHandle EXPCL_DTOOL short_type_handle;
88 extern TypeHandle EXPCL_DTOOL ushort_type_handle;
89 extern TypeHandle EXPCL_DTOOL char_type_handle;
90 extern TypeHandle EXPCL_DTOOL uchar_type_handle;
91 extern TypeHandle EXPCL_DTOOL bool_type_handle;
92 extern TypeHandle EXPCL_DTOOL double_type_handle;
93 extern TypeHandle EXPCL_DTOOL float_type_handle;
94 extern TypeHandle EXPCL_DTOOL string_type_handle;
95 extern TypeHandle EXPCL_DTOOL wstring_type_handle;
96 
97 extern TypeHandle long_p_type_handle;
98 extern TypeHandle int_p_type_handle;
99 extern TypeHandle short_p_type_handle;
100 extern TypeHandle char_p_type_handle;
101 extern TypeHandle bool_p_type_handle;
102 extern TypeHandle double_p_type_handle;
103 extern TypeHandle float_p_type_handle;
104 extern TypeHandle void_p_type_handle;
105 
106 extern TypeHandle EXPCL_DTOOL pvector_type_handle;
107 extern TypeHandle EXPCL_DTOOL ov_set_type_handle;
108 extern TypeHandle EXPCL_DTOOL pdeque_type_handle;
109 extern TypeHandle EXPCL_DTOOL plist_type_handle;
110 extern TypeHandle EXPCL_DTOOL pmap_type_handle;
111 extern TypeHandle EXPCL_DTOOL pset_type_handle;
112 
113 void EXPCL_DTOOL init_system_type_handles();
114 
115 // The following template function and its specializations will return
116 // a TypeHandle for any type in the world, from a pointer to that
117 // type.
118 
119 template<class T>
120 INLINE TypeHandle _get_type_handle(const T *) {
121  return T::get_class_type();
122 }
123 
124 template<>
125 INLINE TypeHandle _get_type_handle(const long *) {
126  return long_type_handle;
127 }
128 
129 template<>
130 INLINE TypeHandle _get_type_handle(const int *) {
131  return int_type_handle;
132 }
133 
134 template<>
135 INLINE TypeHandle _get_type_handle(const unsigned int *) {
136  return uint_type_handle;
137 }
138 
139 template<>
140 INLINE TypeHandle _get_type_handle(const short *) {
141  return short_type_handle;
142 }
143 
144 template<>
145 INLINE TypeHandle _get_type_handle(const unsigned short *) {
146  return ushort_type_handle;
147 }
148 
149 template<>
150 INLINE TypeHandle _get_type_handle(const char *) {
151  return char_type_handle;
152 }
153 
154 template<>
155 INLINE TypeHandle _get_type_handle(const unsigned char *) {
156  return uchar_type_handle;
157 }
158 
159 template<>
160 INLINE TypeHandle _get_type_handle(const bool *) {
161  return bool_type_handle;
162 }
163 
164 template<>
165 INLINE TypeHandle _get_type_handle(const double *) {
166  return double_type_handle;
167 }
168 
169 template<>
170 INLINE TypeHandle _get_type_handle(const float *) {
171  return float_type_handle;
172 }
173 
174 template<>
175 INLINE TypeHandle _get_type_handle(const string *) {
176  return string_type_handle;
177 }
178 
179 template<>
180 INLINE TypeHandle _get_type_handle(const wstring *) {
181  return wstring_type_handle;
182 }
183 
184 template<>
185 INLINE TypeHandle _get_type_handle(const long * const *) {
186  return long_p_type_handle;
187 }
188 
189 template<>
190 INLINE TypeHandle _get_type_handle(const int * const *) {
191  return int_p_type_handle;
192 }
193 
194 template<>
195 INLINE TypeHandle _get_type_handle(const short * const *) {
196  return short_p_type_handle;
197 }
198 
199 template<>
200 INLINE TypeHandle _get_type_handle(const char * const *) {
201  return char_p_type_handle;
202 }
203 
204 template<>
205 INLINE TypeHandle _get_type_handle(const bool * const *) {
206  return bool_p_type_handle;
207 }
208 
209 template<>
210 INLINE TypeHandle _get_type_handle(const double * const *) {
211  return double_p_type_handle;
212 }
213 
214 template<>
215 INLINE TypeHandle _get_type_handle(const float * const *) {
216  return float_p_type_handle;
217 }
218 
219 template<>
220 INLINE TypeHandle _get_type_handle(const void * const *) {
221  return void_p_type_handle;
222 }
223 
224 
225 // The macro get_type_handle(type) is defined to make getting the type
226 // handle associated with a particular type a bit cleaner.
227 #define get_type_handle(type) _get_type_handle((const type *)0)
228 
229 
230 // The following template function and its specializations can be used
231 // to call init() on any unknown type. Handy for use within a
232 // template class.
233 
234 template<class T>
235 INLINE void _do_init_type(const T *) {
236  T::init_type();
237 }
238 
239 template<>
240 INLINE void _do_init_type(const long *) {
241  init_system_type_handles();
242 }
243 
244 template<>
245 INLINE void _do_init_type(const int *) {
246  init_system_type_handles();
247 }
248 
249 template<>
250 INLINE void _do_init_type(const short *) {
251  init_system_type_handles();
252 }
253 
254 template<>
255 INLINE void _do_init_type(const char *) {
256  init_system_type_handles();
257 }
258 
259 template<>
260 INLINE void _do_init_type(const bool *) {
261  init_system_type_handles();
262 }
263 
264 template<>
265 INLINE void _do_init_type(const double *) {
266  init_system_type_handles();
267 }
268 
269 template<>
270 INLINE void _do_init_type(const float *) {
271  init_system_type_handles();
272 }
273 
274 template<>
275 INLINE void _do_init_type(const long * const *) {
276  init_system_type_handles();
277 }
278 
279 template<>
280 INLINE void _do_init_type(const int * const *) {
281  init_system_type_handles();
282 }
283 
284 template<>
285 INLINE void _do_init_type(const short * const *) {
286  init_system_type_handles();
287 }
288 
289 template<>
290 INLINE void _do_init_type(const char * const *) {
291  init_system_type_handles();
292 }
293 
294 template<>
295 INLINE void _do_init_type(const bool * const *) {
296  init_system_type_handles();
297 }
298 
299 template<>
300 INLINE void _do_init_type(const double * const *) {
301  init_system_type_handles();
302 }
303 
304 template<>
305 INLINE void _do_init_type(const float * const *) {
306  init_system_type_handles();
307 }
308 
309 template<>
310 INLINE void _do_init_type(const void * const *) {
311  init_system_type_handles();
312 }
313 
314 #define do_init_type(type) _do_init_type((const type *)0)
315 
316 #include "register_type.I"
317 
318 #endif
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85