Panda3D
 All Classes Functions Variables Enumerations
dcast.h
1 // Filename: dcast.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 DCAST_H
16 #define DCAST_H
17 
18 #include "pandabase.h"
19 
20 #include "typeHandle.h"
21 #include "typedObject.h"
22 #include "config_express.h"
23 
24 // The DCAST (downcast) macro is defined as a convenience for
25 // downcasting from some TypedObject pointer (or a PointerTo). It's
26 // just a normal C++-style downcast, except it first checks get_type()
27 // to make sure the downcasting is safe. If you compile with NDEBUG,
28 // or set verify-dcast to #f, this check is removed.
29 
30 // DCAST will return NULL if the downcasting is unsafe. If you'd
31 // rather it abort out of the function (a la nassertv/nassertr), then
32 // see DCAST_INTO_V and DCAST_INTO_R, below.
33 
34 template<class WantType>
35 INLINE WantType *_dcast(WantType *, TypedObject *ptr);
36 template<class WantType>
37 INLINE const WantType *_dcast(WantType *, const TypedObject *ptr);
38 
39 // Note: it is important that DCAST not repeat the pointer parameter,
40 // since many users of DCAST may want to use the result of a function
41 // as the pointer parameter, and it could be terribly confusing and
42 // difficult to trace if the function were inadvertently executed
43 // twice. This happened!
44 #define DCAST(want_type, pointer) _dcast((want_type*)0, pointer)
45 
46 // DCAST_INTO_V and DCAST_INTO_R are similar in purpose to DCAST,
47 // except they: (a) automatically assign a variable instead of
48 // returning the downcasted pointer, and (b) they immediately return
49 // out of the function if the downcasting fails. DCAST_INTO_V is for
50 // use in a void function and returns nothing; DCAST_INTO_R is for use
51 // in a non-void function and returns the indicated value.
52 
53 // Both DCAST_INTO_V and DCAST_INTO_R accept as the first parameter a
54 // variable of type (want_type *) or (const want_type *), instead of
55 // the name of the type. This variable will be filled with the new
56 // pointer.
57 
58 
59 // _dcast_ref is used to implement DCAST_INTO_V and DCAST_INTO_R. Its
60 // difference from _dcast is that it takes a reference to a pointer as
61 // a first parameter. The main point of this is to shut up the
62 // compiler about pointers used before their value is assigned.
63 template<class WantType>
64 INLINE WantType *_dcast_ref(WantType *&, TypedObject *ptr);
65 template<class WantType>
66 INLINE const WantType *_dcast_ref(WantType *&, const TypedObject *ptr);
67 
68 #ifdef DO_DCAST
69 // _dcast_verify performs the actual verification.
70 EXPCL_PANDAEXPRESS bool
71 _dcast_verify(TypeHandle want_handle, size_t want_size,
72  const TypedObject *ptr);
73 #endif // DO_DCAST
74 
75 #define DCAST_INTO_V(to_pointer, from_pointer) \
76  { \
77  (to_pointer) = _dcast_ref(to_pointer, from_pointer); \
78  nassertv((void *)(to_pointer) != (void *)NULL); \
79  }
80 
81 #define DCAST_INTO_R(to_pointer, from_pointer, return_value) \
82  { \
83  (to_pointer) = _dcast_ref(to_pointer, from_pointer); \
84  nassertr((void *)(to_pointer) != (void *)NULL, return_value); \
85  }
86 
87 #include "dcast.T"
88 
89 #endif
This is an abstract class that all classes which use TypeHandle, and also provide virtual functions t...
Definition: typedObject.h:98
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85