threadSafePointerTo.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Filename: threadSafePointerTo.h
  2. // Created by: drose (28Apr06)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #ifndef THREADSAFEPOINTERTO_H
  19. #define THREADSAFEPOINTERTO_H
  20. #include "pandabase.h"
  21. #include "threadSafePointerToBase.h"
  22. ////////////////////////////////////////////////////////////////////
  23. // Class : ThreadSafePointerTo
  24. // Description : This works exactly like PointerTo, except that the
  25. // object is designed to be thread-safe: it is generally
  26. // safe to make unprotected assignments to this pointer,
  27. // in the sense that the last assignment will win and
  28. // the reference counts will be properly maintained.
  29. ////////////////////////////////////////////////////////////////////
  30. template <class T>
  31. class ThreadSafePointerTo : public ThreadSafePointerToBase<T> {
  32. public:
  33. typedef TYPENAME ThreadSafePointerToBase<T>::To To;
  34. PUBLISHED:
  35. INLINE ThreadSafePointerTo(To *ptr = (To *)NULL);
  36. INLINE ThreadSafePointerTo(const ThreadSafePointerTo<T> &copy);
  37. INLINE ~ThreadSafePointerTo();
  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. PUBLISHED:
  44. // When downcasting to a derived class from a ThreadSafePointerTo<BaseClass>,
  45. // C++ would normally require you to cast twice: once to an actual
  46. // BaseClass pointer, and then again to your desired pointer. You
  47. // can use the handy function p() to avoid this first cast and make
  48. // your code look a bit cleaner.
  49. // e.g. instead of (MyType *)(BaseClass *)ptr, use (MyType *)ptr.p()
  50. // If your base class is a derivative of TypedObject, you might want
  51. // to use the DCAST macro defined in typedObject.h instead,
  52. // e.g. DCAST(MyType, ptr). This provides a clean downcast that
  53. // doesn't require .p() or any double-casting, and it can be
  54. // run-time checked for correctness.
  55. INLINE To *p() const;
  56. INLINE ThreadSafePointerTo<T> &operator = (To *ptr);
  57. INLINE ThreadSafePointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
  58. // These functions normally wouldn't need to be redefined here, but
  59. // we do so anyway just to help out interrogate (which doesn't seem
  60. // to want to automatically export the ThreadSafePointerToBase class). When
  61. // this works again in interrogate, we can remove these.
  62. INLINE bool is_null() const { return ThreadSafePointerToBase<T>::is_null(); }
  63. INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
  64. };
  65. ////////////////////////////////////////////////////////////////////
  66. // Class : ThreadSafeConstPointerTo
  67. // Description :
  68. ////////////////////////////////////////////////////////////////////
  69. template <class T>
  70. class ThreadSafeConstPointerTo : public ThreadSafePointerToBase<T> {
  71. public:
  72. typedef TYPENAME ThreadSafePointerToBase<T>::To To;
  73. PUBLISHED:
  74. INLINE ThreadSafeConstPointerTo(const To *ptr = (const To *)NULL);
  75. INLINE ThreadSafeConstPointerTo(const ThreadSafePointerTo<T> &copy);
  76. INLINE ThreadSafeConstPointerTo(const ThreadSafeConstPointerTo<T> &copy);
  77. INLINE ~ThreadSafeConstPointerTo();
  78. public:
  79. INLINE const To &operator *() const;
  80. INLINE const To *operator -> () const;
  81. INLINE operator const T *() const;
  82. PUBLISHED:
  83. INLINE const To *p() const;
  84. INLINE ThreadSafeConstPointerTo<T> &operator = (const To *ptr);
  85. INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
  86. INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafeConstPointerTo<T> &copy);
  87. // This functions normally wouldn't need to be redefined here, but
  88. // we do so anyway just to help out interrogate (which doesn't seem
  89. // to want to automatically export the ThreadSafePointerToBase class). When
  90. // this works again in interrogate, we can remove this.
  91. INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
  92. };
  93. #define TSPT(type) ThreadSafePointerTo< type >
  94. #define TSCPT(type) ThreadSafeConstPointerTo< type >
  95. #include "threadSafePointerTo.I"
  96. #endif