Panda3D
 All Classes Functions Variables Enumerations
dcast.cxx
1 // Filename: dcast.cxx
2 // Created by: drose (07Aug01)
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 #include "dcast.h"
16 #include "config_express.h"
17 
18 #ifdef _WIN32
19 #ifndef WIN32_LEAN_AND_MEAN
20 #define WIN32_LEAN_AND_MEAN 1
21 #endif
22 #include <windows.h> // for IsBadWritePtr()
23 #endif
24 
25 #ifdef DO_DCAST
26 ////////////////////////////////////////////////////////////////////
27 // Function: _dcast_verify
28 // Description: This function performs the actual check that the
29 // indicated TypedObject pointer is of the intended
30 // type.
31 ////////////////////////////////////////////////////////////////////
32 bool
33 _dcast_verify(TypeHandle want_handle, size_t want_size,
34  const TypedObject *ptr) {
35  if (get_verify_dcast()) {
36  if (ptr == (const TypedObject *)NULL) {
37  // This is allowed these days. It used to be an error, but
38  // what the heck.
39  return true;
40  }
41 #if defined(_DEBUG) && defined(_WIN32)
42  if (IsBadWritePtr((TypedObject *)ptr, want_size)) {
43  express_cat->warning()
44  << "Attempt to cast invalid pointer to "
45  << want_handle << "\n";
46  return false;
47  }
48 #endif
49  if (!ptr->is_of_type(want_handle)) {
50  express_cat->error()
51  << "Attempt to cast pointer from " << ptr->get_type()
52  << " to " << want_handle << "\n";
53  if (ptr->get_type() == TypedObject::get_class_type()) {
54  express_cat->error(false)
55  << "Perhaps pointer was inadvertently deleted?\n";
56  }
57  return false;
58  }
59  }
60 
61  return true;
62 }
63 #endif // DO_DCAST
64 
65 
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:63
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