Panda3D
|
00001 // Filename: dcast.cxx 00002 // Created by: drose (07Aug01) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "dcast.h" 00016 #include "config_express.h" 00017 00018 #ifdef _WIN32 00019 #define WIN32_LEAN_AND_MEAN 00020 #include <windows.h> // for IsBadWritePtr() 00021 #endif 00022 00023 00024 #ifdef DO_DCAST 00025 //////////////////////////////////////////////////////////////////// 00026 // Function: _dcast_verify 00027 // Description: This function performs the actual check that the 00028 // indicated TypedObject pointer is of the intended 00029 // type. 00030 //////////////////////////////////////////////////////////////////// 00031 bool 00032 _dcast_verify(TypeHandle want_handle, size_t want_size, 00033 const TypedObject *ptr) { 00034 if (get_verify_dcast()) { 00035 if (ptr == (const TypedObject *)NULL) { 00036 // This is allowed these days. It used to be an error, but 00037 // what the heck. 00038 return true; 00039 } 00040 #if defined(_DEBUG) && defined(_WIN32) 00041 if (IsBadWritePtr((TypedObject *)ptr, want_size)) { 00042 express_cat->warning() 00043 << "Attempt to cast invalid pointer to " 00044 << want_handle << "\n"; 00045 return false; 00046 } 00047 #endif 00048 if (!ptr->is_of_type(want_handle)) { 00049 express_cat->error() 00050 << "Attempt to cast pointer from " << ptr->get_type() 00051 << " to " << want_handle << "\n"; 00052 if (ptr->get_type() == TypedObject::get_class_type()) { 00053 express_cat->error(false) 00054 << "Perhaps pointer was inadvertently deleted?\n"; 00055 } 00056 return false; 00057 } 00058 } 00059 00060 return true; 00061 } 00062 #endif // DO_DCAST 00063 00064