Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/putil/globalPointerRegistry.cxx

Go to the documentation of this file.
00001 // Filename: globalPointerRegistry.cxx
00002 // Created by:  drose (03Feb00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "globalPointerRegistry.h"
00020 #include "config_util.h"
00021 
00022 // In general, we use the util_cat->info() syntax in this file
00023 // (instead of util_cat.info()), because much of this work is done at
00024 // static init time, and we must use the arrow syntax to force
00025 // initialization of the util_cat category.
00026 
00027 GlobalPointerRegistry *GlobalPointerRegistry::_global_ptr;
00028 
00029 ////////////////////////////////////////////////////////////////////
00030 //     Function: GlobalPointerRegistry::ns_get_pointer
00031 //       Access: Private
00032 //  Description: Returns the pointer associated with the indicated
00033 //               TypeHandle, if any.  If no pointer has yet been
00034 //               associated, returns NULL.
00035 ////////////////////////////////////////////////////////////////////
00036 void *GlobalPointerRegistry::
00037 ns_get_pointer(TypeHandle type) const {
00038   if (type == TypeHandle::none()) {
00039     util_cat->error()
00040       << "GlobalPointerRegistry::get_pointer() called on empty TypeHandle\n";
00041   }
00042   Pointers::const_iterator pi;
00043   pi = _pointers.find(type);
00044   if (pi == _pointers.end()) {
00045     return (void *)NULL;
00046   }
00047 
00048   return (*pi).second;
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //     Function: GlobalPointerRegistry::ns_store_pointer
00053 //       Access: Private
00054 //  Description: Associates the given pointer with the indicated
00055 //               TypeHandle.  It is an error to call this with a NULL
00056 //               pointer, or to call this function more than once with
00057 //               a given TypeHandle (without first calling
00058 //               clear_pointer).
00059 ////////////////////////////////////////////////////////////////////
00060 void GlobalPointerRegistry::
00061 ns_store_pointer(TypeHandle type, void *ptr) {
00062   if (type == TypeHandle::none()) {
00063     util_cat->error()
00064       << "GlobalPointerRegistry::store_pointer() called on empty TypeHandle\n";
00065   }
00066   if (ptr == (void *)NULL) {
00067     util_cat->error()
00068       << "Invalid attempt to store a NULL pointer for " << type << "\n";
00069     clear_pointer(type);
00070     return;
00071   }
00072   pair<Pointers::iterator, bool> result =
00073     _pointers.insert(Pointers::value_type(type, ptr));
00074 
00075   if (!result.second) {
00076     // There was already a pointer in the map.
00077     if ((*result.first).second == ptr) {
00078       util_cat->error()
00079         << "Invalid attempt to store pointer " << ptr
00080         << " twice for " << type << "\n";
00081     } else {
00082       util_cat->error()
00083         << "Invalid attempt to store additional pointer " << ptr
00084         << " for " << type << "; " << (*result.first).second
00085         << " stored previously.\n";
00086     }
00087   }
00088 }
00089 
00090 ////////////////////////////////////////////////////////////////////
00091 //     Function: GlobalPointerRegistry::ns_clear_pointer
00092 //       Access: Private
00093 //  Description: Removes the association of the given pointer with the
00094 //               indicated TypeHandle.  Subsequent calls to
00095 //               get_pointer() with this TypeHandle will return NULL,
00096 //               until another call to store_pointer() is made.
00097 ////////////////////////////////////////////////////////////////////
00098 void GlobalPointerRegistry::
00099 ns_clear_pointer(TypeHandle type) {
00100   if (type == TypeHandle::none()) {
00101     util_cat->error()
00102       << "GlobalPointerRegistry::clear_pointer() called on empty TypeHandle\n";
00103   }
00104 
00105   // It's not an error to clear_pointer() if it was already cleared.
00106   // Don't bother checking that.
00107   _pointers.erase(type);
00108 }

Generated on Fri May 2 00:43:38 2003 for Panda by doxygen1.3