Panda3D

threadSafePointerTo.h

00001 // Filename: threadSafePointerTo.h
00002 // Created by:  drose (28Apr06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef THREADSAFEPOINTERTO_H
00016 #define THREADSAFEPOINTERTO_H
00017 
00018 #include "pandabase.h"
00019 #include "threadSafePointerToBase.h"
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //       Class : ThreadSafePointerTo
00023 // Description : This works exactly like PointerTo, except that the
00024 //               object is designed to be thread-safe: it is generally
00025 //               safe to make unprotected assignments to this pointer,
00026 //               in the sense that the last assignment will win and
00027 //               the reference counts will be properly maintained.
00028 ////////////////////////////////////////////////////////////////////
00029 template <class T>
00030 class ThreadSafePointerTo : public ThreadSafePointerToBase<T> {
00031 public:
00032   typedef TYPENAME ThreadSafePointerToBase<T>::To To;
00033 PUBLISHED:
00034   INLINE ThreadSafePointerTo(To *ptr = (To *)NULL);
00035   INLINE ThreadSafePointerTo(const ThreadSafePointerTo<T> &copy);
00036   INLINE ~ThreadSafePointerTo();
00037 
00038 public:
00039   INLINE To &operator *() const;
00040   INLINE To *operator -> () const;
00041   // MSVC.NET 2005 insists that we use T *, and not To *, here.
00042   INLINE operator T *() const;
00043 
00044 PUBLISHED:
00045   // When downcasting to a derived class from a ThreadSafePointerTo<BaseClass>,
00046   // C++ would normally require you to cast twice: once to an actual
00047   // BaseClass pointer, and then again to your desired pointer.  You
00048   // can use the handy function p() to avoid this first cast and make
00049   // your code look a bit cleaner.
00050 
00051   // e.g. instead of (MyType *)(BaseClass *)ptr, use (MyType *)ptr.p()
00052 
00053   // If your base class is a derivative of TypedObject, you might want
00054   // to use the DCAST macro defined in typedObject.h instead,
00055   // e.g. DCAST(MyType, ptr).  This provides a clean downcast that
00056   // doesn't require .p() or any double-casting, and it can be
00057   // run-time checked for correctness.
00058   INLINE To *p() const;
00059 
00060   INLINE ThreadSafePointerTo<T> &operator = (To *ptr);
00061   INLINE ThreadSafePointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
00062 
00063   // These functions normally wouldn't need to be redefined here, but
00064   // we do so anyway just to help out interrogate (which doesn't seem
00065   // to want to automatically export the ThreadSafePointerToBase class).  When
00066   // this works again in interrogate, we can remove these.
00067   INLINE bool is_null() const { return ThreadSafePointerToBase<T>::is_null(); }
00068   INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
00069 };
00070 
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //       Class : ThreadSafeConstPointerTo
00074 // Description : 
00075 ////////////////////////////////////////////////////////////////////
00076 template <class T>
00077 class ThreadSafeConstPointerTo : public ThreadSafePointerToBase<T> {
00078 public:
00079   typedef TYPENAME ThreadSafePointerToBase<T>::To To;
00080 PUBLISHED:
00081   INLINE ThreadSafeConstPointerTo(const To *ptr = (const To *)NULL);
00082   INLINE ThreadSafeConstPointerTo(const ThreadSafePointerTo<T> &copy);
00083   INLINE ThreadSafeConstPointerTo(const ThreadSafeConstPointerTo<T> &copy);
00084   INLINE ~ThreadSafeConstPointerTo();
00085 
00086 public:
00087   INLINE const To &operator *() const;
00088   INLINE const To *operator -> () const;
00089   INLINE operator const T *() const;
00090 
00091 PUBLISHED:
00092   INLINE const To *p() const;
00093 
00094   INLINE ThreadSafeConstPointerTo<T> &operator = (const To *ptr);
00095   INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafePointerTo<T> &copy);
00096   INLINE ThreadSafeConstPointerTo<T> &operator = (const ThreadSafeConstPointerTo<T> &copy);
00097 
00098   // This functions normally wouldn't need to be redefined here, but
00099   // we do so anyway just to help out interrogate (which doesn't seem
00100   // to want to automatically export the ThreadSafePointerToBase class).  When
00101   // this works again in interrogate, we can remove this.
00102   INLINE void clear() { ThreadSafePointerToBase<T>::clear(); }
00103 };
00104 
00105 #define TSPT(type) ThreadSafePointerTo< type >
00106 #define TSCPT(type) ThreadSafeConstPointerTo< type >
00107 
00108 #include "threadSafePointerTo.I"
00109 
00110 #endif
 All Classes Functions Variables Enumerations