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