1 /* 2 * Copyright 2006 Sony Computer Entertainment Inc. 3 * 4 * Licensed under the MIT Open Source License, for details please see license.txt or the website 5 * http://www.opensource.org/licenses/mit-license.php 6 * 7 */ 8 9 #ifndef __DAE_SMARTREF_H__ 10 #define __DAE_SMARTREF_H__ 11 12 #include <assert.h> 13 #include <dae/daeRefCountedObj.h> 14 15 /** 16 * The @c daeSmartRef template class automates reference counting for 17 * objects derived from @c daeRefCountedObj. 18 */ 19 template<class T> class daeSmartRef 20 { 21 public: 22 /** 23 * Constructor 24 */ 25 inline daeSmartRef() : _ptr(NULL) { } 26 27 /** 28 * Destructor 29 */ 30 inline ~daeSmartRef() { 31 checkedRelease(_ptr); 32 } 33 34 /** 35 * Copy Constructor that will convert from one template to the other. 36 * @param smartRef a daeSmartRef to the object to copy from. 37 */ 38 template<class U> 39 inline daeSmartRef(const daeSmartRef<U>& smartRef) : _ptr(smartRef.cast()) { 40 checkedRef(_ptr); 41 } 42 43 /** 44 * Function that returns a pointer to object being reference counted. 45 * @return the object being reference counted. 46 */ 47 inline T* cast() const { return _ptr; } 48 49 /** 50 * Copy Constructor. 51 * @param smartRef a daeSmartRef of the same template type to copy from 52 */ 53 inline daeSmartRef(const daeSmartRef<T>& smartRef) : _ptr(smartRef._ptr) { 54 checkedRef(_ptr); 55 } 56 57 /** 58 * Constructor 59 * @param ptr a pointer to an object of the same template type. 60 */ 61 inline daeSmartRef(T* ptr) : _ptr(ptr) { 62 checkedRef(_ptr); 63 } 64 65 /** 66 * Overloaded assignment operator which will convert between template types. 67 * @param smartRef a daeSmartRef to the object to copy from. 68 * @return Returns a reference to this object. 69 */ 70 template<class U> 71 inline const daeSmartRef<T>& operator=(const daeSmartRef<U>& smartRef) { 72 T* ptr = smartRef.cast(); 73 checkedRef(ptr); 74 checkedRelease(_ptr); 75 _ptr = ptr; 76 return *this; } 77 78 /** 79 * Overloaded assignment operator. 80 * @param other a daeSmartRef to the object to copy from. Must be of the same template type. 81 * @return Returns a reference to this object. 82 */ 83 inline const daeSmartRef<T>& operator=(const daeSmartRef<T>& other) { 84 T* ptr = other._ptr; 85 checkedRef(ptr); 86 checkedRelease(_ptr); 87 _ptr = ptr; 88 return *this; } 89 90 /** 91 * Overloaded assignment operator. 92 * @param ptr a pointer to the object to copy from. Must be of the same template type. 93 * @return Returns a reference to this object. 94 */ 95 inline const daeSmartRef<T>& operator=(T* ptr) { 96 checkedRef(ptr); 97 checkedRelease(_ptr); 98 _ptr = ptr; 99 return *this; } 100 101 /** 102 * Overloaded member selection operator. 103 * @return a pointer of the template class to the object. 104 */ 105 inline T* operator->() const { 106 assert (_ptr != (T*)NULL); return _ptr; } 107 108 /** 109 * Overloaded cast operator. 110 * @return a pointer of the template class to the object. 111 */ 112 inline operator T*() const { 113 return _ptr; } 114 115 /** 116 * Static cast function. 117 * @param smartRef a smartRef to cast from 118 * @return a pointer to an object of this template class 119 */ 120 template<class U> 121 inline static T* staticCast(const daeSmartRef<U>& smartRef) { 122 return static_cast<T*>(smartRef.cast()); } 123 124 private: 125 /* The pointer to the element which is being reference counted */ 126 T* _ptr; 127 }; 128 129 #endif // __DAE_SMARTREF_H__ 130 131 132 133 134 135