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