Panda3D
threadSafePointerTo.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 threadSafePointerTo.h
10  * @author drose
11  * @date 2006-04-28
12  */
13 
14 #ifndef THREADSAFEPOINTERTO_H
15 #define THREADSAFEPOINTERTO_H
16 
17 #include "pandabase.h"
19 
20 /**
21  * This works exactly like PointerTo, except that the object is designed to be
22  * thread-safe: it is generally safe to make unprotected assignments to this
23  * pointer, in the sense that the last assignment will win and the reference
24  * counts will be properly maintained.
25  */
26 template <class T>
28 public:
29  typedef typename ThreadSafePointerToBase<T>::To To;
30 PUBLISHED:
31  INLINE ThreadSafePointerTo(To *ptr = nullptr);
32  INLINE ThreadSafePointerTo(const ThreadSafePointerTo<T> &copy);
33  INLINE ~ThreadSafePointerTo();
34 
35 public:
36  INLINE To &operator *() const;
37  INLINE To *operator -> () const;
38  // MSVC.NET 2005 insists that we use T *, and not To *, here.
39  INLINE operator T *() const;
40 
41 PUBLISHED:
42  // When downcasting to a derived class from a
43  // ThreadSafePointerTo<BaseClass>, C++ would normally require you to cast
44  // twice: once to an actual BaseClass pointer, and then again to your
45  // desired pointer. You can use the handy function p() to avoid this first
46  // cast and make your code look a bit cleaner.
47 
48  // e.g. instead of (MyType *)(BaseClass *)ptr, use (MyType *)ptr.p()
49 
50  // If your base class is a derivative of TypedObject, you might want to use
51  // the DCAST macro defined in typedObject.h instead, e.g. DCAST(MyType,
52  // ptr). This provides a clean downcast that doesn't require .p() or any
53  // double-casting, and it can be run-time checked for correctness.
54  INLINE To *p() const;
55 
56  INLINE ThreadSafePointerTo<T> &operator = (To *ptr);
57  INLINE ThreadSafePointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
58 
59  // These functions normally wouldn't need to be redefined here, but we do so
60  // anyway just to help out interrogate (which doesn't seem to want to
61  // automatically export the ThreadSafePointerToBase class). When this works
62  // again in interrogate, we can remove these.
63  INLINE bool is_null() const { return ThreadSafePointerToBase<T>::is_null(); }
64  INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
65 };
66 
67 
68 /**
69  *
70  */
71 template <class T>
73 public:
74  typedef typename ThreadSafePointerToBase<T>::To To;
75 PUBLISHED:
76  INLINE ThreadSafeConstPointerTo(const To *ptr = nullptr);
79  INLINE ~ThreadSafeConstPointerTo();
80 
81 public:
82  INLINE const To &operator *() const;
83  INLINE const To *operator -> () const;
84  INLINE operator const T *() const;
85 
86 PUBLISHED:
87  INLINE const To *p() const;
88 
89  INLINE ThreadSafeConstPointerTo<T> &operator = (const To *ptr);
90  INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
91  INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafeConstPointerTo<T> &copy);
92 
93  // This functions normally wouldn't need to be redefined here, but we do so
94  // anyway just to help out interrogate (which doesn't seem to want to
95  // automatically export the ThreadSafePointerToBase class). When this works
96  // again in interrogate, we can remove this.
97  INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
98 };
99 
100 #define TSPT(type) ThreadSafePointerTo< type >
101 #define TSCPT(type) ThreadSafeConstPointerTo< type >
102 
103 #include "threadSafePointerTo.I"
104 
105 #endif
constexpr bool is_null() const
Returns true if the PointerTo is a NULL pointer, false otherwise.
Definition: pointerToVoid.I:27
const To * p() const
Returns an ordinary pointer instead of a ThreadSafeConstPointerTo.
This is the base class for ThreadSafePointerTo and ThreadSafeConstPointerTo.
void clear()
A convenient way to set the ThreadSafePointerTo object to NULL.
This works exactly like PointerTo, except that the object is designed to be thread-safe: it is genera...
To * p() const
Returns an ordinary pointer instead of a ThreadSafePointerTo.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.