Panda3D

Loading...
Searching...
No Matches
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
WeakPointerCallback
Derive from this class and override the callback() method if you want to get an immediate callback fr...
Definition weakPointerCallback.h:24
WeakReferenceList::add_callback
void add_callback(WeakPointerCallback *callback, void *data)
Adds the callback to the list of callbacks that will be called when the underlying pointer is deleted...
Definition weakReferenceList.cxx:44
WeakReferenceList::was_deleted
bool was_deleted() const
Returns true if the object represented has been deleted, ie.
Definition weakReferenceList.I:43
WeakReferenceList::ref
void ref() const
Increases the number of weak references.
Definition weakReferenceList.I:20
WeakReferenceList::remove_callback
void remove_callback(WeakPointerCallback *callback)
Intended to be called only by WeakPointerTo (or by any class implementing a weak reference-counting p...
Definition weakReferenceList.cxx:70
WeakReferenceList::unref
bool unref() const
Decreases the number of weak references.
Definition weakReferenceList.I:32
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.