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