Panda3D

weakReferenceList.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 weakReferenceList.h
10 * @author drose
11 * @date 2004-09-27
12 */
13
14 #ifndef WEAKREFERENCELIST_H
15 #define WEAKREFERENCELIST_H
16
17 #include "pandabase.h"
18 #include "pmap.h"
19 #include "mutexImpl.h"
20
21 class WeakPointerCallback;
22
23 /**
24 * This is an object shared by all the weak pointers that point to the same
25 * ReferenceCount object. It is created whenever a weak reference to an
26 * object is created, and can outlive the object until all weak references
27 * have disappeared.
28 */
29 class EXPCL_PANDA_EXPRESS WeakReferenceList {
30 public:
31 WeakReferenceList();
32 ~WeakReferenceList();
33
34 INLINE void ref() const;
35 INLINE bool unref() const;
36 INLINE bool was_deleted() const;
37
38 void add_callback(WeakPointerCallback *callback, void *data);
39 void remove_callback(WeakPointerCallback *callback);
40
41 private:
42 void mark_deleted();
43
44 public:
45 // This lock protects the callbacks below, but it also protects the object
46 // from being deleted during a call to WeakPointerTo::lock().
47 MutexImpl _lock;
48
49 private:
50 typedef pmap<WeakPointerCallback *, void *> Callbacks;
51 Callbacks _callbacks;
52
53 // This has a very large number added to it if the object is still alive.
54 // It could be 1, but having it be a large number makes it easy to check
55 // whether the object has been deleted or not.
56 static const AtomicAdjust::Integer _alive_offset = (1 << 30);
57 mutable AtomicAdjust::Integer _count;
58
59 friend class ReferenceCount;
60 };
61
62 #include "weakReferenceList.I"
63
64 #endif
MutexDummyImpl
A fake mutex implementation for single-threaded applications that don't need any synchronization cont...
Definition: mutexDummyImpl.h:24
ReferenceCount
A base class for all things that want to be reference-counted.
Definition: referenceCount.h:38
WeakPointerCallback
Derive from this class and override the callback() method if you want to get an immediate callback fr...
Definition: weakPointerCallback.h:24
WeakReferenceList
This is an object shared by all the weak pointers that point to the same ReferenceCount object.
Definition: weakReferenceList.h:29
pmap< WeakPointerCallback *, void * >
mutexImpl.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pandabase.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
pmap.h
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
weakReferenceList.I
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.