00001 // Filename: referenceCount.h 00002 // Created by: drose (23Oct98) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved 00008 // 00009 // All use of this software is subject to the terms of the Panda 3d 00010 // Software license. You should have received a copy of this license 00011 // along with this source code; you will also find a current copy of 00012 // the license at http://www.panda3d.org/license.txt . 00013 // 00014 // To contact the maintainers of this program write to 00015 // panda3d@yahoogroups.com . 00016 // 00017 //////////////////////////////////////////////////////////////////// 00018 00019 #ifndef REFERENCECOUNT_H 00020 #define REFERENCECOUNT_H 00021 00022 #include <pandabase.h> 00023 00024 #include "typedObject.h" 00025 #include "memoryUsage.h" 00026 #include "config_express.h" 00027 #include "atomicAdjust.h" 00028 00029 #include <stdlib.h> 00030 00031 #ifdef HAVE_RTTI 00032 #include <typeinfo> 00033 #endif 00034 00035 /////////////////////////////////////////////////////////////////// 00036 // Class : ReferenceCount 00037 // Description : A base class for all things that want to be 00038 // reference-counted. ReferenceCount works in 00039 // conjunction with PointerTo to automatically delete 00040 // objects when the last pointer to them goes away. 00041 //////////////////////////////////////////////////////////////////// 00042 class EXPCL_PANDAEXPRESS ReferenceCount { 00043 protected: 00044 INLINE ReferenceCount(); 00045 INLINE ReferenceCount(const ReferenceCount &); 00046 INLINE void operator = (const ReferenceCount &); 00047 INLINE ~ReferenceCount(); 00048 00049 PUBLISHED: 00050 INLINE int get_ref_count() const; 00051 INLINE int ref() const; 00052 INLINE int unref() const; 00053 00054 INLINE void test_ref_count_integrity() const; 00055 00056 public: 00057 static TypeHandle get_class_type() { 00058 return _type_handle; 00059 } 00060 static void init_type() { 00061 register_type(_type_handle, "ReferenceCount"); 00062 } 00063 00064 private: 00065 int _ref_count; 00066 static TypeHandle _type_handle; 00067 }; 00068 00069 template<class RefCountType> 00070 INLINE void unref_delete(RefCountType *ptr); 00071 00072 /////////////////////////////////////////////////////////////////// 00073 // Class : RefCountProxy 00074 // Description : A "proxy" to use to make a reference-countable object 00075 // whenever the object cannot inherit from 00076 // ReferenceCount for some reason. RefCountPr<MyClass> 00077 // can be treated as an instance of MyClass directly, 00078 // for the most part, except that it can be reference 00079 // counted. 00080 // 00081 // If you want to declare a RefCountProxy to something 00082 // that does not have get_class_type(), you will have to 00083 // define a template specialization on 00084 // _get_type_handle() and _do_init_type(), as in 00085 // typedObject.h. 00086 //////////////////////////////////////////////////////////////////// 00087 template<class Base> 00088 class RefCountProxy : public ReferenceCount { 00089 public: 00090 INLINE RefCountProxy(); 00091 INLINE RefCountProxy(const Base ©); 00092 00093 INLINE operator Base &(); 00094 INLINE operator const Base &() const; 00095 00096 static TypeHandle get_class_type() { 00097 return _type_handle; 00098 } 00099 static void init_type(); 00100 00101 private: 00102 Base _base; 00103 static TypeHandle _type_handle; 00104 }; 00105 00106 00107 /////////////////////////////////////////////////////////////////// 00108 // Class : RefCountObj 00109 // Description : Another kind of proxy, similar to RefCountProxy. 00110 // This one works by inheriting from the indicated base 00111 // type, giving it an is-a relation instead of a has-a 00112 // relation. As such, it's a little more robust, but 00113 // only works when the base type is, in fact, a class. 00114 //////////////////////////////////////////////////////////////////// 00115 template<class Base> 00116 class RefCountObj : public ReferenceCount, public Base { 00117 public: 00118 INLINE RefCountObj(); 00119 INLINE RefCountObj(const Base ©); 00120 00121 static TypeHandle get_class_type() { 00122 return _type_handle; 00123 } 00124 static void init_type(); 00125 00126 private: 00127 static TypeHandle _type_handle; 00128 }; 00129 00130 00131 00132 #include "referenceCount.I" 00133 00134 #endif