00001 // Filename: globalPointerRegistry.cxx 00002 // Created by: drose (03Feb00) 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 "globalPointerRegistry.h" 00016 #include "config_util.h" 00017 00018 // In general, we use the util_cat->info() syntax in this file 00019 // (instead of util_cat.info()), because much of this work is done at 00020 // static init time, and we must use the arrow syntax to force 00021 // initialization of the util_cat category. 00022 00023 GlobalPointerRegistry *GlobalPointerRegistry::_global_ptr; 00024 00025 //////////////////////////////////////////////////////////////////// 00026 // Function: GlobalPointerRegistry::ns_get_pointer 00027 // Access: Private 00028 // Description: Returns the pointer associated with the indicated 00029 // TypeHandle, if any. If no pointer has yet been 00030 // associated, returns NULL. 00031 //////////////////////////////////////////////////////////////////// 00032 void *GlobalPointerRegistry:: 00033 ns_get_pointer(TypeHandle type) const { 00034 if (type == TypeHandle::none()) { 00035 util_cat->error() 00036 << "GlobalPointerRegistry::get_pointer() called on empty TypeHandle\n"; 00037 } 00038 Pointers::const_iterator pi; 00039 pi = _pointers.find(type); 00040 if (pi == _pointers.end()) { 00041 return (void *)NULL; 00042 } 00043 00044 return (*pi).second; 00045 } 00046 00047 //////////////////////////////////////////////////////////////////// 00048 // Function: GlobalPointerRegistry::ns_store_pointer 00049 // Access: Private 00050 // Description: Associates the given pointer with the indicated 00051 // TypeHandle. It is an error to call this with a NULL 00052 // pointer, or to call this function more than once with 00053 // a given TypeHandle (without first calling 00054 // clear_pointer). 00055 //////////////////////////////////////////////////////////////////// 00056 void GlobalPointerRegistry:: 00057 ns_store_pointer(TypeHandle type, void *ptr) { 00058 if (type == TypeHandle::none()) { 00059 util_cat->error() 00060 << "GlobalPointerRegistry::store_pointer() called on empty TypeHandle\n"; 00061 } 00062 if (ptr == (void *)NULL) { 00063 util_cat->error() 00064 << "Invalid attempt to store a NULL pointer for " << type << "\n"; 00065 clear_pointer(type); 00066 return; 00067 } 00068 pair<Pointers::iterator, bool> result = 00069 _pointers.insert(Pointers::value_type(type, ptr)); 00070 00071 if (!result.second) { 00072 // There was already a pointer in the map. 00073 if ((*result.first).second == ptr) { 00074 util_cat->error() 00075 << "Invalid attempt to store pointer " << ptr 00076 << " twice for " << type << "\n"; 00077 } else { 00078 util_cat->error() 00079 << "Invalid attempt to store additional pointer " << ptr 00080 << " for " << type << "; " << (*result.first).second 00081 << " stored previously.\n"; 00082 } 00083 } 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: GlobalPointerRegistry::ns_clear_pointer 00088 // Access: Private 00089 // Description: Removes the association of the given pointer with the 00090 // indicated TypeHandle. Subsequent calls to 00091 // get_pointer() with this TypeHandle will return NULL, 00092 // until another call to store_pointer() is made. 00093 //////////////////////////////////////////////////////////////////// 00094 void GlobalPointerRegistry:: 00095 ns_clear_pointer(TypeHandle type) { 00096 if (type == TypeHandle::none()) { 00097 util_cat->error() 00098 << "GlobalPointerRegistry::clear_pointer() called on empty TypeHandle\n"; 00099 } 00100 00101 // It's not an error to clear_pointer() if it was already cleared. 00102 // Don't bother checking that. 00103 _pointers.erase(type); 00104 }