Panda3D
globalPointerRegistry.h
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.h
10  * @author drose
11  * @date 2000-02-03
12  */
13 
14 #ifndef GLOBALPOINTERREGISTRY_H
15 #define GLOBALPOINTERREGISTRY_H
16 
17 #include "pandabase.h"
18 
19 #include "typedObject.h"
20 
21 #include "pmap.h"
22 
23 /**
24  * This class maintains a one-to-one mapping from TypeHandle to a void *
25  * pointer. Its purpose is to store a pointer to some class data for a given
26  * class.
27  *
28  * Normally, one would simply use a static data member to store class data.
29  * However, when the static data is associated with a template class, the
30  * dynamic loader may have difficulty in properly resolving the statics.
31  *
32  * Consider: class foo<int> defines a static member, _a. There should be only
33  * one instance of _a shared between all instances of foo<int>, and there will
34  * be a different instance of _a shared between all instances of foo<float>.
35  *
36  * Now suppose that two different shared libraries instantiate foo<int>. In
37  * each .so, there exists a different foo<int>::_a. It is the loader's job to
38  * recognize this and collapse them together when both libraries are loaded.
39  * This usually works, but sometimes it doesn't, and you end up with two
40  * different instances of foo<int>::_a; some functions see one instance, while
41  * others see the other. We have particularly seen this problem occur under
42  * Linux with gcc.
43  *
44  * This class attempts to circumvent the problem by managing pointers to data
45  * based on TypeHandle. Since the TypeHandle will already be unique based on
46  * the string name supplied to the init_type() function, it can be used to
47  * differentiate foo<int> from foo<float>, while allowing different instances
48  * of foo<int> to guarantee that they share the same static data.
49  */
50 class EXPCL_PANDA_PUTIL GlobalPointerRegistry {
51 public:
52  INLINE static void *get_pointer(TypeHandle type);
53  INLINE static void store_pointer(TypeHandle type, void *ptr);
54  INLINE static void clear_pointer(TypeHandle type);
55 
56 private:
57  // Nonstatic implementations of the above functions.
58  void *ns_get_pointer(TypeHandle type) const;
59  void ns_store_pointer(TypeHandle type, void *ptr);
60  void ns_clear_pointer(TypeHandle type);
61 
62  INLINE static GlobalPointerRegistry *get_global_ptr();
63  static GlobalPointerRegistry *_global_ptr;
64 
65 private:
66  typedef phash_map<TypeHandle, void *> Pointers;
67  Pointers _pointers;
68 };
69 
70 #include "globalPointerRegistry.I"
71 
72 #endif
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
globalPointerRegistry.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
TypeHandle
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
typedObject.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
GlobalPointerRegistry
This class maintains a one-to-one mapping from TypeHandle to a void * pointer.
Definition: globalPointerRegistry.h:50