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