Changeset 10925 in vbox
- Timestamp:
- Jul 29, 2008 12:11:56 PM (17 years ago)
- svn:sync-xref-src-repo-rev:
- 33773
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/autores
r10918 r10925 25 25 26 26 /** 27 * Wrapper class around RTAutoRes to provide reference semantics. Certain 28 * operations like constructing from AutoRes objects passed by value are only 29 * possible if the object "transitions through" a wrapper struct. 30 * See auto_ptr in the C++ <memory> header. 27 * Wrapper class around RTAutoRes to provide reference semantics. This is 28 * needed to construct AutoRes objects from constant references to other 29 * AutoRes objects (such as temporary objects returned from functions) which 30 * can not be modified due to their const modifier, but are not actually 31 * constant objects. See a discussion of auto_ptr and auto_ptr_ref for more 32 * details. 31 33 */ 32 34 template <class T, void Destruct(T)> … … 38 40 }; 39 41 40 /** Re-casting template to convert a void fn(void *) to a void fn(T) */41 template <class T, void fn(void *)>42 inline void RTAutoResRecastVoid(T aT) { fn(aT); }43 44 42 /** 45 43 * An auto pointer-type class for resources which take a C-style destructor 46 44 * destructor (free() or equivalent). 47 45 */ 48 template <class T, void Destruct(T) = RTAutoResRecastVoid<T, RTMemFree>>46 template <class T, void Destruct(T)> 49 47 class RTAutoRes 50 48 { … … 54 52 public: 55 53 /** Constructor */ 56 RTAutoRes(T aValue = NULL) { mValue = aValue; } 54 RTAutoRes(T aValue = 0) { mValue = aValue; } 55 57 56 /** Destructor */ 58 ~RTAutoRes() { if (NULL != mValue) Destruct(mValue); } 59 60 /** release method to get the pointer's value and "reset" the pointer. */ 61 T release(void) { T aTmp = mValue; mValue = NULL; return aTmp; } 62 63 /** reset the pointer value to zero or to another pointer. */ 64 void reset(T aValue = NULL) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } } 65 66 /** 67 * Reallocate the resource value. Free the old value if allocation fails. 68 * @returns true if the new allocation succeeds, false otherwise 69 * @param Reallocator the function to be used for reallocating the 70 * resource. It should have equivalent semantics to 71 * C realloc. 72 * @note We can overload this member for other reallocator signatures as 73 * needed. 74 */ 75 template <void *Reallocator(void *, size_t)> 76 bool realloc(size_t cchNewSize) 77 { 78 T aNewValue = reinterpret_cast<T>(Reallocator(mValue, cchNewSize)); 79 if (aNewValue != NULL) 80 mValue = aNewValue; 81 else 82 Destruct(mValue); 83 return (aNewValue != NULL); 84 } 57 ~RTAutoRes() { if (mValue != 0) Destruct(mValue); } 85 58 86 59 /** Copy constructor */ … … 89 62 /** Copy from equivalent class */ 90 63 template <class T1> 91 RTAutoRes(RTAutoRes<T1, Destruct> &orig) { mValue = orig.release(); } 64 RTAutoRes(RTAutoRes<T1, Destruct> &orig) 65 { mValue = orig.release(); } 66 67 /** Convert a reference structure into an AutoRes pointer. */ 68 RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; } 92 69 93 70 /** Assignment operator. */ … … 109 86 RTAutoRes& operator=(T aValue) 110 87 { 111 if ( NULL != mValue)88 if (mValue != 0) 112 89 { 113 90 Destruct(mValue); … … 116 93 return *this; 117 94 } 118 119 #if 0120 /**121 * @todo I'm not sure how to express this now that our type is T and not122 * T*. Something like return type *T, which would only be valid for T a123 * pointer type. Or perhaps easiest to just leave it out.124 */125 126 /** Dereference with * operator. */127 T &operator*() { return *mValue; }128 #endif129 130 /** Dereference with -> operator. Only makes sense for pointer values. */131 T operator->() { return mValue; }132 133 /** Accessing the value inside. */134 T get(void) { return mValue; }135 136 /** Convert a reference structure into an AutoRes pointer. */137 RTAutoRes(RTAutoResRef<T, Destruct> ref) { mValue = ref.mValue; }138 95 139 96 /** Assign from a reference structure into an AutoRes pointer. */ … … 148 105 } 149 106 107 /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */ 108 template <class T1> 109 operator RTAutoRes<T1, Destruct>() 110 { return RTAutoRes<T1, Destruct>(release()); } 111 150 112 /** Typecast an AutoRes pointer to a reference structure. */ 151 113 template <class T1> 152 operator RTAutoResRef<T1, Destruct>() { return RTAutoResRef<T1, Destruct>(release()); } 114 operator RTAutoResRef<T1, Destruct>() 115 { return RTAutoResRef<T1, Destruct>(release()); } 153 116 154 /** Typecast an AutoRes pointer to an AutoRes pointer of a different type. */ 117 /** release method to get the pointer's value and "reset" the pointer. */ 118 T release(void) { T aTmp = mValue; mValue = 0; return aTmp; } 119 120 /** reset the pointer value to zero or to another pointer. */ 121 void reset(T aValue = 0) { if (aValue != mValue) { Destruct(mValue); mValue = aValue; } } 122 123 /** Accessing the value inside. */ 124 T get(void) { return mValue; } 125 }; 126 127 /** Re-casting template to convert a void fn(void *) to a void fn(T *) */ 128 template <class T, void fn(void *)> 129 inline void RTAutoResRecastVoid(T *aValue) { fn(aValue); } 130 131 template <class T, void Destruct(T *) = RTAutoResRecastVoid <T, RTMemFree> > 132 class RTAutoResPtr : public RTAutoRes <T *, Destruct> 133 { 134 public: 135 /** Constructor */ 136 RTAutoResPtr(T *aValue = NULL) : RTAutoRes <T *, Destruct>(aValue) {} 137 138 /** Copy constructors */ 139 RTAutoResPtr(RTAutoResPtr &orig) : RTAutoRes <T *, Destruct>(orig) {} 155 140 template <class T1> 156 operator RTAutoRes<T1, Destruct>() { return RTAutoRes<T1, Destruct>(release()); } 141 RTAutoResPtr(RTAutoResPtr<T1, Destruct> &orig) : RTAutoRes<T1 *, Destruct>(orig) {} 142 143 /** Assignment operators */ 144 RTAutoResPtr& operator=(RTAutoResPtr &orig) 145 { this->RTAutoRes <T *, Destruct>::operator=(orig); } 146 template <class T1> 147 RTAutoResPtr& operator=(RTAutoResPtr<T1, Destruct> &orig) 148 { this->RTAutoRes<T1 *, Destruct>::operator=(orig); return *this; } 149 RTAutoResPtr& operator=(T *aValue) 150 { this->RTAutoRes <T *, Destruct>::operator=(aValue); return *this; } 151 RTAutoResPtr& operator=(RTAutoResRef<T *, Destruct> ref) 152 { this->RTAutoRes <T *, Destruct>::operator=(ref); return *this; } 153 154 /** Dereference with * operator. */ 155 T &operator*() { return *this->get(); } 156 157 /** Dereference with -> operator. */ 158 T* operator->() { return this->get(); } 159 160 /** 161 * Reallocate the resource value. Free the old value if allocation fails. 162 * @returns true if the new allocation succeeds, false otherwise 163 * @param Reallocator the function to be used for reallocating the 164 * resource. It should have equivalent semantics to 165 * C realloc. 166 * @note We can overload this member for other reallocator signatures as 167 * needed. 168 */ 169 template <void *Reallocator(void *, size_t)> 170 bool realloc(size_t cchNewSize) 171 { 172 T *aNewValue = reinterpret_cast<T *>(Reallocator(this->release(), cchNewSize)); 173 if (aNewValue != NULL) /* This line is technically not necessary */ 174 this->reset(aNewValue); 175 return (aNewValue != NULL); 176 } 157 177 }; 158 178
Note:
See TracChangeset
for help on using the changeset viewer.