Panda3D
 All Classes Functions Variables Enumerations
globalPointerRegistry.cxx
1 // Filename: globalPointerRegistry.cxx
2 // Created by: drose (03Feb00)
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 "globalPointerRegistry.h"
16 #include "config_util.h"
17 
18 // In general, we use the util_cat->info() syntax in this file
19 // (instead of util_cat.info()), because much of this work is done at
20 // static init time, and we must use the arrow syntax to force
21 // initialization of the util_cat category.
22 
23 GlobalPointerRegistry *GlobalPointerRegistry::_global_ptr;
24 
25 ////////////////////////////////////////////////////////////////////
26 // Function: GlobalPointerRegistry::ns_get_pointer
27 // Access: Private
28 // Description: Returns the pointer associated with the indicated
29 // TypeHandle, if any. If no pointer has yet been
30 // associated, returns NULL.
31 ////////////////////////////////////////////////////////////////////
32 void *GlobalPointerRegistry::
33 ns_get_pointer(TypeHandle type) const {
34  if (type == TypeHandle::none()) {
35  util_cat->error()
36  << "GlobalPointerRegistry::get_pointer() called on empty TypeHandle\n";
37  }
38  Pointers::const_iterator pi;
39  pi = _pointers.find(type);
40  if (pi == _pointers.end()) {
41  return (void *)NULL;
42  }
43 
44  return (*pi).second;
45 }
46 
47 ////////////////////////////////////////////////////////////////////
48 // Function: GlobalPointerRegistry::ns_store_pointer
49 // Access: Private
50 // Description: Associates the given pointer with the indicated
51 // TypeHandle. It is an error to call this with a NULL
52 // pointer, or to call this function more than once with
53 // a given TypeHandle (without first calling
54 // clear_pointer).
55 ////////////////////////////////////////////////////////////////////
56 void GlobalPointerRegistry::
57 ns_store_pointer(TypeHandle type, void *ptr) {
58  if (type == TypeHandle::none()) {
59  util_cat->error()
60  << "GlobalPointerRegistry::store_pointer() called on empty TypeHandle\n";
61  }
62  if (ptr == (void *)NULL) {
63  util_cat->error()
64  << "Invalid attempt to store a NULL pointer for " << type << "\n";
65  clear_pointer(type);
66  return;
67  }
68  pair<Pointers::iterator, bool> result =
69  _pointers.insert(Pointers::value_type(type, ptr));
70 
71  if (!result.second) {
72  // There was already a pointer in the map.
73  if ((*result.first).second == ptr) {
74  util_cat->error()
75  << "Invalid attempt to store pointer " << ptr
76  << " twice for " << type << "\n";
77  } else {
78  util_cat->error()
79  << "Invalid attempt to store additional pointer " << ptr
80  << " for " << type << "; " << (*result.first).second
81  << " stored previously.\n";
82  }
83  }
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: GlobalPointerRegistry::ns_clear_pointer
88 // Access: Private
89 // Description: Removes the association of the given pointer with the
90 // indicated TypeHandle. Subsequent calls to
91 // get_pointer() with this TypeHandle will return NULL,
92 // until another call to store_pointer() is made.
93 ////////////////////////////////////////////////////////////////////
94 void GlobalPointerRegistry::
95 ns_clear_pointer(TypeHandle type) {
96  if (type == TypeHandle::none()) {
97  util_cat->error()
98  << "GlobalPointerRegistry::clear_pointer() called on empty TypeHandle\n";
99  }
100 
101  // It's not an error to clear_pointer() if it was already cleared.
102  // Don't bother checking that.
103  _pointers.erase(type);
104 }
static TypeHandle none()
Returns a special zero-valued TypeHandle that is used to indicate no type.
Definition: typeHandle.I:274
static void clear_pointer(TypeHandle type)
Removes the association of the given pointer with the indicated TypeHandle.
This class maintains a one-to-one mapping from TypeHandle to a void * pointer.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85