Panda3D
 All Classes Functions Variables Enumerations
copyOnWritePointer.h
00001 // Filename: copyOnWritePointer.h
00002 // Created by:  drose (09Apr07)
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 COPYONWRITEPOINTER_H
00016 #define COPYONWRITEPOINTER_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "copyOnWriteObject.h"
00021 #include "pointerTo.h"
00022 #include "dcast.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //       Class : CopyOnWritePointer
00026 // Description : This safely stores the primary, owned pointer to a
00027 //               CopyOnWriteObject.  At any time, you may call
00028 //               get_read_pointer() or get_write_pointer() to get a
00029 //               read-only or modifiable pointer to the object stored.
00030 //
00031 //               There may be multiple copies of a CopyOnWritePointer
00032 //               which all refer to the same shared object.  They will
00033 //               negotiate with each other properly.
00034 ////////////////////////////////////////////////////////////////////
00035 class EXPCL_PANDA_PUTIL CopyOnWritePointer {
00036 public:
00037   INLINE CopyOnWritePointer(CopyOnWriteObject *object = NULL);
00038   INLINE CopyOnWritePointer(const CopyOnWritePointer &copy);
00039   INLINE void operator = (const CopyOnWritePointer &copy);
00040   INLINE void operator = (CopyOnWriteObject *object);
00041   INLINE ~CopyOnWritePointer();
00042 
00043   INLINE bool operator == (const CopyOnWritePointer &other) const;
00044   INLINE bool operator != (const CopyOnWritePointer &other) const;
00045   INLINE bool operator < (const CopyOnWritePointer &other) const;
00046 
00047 #ifdef COW_THREADED
00048   CPT(CopyOnWriteObject) get_read_pointer() const;
00049   PT(CopyOnWriteObject) get_write_pointer();
00050 #else
00051   INLINE const CopyOnWriteObject *get_read_pointer() const;
00052   INLINE CopyOnWriteObject *get_write_pointer();
00053 #endif  // COW_THREADED
00054 
00055   INLINE CopyOnWriteObject *get_unsafe_pointer();
00056 
00057   INLINE bool is_null() const;
00058   INLINE void clear();
00059 
00060   INLINE bool test_ref_count_integrity() const;
00061   INLINE bool test_ref_count_nonzero() const;
00062 
00063 private:
00064   CopyOnWriteObject *_object;
00065 };
00066 
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //       Class : CopyOnWritePointerTo
00070 // Description : A template wrapper around the above class, mainly to
00071 //               handle the little typecasting niceties.
00072 ////////////////////////////////////////////////////////////////////
00073 template <class T>
00074 class CopyOnWritePointerTo : public CopyOnWritePointer {
00075 public:
00076   // By hiding this template from interrogate, we improve compile-time
00077   // speed and memory utilization.
00078 #ifndef CPPPARSER
00079   typedef T To;
00080 
00081   INLINE CopyOnWritePointerTo(To *object = NULL);
00082   INLINE CopyOnWritePointerTo(const CopyOnWritePointerTo<T> &copy);
00083   INLINE void operator = (const CopyOnWritePointerTo<T> &copy);
00084   INLINE void operator = (To *object);
00085 
00086 #ifdef COW_THREADED
00087   INLINE CPT(To) get_read_pointer() const;
00088   INLINE PT(To) get_write_pointer();
00089 #else
00090   INLINE const To *get_read_pointer() const;
00091   INLINE To *get_write_pointer();
00092 #endif  // COW_THREADED
00093 
00094   INLINE To *get_unsafe_pointer();
00095 #endif  // CPPPARSER
00096 };
00097 
00098 #define COWPT(type) CopyOnWritePointerTo< type >
00099 
00100 #include "copyOnWritePointer.I"
00101 
00102 #endif
 All Classes Functions Variables Enumerations