Panda3D
weakPointerTo.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 weakPointerTo.h
10  * @author drose
11  * @date 2004-09-27
12  */
13 
14 #ifndef WEAKPOINTERTO_H
15 #define WEAKPOINTERTO_H
16 
17 #include "pandabase.h"
18 #include "weakPointerToBase.h"
19 #include "pointerTo.h"
20 
21 /**
22  * WeakPointerTo is similar to PointerTo, except that it does not actually
23  * prevent the referenced pointer from deleting. Instead, the referenced
24  * pointer is allowed to delete, but if this happens then was_deleted() will
25  * return true, and it will be an assertion error to dereference the pointer
26  * thereafter.
27  */
28 template <class T>
29 class WeakPointerTo : public WeakPointerToBase<T> {
30 public:
31  typedef typename WeakPointerToBase<T>::To To;
32 PUBLISHED:
33  constexpr WeakPointerTo() noexcept = default;
34  INLINE WeakPointerTo(To *ptr);
35  INLINE WeakPointerTo(const PointerTo<T> &copy);
36  INLINE WeakPointerTo(const WeakPointerTo<T> &copy);
37 
38 public:
39  INLINE WeakPointerTo(WeakPointerTo<T> &&from) noexcept;
40 
41  template<class Y>
42  ALWAYS_INLINE WeakPointerTo(const WeakPointerTo<Y> &r) noexcept;
43  template<class Y>
44  ALWAYS_INLINE WeakPointerTo(const PointerTo<Y> &r) noexcept;
45  template<class Y>
46  ALWAYS_INLINE WeakPointerTo(WeakPointerTo<Y> &&r) noexcept;
47 
48  INLINE To &operator *() const;
49  INLINE To *operator -> () const;
50  // MSVC.NET 2005 insists that we use T *, and not To *, here.
51  INLINE explicit operator T *() const;
52 
53 PUBLISHED:
54  INLINE PointerTo<T> lock() const;
55  INLINE To *p() const;
56  INLINE To *get_orig() const;
57 
58  INLINE WeakPointerTo<T> &operator = (To *ptr);
59  INLINE WeakPointerTo<T> &operator = (const PointerTo<T> &copy);
60  INLINE WeakPointerTo<T> &operator = (const WeakPointerTo<T> &copy);
61 
62 public:
63  INLINE WeakPointerTo<T> &operator = (WeakPointerTo<T> &&from) noexcept;
64 
65  template<class Y>
66  ALWAYS_INLINE WeakPointerTo<T> &operator = (const WeakPointerTo<Y> &r) noexcept;
67  template<class Y>
68  ALWAYS_INLINE WeakPointerTo<T> &operator = (const PointerTo<Y> &r) noexcept;
69  template<class Y>
70  ALWAYS_INLINE WeakPointerTo<T> &operator = (WeakPointerTo<Y> &&r) noexcept;
71 
72 PUBLISHED:
73  // This function normally wouldn't need to be redefined here, but we do so
74  // anyway just to help out interrogate (which doesn't seem to want to
75  // automatically export the WeakPointerToBase class). When this works again
76  // in interrogate, we can remove this.
77  INLINE void clear() { WeakPointerToBase<T>::clear(); }
78 };
79 
80 
81 /**
82  * A WeakConstPointerTo is similar to a WeakPointerTo, except it keeps a const
83  * pointer to the thing, that will be cleared to NULL when the thing deleted.
84  */
85 template <class T>
87 public:
88  typedef typename WeakPointerToBase<T>::To To;
89 PUBLISHED:
90  constexpr WeakConstPointerTo() noexcept = default;
91  INLINE WeakConstPointerTo(const To *ptr);
92  INLINE WeakConstPointerTo(const PointerTo<T> &copy);
93  INLINE WeakConstPointerTo(const ConstPointerTo<T> &copy);
94  INLINE WeakConstPointerTo(const WeakPointerTo<T> &copy);
95  INLINE WeakConstPointerTo(const WeakConstPointerTo<T> &copy);
96 
97 public:
98  INLINE WeakConstPointerTo(WeakPointerTo<T> &&from) noexcept;
99  INLINE WeakConstPointerTo(WeakConstPointerTo<T> &&from) noexcept;
100 
101  template<class Y>
102  ALWAYS_INLINE WeakConstPointerTo(const WeakPointerTo<Y> &r) noexcept;
103  template<class Y>
104  ALWAYS_INLINE WeakConstPointerTo(const WeakConstPointerTo<Y> &r) noexcept;
105  template<class Y>
106  ALWAYS_INLINE WeakConstPointerTo(const PointerTo<Y> &r) noexcept;
107  template<class Y>
108  ALWAYS_INLINE WeakConstPointerTo(const ConstPointerTo<Y> &r) noexcept;
109  template<class Y>
110  ALWAYS_INLINE WeakConstPointerTo(WeakPointerTo<Y> &&r) noexcept;
111  template<class Y>
112  ALWAYS_INLINE WeakConstPointerTo(WeakConstPointerTo<Y> &&r) noexcept;
113 
114  INLINE const To &operator *() const;
115  INLINE const To *operator -> () const;
116  INLINE explicit operator const T *() const;
117 
118 PUBLISHED:
119  INLINE ConstPointerTo<T> lock() const;
120  INLINE const To *p() const;
121  INLINE const To *get_orig() const;
122 
123  INLINE WeakConstPointerTo<T> &operator = (const To *ptr);
124  INLINE WeakConstPointerTo<T> &operator = (const PointerTo<T> &copy);
125  INLINE WeakConstPointerTo<T> &operator = (const ConstPointerTo<T> &copy);
126  INLINE WeakConstPointerTo<T> &operator = (const WeakPointerTo<T> &copy);
127  INLINE WeakConstPointerTo<T> &operator = (const WeakConstPointerTo<T> &copy);
128 
129 public:
130  INLINE WeakConstPointerTo<T> &operator = (WeakPointerTo<T> &&from) noexcept;
131  INLINE WeakConstPointerTo<T> &operator = (WeakConstPointerTo<T> &&from) noexcept;
132 
133  template<class Y>
134  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (const WeakPointerTo<Y> &r) noexcept;
135  template<class Y>
136  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (const WeakConstPointerTo<Y> &r) noexcept;
137  template<class Y>
138  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (const PointerTo<Y> &r) noexcept;
139  template<class Y>
140  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (const ConstPointerTo<Y> &r) noexcept;
141  template<class Y>
142  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (WeakPointerTo<Y> &&r) noexcept;
143  template<class Y>
144  ALWAYS_INLINE WeakConstPointerTo<T> &operator = (WeakConstPointerTo<Y> &&r) noexcept;
145 
146 PUBLISHED:
147  // These functions normally wouldn't need to be redefined here, but we do so
148  // anyway just to help out interrogate (which doesn't seem to want to
149  // automatically export the WeakPointerToBase class). When this works again
150  // in interrogate, we can remove these.
151  INLINE bool is_null() const { return WeakPointerToBase<T>::is_null(); }
152  INLINE void clear() { WeakPointerToBase<T>::clear(); }
153 };
154 
155 // Provide specializations of std::owner_less, for using a WPT as a map key.
156 namespace std {
157  template<class T>
158  struct owner_less<WeakPointerTo<T> > {
159  bool operator () (const WeakPointerTo<T> &lhs,
160  const WeakPointerTo<T> &rhs) const noexcept {
161  return lhs.owner_before(rhs);
162  }
163  };
164 
165  template<class T>
166  struct owner_less<WeakConstPointerTo<T> > {
167  bool operator () (const WeakConstPointerTo<T> &lhs,
168  const WeakConstPointerTo<T> &rhs) const noexcept {
169  return lhs.owner_before(rhs);
170  }
171  };
172 }
173 
174 #define WPT(type) WeakPointerTo< type >
175 #define WCPT(type) WeakConstPointerTo< type >
176 
177 #include "weakPointerTo.I"
178 
179 #endif
const To * get_orig() const
Returns the original pointer value, even if the object has since been deleted.
To * get_orig() const
Returns the original pointer value, even if the object has since been deleted.
bool owner_before(const WeakPointerToBase< Y > &other) const noexcept
Defines an ordering that is guaranteed to remain consistent even after the weak pointers have expired...
WeakPointerTo is similar to PointerTo, except that it does not actually prevent the referenced pointe...
Definition: weakPointerTo.h:29
void clear()
A convenient way to set the PointerTo object to NULL.
constexpr bool is_null() const
Returns true if the PointerTo is a NULL pointer, false otherwise.
Definition: pointerToVoid.I:27
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
To * p() const
Returns an ordinary pointer instead of a WeakPointerTo.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
const To * p() const
Returns an ordinary pointer instead of a WeakConstPointerTo.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PointerTo< T > lock() const
A thread-safe way to access the underlying pointer; will silently return null if the underlying point...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A WeakConstPointerTo is similar to a WeakPointerTo, except it keeps a const pointer to the thing,...
Definition: weakPointerTo.h:86
A ConstPointerTo is similar to a PointerTo, except it keeps a const pointer to the thing.
Definition: pointerTo.h:144
This file defines the classes PointerTo and ConstPointerTo (and their abbreviations,...
Definition: pointerTo.h:69
This is the base class for PointerTo and ConstPointerTo.
ConstPointerTo< T > lock() const
A thread-safe way to access the underlying pointer; will silently return null if the underlying point...